IT测试总结

IT测试主要测试模块之间的接口和接口数据传递关系,以及模块组合后的整体功能。

在做集成测试时,若涉及到数据库的数据变更的,最好在测试过后将数据还原,可以先构建一条新的数据测试完成后删除,防止印象到数据库中原有的数据。

Controller层测试

对于SpringBoot项目,Controller层的IT测试可以通过在类上加@AutoConfigureMockMvc注解并直接注入MockMvc的方式进行测试。

若对于一些特殊的测试,需要使用不同的配置的可使用@TestPropertySource(locations="classpath:test.application.properties")注解指定特定的配置文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class DashboardControllerTest {

@Autowired
private MockMvc mockMvc;

@Test
public void get_method_test() throws Exception {
this.mockMvc.perform(get("/test//v1")
.param("param1", "value1")
.param("param2", "value2")
.header("uid", "123456"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("{\"response_code\":\"00\"")));
}
}

对于MockMvc的使用还可以通过如下方式,这样可以不用在类上添加@AutoConfigureMockMvc注解:

1
2
3
4
5
6
7
8
9
10
@Autowired
private WebApplicationContext context;

private MockMvc mvc;

@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context)
.addFilter(new BaseParamCheckFilter()).build();
}

mockMvc.perform需要传入的是一个RequestBuilder,可以将其封装好了再传入,需要放入RequestBody中的参数可以通过content进行参数构造:

1
2
3
4
5
6
7
8
9
10
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("uuid", "68A");
httpHeaders.add("Content-Type", "application/json");
RequestBuilder request = post("/test/v1")
.headers(httpHeaders)
.content("{\"uuid\": \"68A\",\"param1\":\"aa\"}")
.accept(MediaType.APPLICATION_JSON);

mockMvc.perform(request).andExpect(status().isOk()).andDo(print()).andExpect(
content().string(containsString("{\"response_code\":\"02\",\"message\":\"请求参数缺失\"")));

对于返回结果的严重可以使用上面的示例通过MockMvcResultMatchers结合Matchers中的方法进行验证,也可以通过以下方式获取到具体结果后进行验证。

1
2
3
String responseString = mvc.perform(request)
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
  • perform:执行一个RequestBuilder请求,会自动执行SpringMVC的流程并映射到相应的控制器执行处理;
  • andExpect:添加ResultMatcher验证规则,验证控制器执行完成后结果是否正确;
  • andDo:添加ResultHandler结果处理器,比如调试时打印结果到控制台;
  • andReturn:最后返回相应的MvcResult;然后进行自定义验证/进行下一步的异步处理;

测试文件上传

对于文件大小限制的测试可以直接构建一个指定大小的byte数组。

1
2
3
4
5
6
7
MockMultipartFile xmlFile = new MockMultipartFile("xmlFile",
"emptyModel.xml", "text/plain", new byte[1024 * 1024 * 200 + 1]);

this.mockMvc.perform(MockMvcRequestBuilders.fileUpload("/test/config/add")
.file(xmlFile).param("mid", mid).param("status", "1"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("{\"message\":\"上传文件异常\"")));

如果对于真实的文件上传测试可以读取真实的文件传输:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
URL url = this.getClass().getClassLoader().getResource("test/errorFileType.txt");
File file = new File(url.getPath());

MockMultipartFile xmlFile = new MockMultipartFile("xmlFile",
"errorFileType.txt", "text/plain", getByte(file));

private byte[] getByte(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
return bos.toByteArray();
}
}
使用TestRestTemplate 对象测试
1
2
3
4
5
6
7
8
9
10
@Autowired
private TestRestTemplate template;

@Test
public void testController(){
// template.getForObject() 会得到 controller 返回的 json 值
String content = template.getForObject("/show/100", String.class);
// 使用断言测试,使用正确的断言
Assert.assertEquals("show100", content);
}

非Controller层测试

对于非Controller测试一般更简单一些,只需要注入相关的类构造入参进行具体的方法调用测试,输出结果进行严重即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = NONE)
public class ITConfigServiceTest {

@Autowired
private TestBusiConfigMapper testBuisConfigMapper;

@Autowired
private otherConfigService otherConfigService;

@Test
public void getConfigTest() {
List<Config> configList = testBuisConfigMapper.getAllConfigs();
assertNotNull(configList);
assertEquals(true, configList.size() > 0);
for (Config config : configList) {
Config config = otherConfigService.getConfig(config.getId());
assertNotNull(config);
}
}
}