背景:在项目后端接口开发还未完成,我们无法进行自动化接口用例的调试,希望与开发同步完成接口自动化用例的编写及调试,待项目转测后,可以直接跑自动化用例,提高测试效率.
选用的maven + Spring Boot框架,代码的示例自己随便构造的, 由于实际项目接口没有做cookie的校验, 所以示例接口没有添加cookie和做cookie校验,后续有需求再研究...直接上代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>LMock</groupId>
<artifactId>spring-boot-LMock</artifactId>
<version>1.0.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${project.basedir}/lib</extdirs>
</compilerArguments>
</configuration>
</plugin> </plugins>
</build> </project>
resources目录下面的配置文件application.properties, 注意:该文件要utf8编码格式,不然中文会乱码
server.port=${port:8889}
logging.file=lmock.log
logging.path=logs #userInfo
u.name=张三
u.sex=男
u.age=20
u.addr=北京市紫禁城
u.status=0
u.uid=123456789 #loginInfo
login.account=123456789@qq.com,987654321@163.com
login.pwd=123456 #responsemsg
ERROR.CODE.1001={"resultCode": 1001, "msg": "用户名或者密码错误"}
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n"/>
<property name="LOG_PATH" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}"/>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}</fileNamePattern>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender> <appender name="CRAWLER_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/event.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/event.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%msg%n</pattern>
</encoder>
</appender> <logger name="com.business.intelligence.util.CrawlerLogger" level="INFO" additivity="false">
<appender-ref ref="CRAWLER_LOG"/>
</logger> <root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root> </configuration>
入口程序:Application.java
1 import org.springframework.boot.SpringApplication;
2 import org.springframework.boot.autoconfigure.SpringBootApplication;
3 import org.springframework.context.annotation.ComponentScan;
4
5 @SpringBootApplication
6 @ComponentScan("com.lmock")
7 public class Application {
8
9 public static void main(String[] args) {
10 SpringApplication.run(Application.class, args);
11 }
12 }
javabean的类, LoginInfo.java UserInfo.java
1 package com.lmock.bean;
2
3 import lombok.Data;
4
5 @Data
6 public class UserInfo {
7 private String name;
8 private String sex;
9 private int age;
10 private String addr;
11 private int status;
12 private String uid;
13
14 @Override
15 public String toString() {
16 return "{\"name\": \"" + name +
17 "\", \"sex\": \"" + sex +
18 "\", \"age\": " + age +
19 ", \"addr\": \"" + addr +
20 "\", \"status\": " + status +
21 ", \"uid\": \"" + uid +
22 "\"}";
23 }
24 }
1 package com.lmock.bean;
2
3 import lombok.Data;
4
5 @Data
6 public class LoginInfo {
7 private String account;
8 private String pwd;
9 }
配置文件数据处理类, ReadConfig.java
1 package com.lmock.data;
2
3 import com.lmock.bean.UserInfo;
4
5 import java.io.UnsupportedEncodingException;
6 import java.util.Arrays;
7 import java.util.List;
8 import java.util.Locale;
9 import java.util.ResourceBundle;
10
11 public class ReadConfig {
12 private static ResourceBundle bundle = ResourceBundle.getBundle("application", Locale.PRC);
13
14 public static String login_account = bundle.getString("login.account");
15 public static List<String> login_account_list = Arrays.asList(login_account.split(","));
16 public static String login_pwd = bundle.getString("login.pwd");
17 public static String ERROR_CODE_1001 = transcoding("ERROR.CODE.1001");
18
19 public String readUserInfo() {
20 UserInfo ui = new UserInfo();
21 ui.setName(transcoding("u.name"));
22 ui.setSex(transcoding("u.sex"));
23 ui.setAge(Integer.parseInt(transcoding("u.age")));
24 ui.setAddr(transcoding("u.addr"));
25 ui.setStatus(Integer.parseInt(transcoding("u.status")));
26 ui.setUid(transcoding("u.uid"));
27 return ui.toString();
28 }
29
30 private static String transcoding(String key) {
31 try {
32 return new String(bundle.getString(key).getBytes("ISO-8859-1"), "UTF8");
33 } catch (UnsupportedEncodingException e) {
34 return bundle.getString(key);
35 }
36 }
37
38 }
接口实现类, PostDemo.java
1 package com.lmock.server;
2
3 import com.lmock.bean.LoginInfo;
4 import com.lmock.bean.UserInfo;
5 import com.lmock.data.ReadConfig;
6 import org.springframework.web.bind.annotation.RequestBody;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RequestMethod;
9 import org.springframework.web.bind.annotation.RestController;
10
11 @RestController
12 public class PostDemo {
13 @RequestMapping(value = "/v1/getUserInfo", method = RequestMethod.POST)
14 public String getUserInfo(@RequestBody LoginInfo loginInfo){
15 UserInfo ui;
16 if(loginInfo.getAccount().equals("123456789@qq.com") && loginInfo.getPwd().equals("123456")){
17 ui = new UserInfo();
18 ui.setName("张三");
19 ui.setAge(20);
20 ui.setAddr("北京市紫禁城");
21 ui.setSex("男");
22 ui.setStatus(0);
23 ui.setUid("123456789");
24 return ui.toString();
25 }
26 return "{\"resultCode\": 1001, \"msg\": \"用户名或者密码错误\"}";
27 }
28
29 @RequestMapping(value = "/v2/getUserInfo", method = RequestMethod.POST)
30 public String getUserInfoWithConfig(@RequestBody LoginInfo loginInfo){
31 if(ReadConfig.login_account_list.contains(loginInfo.getAccount()) && loginInfo.getPwd().equals(ReadConfig.login_pwd)){
32 return new ReadConfig().readUserInfo();
33 }
34 return ReadConfig.ERROR_CODE_1001;
35 }
36 }
最后决定用V2的方式写Mock, 这样接口参数校验的逻辑写好后, 只需要修改properties配置文件数据即可, 这样就可以当作一个测试桩, 也可以把桩要返回的数据写成json文件,这里没有去研究了, 个人觉得写成配置文件也挺好的,特别是需要多个数据, 可以利用List
启动Mock后,接口请求的效果