因为近段时间在一个系统,后端代码使用的技术栈是spring boot (版本1.5.12.RELEASE)、alibaba-spring-boot (版本1.5.12.0-SNAPSHOT)、pandora-boot (版本2018-05-release),写好各种mapper、service、controller层的代码之后免不了要进行测试,最高效的测试方法还是写单元测试,如果自己在本地把服务起来,页面上点点点,那是极其low极力不推荐的!
下面就介绍一下各个层的测试基类的写法:
pom依赖如下:
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.taobao.pandora</groupId>
<artifactId>pandora-boot-test</artifactId>
<scope>test</scope>
</dependency>
一、mapper层的测试
测试基类如下:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@MybatisTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public class BaseMapperTest {
}
说明:
1、使用@MybatisTest,如果不加注解@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE),那么mybatis使用的是内存数据库,并不是真实的tddl的数据库,会报表不存在的错,官方文档在此:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-test-autoconfigure/
Using a real database
The In-memory embedded databases generally work well for tests since they are fast and don’t require any developer installation. However if you prefer to run tests against a real database, you can use the @AutoConfigureTestDatabase as follow:
2、@Rollback(false) 单测完成默认会将数据回滚,如果不想回滚,想保留在数据库中,要加(false)。
二、service层的测试
测试基类如下:
@RunWith(PandoraBootRunner.class)
@DelegateTo(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
@MybatisTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@SpringBootTest(classes = TestServiceConfiguration.class)
public class BaseServiceTest {
}
说明:在做service层的测试的时候,还是遇到一些问题,网上各种搜资料,很多例子在进行service层的测试时将dao层进行了mock的办法,但我并不想mock,经过一顿勇猛猜测,最后终于跑通了。比起mapper层的测试,多了这么几个注解:
@RunWith(PandoraBootRunner.class)
@DelegateTo(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
参考资料: http://gitlab.alibaba-inc.com/middleware-container/pandora-boot/wikis/test
为什么要加最后一个注解呢,这个要看自己的项目来定,一开始写的是@SpringBootTest(classes = Application.class),发现运行测试的时候总有一些service无法注入,报错: No qualifying bean of type 'xxx' available。
后来按照网上的解决办法,自己写了个TestConfiguration,代码如下:
@ComponentScan(basePackages={
"com.alibaba.ais.feds.mapper",
"com.alibaba.ais.feds.service"})
@SpringBootApplication
public class TestServiceConfiguration {
public static void main(String[] args){
SpringApplication.run(TestServiceConfiguration.class, args);
}
}
具体的原因等空了还需要再推敲一下,遇到问题不能仅仅靠猜。
三、controller层的测试:
基类如下:
@RunWith(PandoraBootRunner.class)
@DelegateTo(SpringRunner.class)
@ActiveProfiles("test")
public class BaseAjaxControllerTest {
}
一般在进程controller层测试的时候,会将service层进行mock,我介绍2种写法,分别是mock的方法和不mock的方法:
1、不mock的方法:
@WebMvcTest(value = DrillScenarioAjaxController.class, secure = false)
public class DrillScenarioAjaxControllerTest extends BaseAjaxControllerTest{
@Autowired
private MockMvc mockMvc;
@Autowired
private ScenarioService scenarioService;
@Test
public void testExecScenario(){
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/scenario/42/exec?employeeId=57524")
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
try {
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
Assert.assertThat(response.getStatus(), equalTo(200));
System.out.println(response.getContentAsString());
}
catch (Exception e){
System.out.println(e.fillInStackTrace());
}
}
@Configuration
@EnableWebMvc
@Import({TestControllerConfiguration.class})
static class Config {
}
}
@ComponentScan(basePackages={
"com.alibaba.ais.feds.mapper",
"com.alibaba.ais.feds.service",
"com.alibaba.ais.feds.controller"})
@SpringBootApplication
public class TestControllerConfiguration {
public static void main(String[] args){
SpringApplication.run(TestControllerConfiguration.class, args);
}
}
2、mock的方法:
@WebMvcTest(value = ApplicationAjaxController.class, secure = false)
public class ApplicationAjaxControllerTest extends BaseAjaxControllerTest{
@Autowired
private MockMvc mockMvc;
@MockBean
//@Autowired
private ApplicationApi applicationApi;
@Autowired
private TokenService tokenService;
@Test
public void test() throws Exception {
String mockResult = "{\"object\":{\"applications\":[{\"name\":\"app-center\",\"id\":112608}]},\"successful\":true}";
Mockito.when(
applicationApi.queryApps(Mockito.anyMap())).thenReturn(JSON.parseObject(mockResult));
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/queryAppsByNameFromAone?query=app-center")
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
Assert.assertThat(response.getStatus(), equalTo(200));
Assert.assertThat(response.getContentAsString(),equalTo(mockResult));
}
@Configuration
@EnableWebMvc
@Import({TestControllerConfiguration.class})
static class Config {
}
}
注意:
一开始跑controller层测试的时候,response 总是404,后来发现一定要加上 @EnableWebMvc注解,问题解决。
好了,测试跑通,现在感觉想怎么测,就怎么测。