使用springboot对各个层的代码进行测试

简介: 因为近段时间在一个系统,后端代码使用的技术栈是spring boot (版本1.5.12.RELEASE)、alibaba-spring-boot (版本1.5.12.0-SNAPSHOT)、pandora-boot (版本2018-05-release),写好各种mapper、service、c.

因为近段时间在一个系统,后端代码使用的技术栈是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注解,问题解决。

好了,测试跑通,现在感觉想怎么测,就怎么测。

目录
相关文章
|
22天前
|
Java 关系型数据库 数据库连接
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
22 1
|
2月前
|
测试技术
包含用例执行时间的测试报告代码
包含用例执行时间的测试报告代码
|
8天前
|
Java 测试技术
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
14 0
|
1月前
|
Java 测试技术
单元测试编写可测试代码
单元测试编写可测试代码
19 2
|
1月前
|
Java 测试技术 数据库
springboot大学生体质测试管理系统
springboot大学生体质测试管理系统
|
1月前
|
存储 JavaScript 前端开发
基于SpringBoot的医护人员排班系统(代码+数据库+文档)
基于SpringBoot的医护人员排班系统(代码+数据库+文档)
|
1月前
|
前端开发 Java 数据库连接
基于SpringBoot宠物领养系统的设计与实现(代码+数据库+文档)
基于SpringBoot宠物领养系统的设计与实现(代码+数据库+文档)
|
1月前
|
存储 Java 数据库
基于springboot的医院信息管理系统(程序+代码+文档)
基于springboot的医院信息管理系统(程序+代码+文档)
|
1月前
|
存储 前端开发 Java
基于springboot的图书管理系统(代码+数据库+文档)
基于springboot的图书管理系统(代码+数据库+文档)
|
2月前
|
存储 测试技术
统计测试结果的代码实现接昨天
统计测试结果的代码实现接昨天