springboot的单元测试(junit单元测试实例)

转载 原文:https://www.codenong.com/cs106212170/文章目录一.Junit测试二.集成测试1.SpringBoot测试-测试其中的Bean2.SpringBootWeb测试-启动tomcat3.SpringBootWeb测试-不启动tomcat(模拟环境)三.单元测试1.web层测试2.mybtismapper测试3.测试任意的bean4.Mock操作四.相关注解的汇总五.参考网站一.Junit测试当你的单元测试代码不需要用到..

大家好,又见面了,我是你们的朋友全栈君。

转载   原文:https://www.codenong.com/cs106212170/

一.Junit 测试

当你的单元测试代码不需要用到 Spring Boot 功能,而只是一个简单的测试时,你可以直接编写你的 Junit 测试代码:

public class SimpleJunitTest { 
   
   
    @Test
    public void testSayHi() { 
   
        System.out.println("Hi Junit.");
    }
   
}

二.集成测试

Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的集成测试。 在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启和使用它。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

1.Spring Boot 测试-测试其中的Bean

当你的集成测试代码需要用到 Spring Boot 功能时,你可以使用@SpringBootTest注解。
该注解是普通的 Spring 项目(非 Spring Boot 项目)中编写集成测试代码所使用的@ContextConfiguration注解的替代品。其作用是用于确定如何装载 Spring 应用程序的上下文资源

@SpringBootTest
class MysqlDataTestApplicationTests { 
   
    @Autowired
    TOrderInfoService tOrderInfoService;
    @Test
    public void tOrderInfoServiceSelect() { 
   
        TOrderInfo tOrderInfo = tOrderInfoService.selectByPrimaryKey(1L);
        Assertions.assertThat(tOrderInfo).isNotNull();
    }
}

当运行 Spring Boot 应用程序测试时,它会自动的从当前测试类所在的包起一层一层向上搜索,直到找到一个@SpringBootApplication或@SpringBootConfiguration注释类为止。以此来确定如何装载 Spring 应用程序的上下文资源。只要你以合理的方式组织你的代码,你项目的主配置通常是可以被发现的。

如果搜索算法搜索不到你项目的主配置文件,将报出异常:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=) with your test

解决办法是,按 Spring Boot 的约定重新组织你的代码结构,或者手工指定你要装载的主配置文件:

@SpringBootTest(classes = { 
   MysqlDataTestApplication.class})
class MysqlDataTestApplicationTests { 
   
    @Autowired
    TOrderInfoService tOrderInfoService;
    @Test
    public void tOrderInfoServiceSelect() { 
   
        TOrderInfo tOrderInfo = tOrderInfoService.selectByPrimaryKey(1L);
        Assertions.assertThat(tOrderInfo).isNotNull();
    }
}

2.Spring Boot Web 测试- 启动tomcat

当你想启动一个完整的 HTTP 服务器对 Spring Boot 的 Web 应用编写测试代码时,可以使用@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)注解开启一个随机的可用端口。Spring Boot 针对 REST 调用的测试提供了一个 TestRestTemplate 模板,它可以解析链接服务器的相对地址。

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MysqlDataTestApplicationTests2 { 
   

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void test() throws Exception { 
   
        String response = restTemplate.getForObject("/query", String.class);
        Assertions.assertThat(response).contains("QRC123456789012");
    }

}

3.Spring Boot Web 测试- 不启动tomcat(模拟环境)

@SpringBootTest
@AutoConfigureMockMvc
class MysqlDataTestApplicationTests2 { 
   

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test() throws Exception { 
   
        mockMvc.perform(MockMvcRequestBuilders.get("/query")).andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk());

    }

}

三.单元测试

上文中描述的两种集成测试的方案,相同的一点是都会构建整个Spring Context。这表示所有声明的bean,而不管声明的方式为何,都会被构建实例,并且都能被依赖。这里隐含的意思是从上到下整条依赖链上的代码都已实现。

  • Mock技术
    在开发的过程中进行测试,无法满足上述的条件,Mock技术可以让我们屏蔽掉下层的依赖,从而专注于当前的测试目标。Mock技术的思想是,当测试目标的下层依赖的行为是可预期的,那么测试目标本身的行为也是可预期的,测试就是把实际的结果和测试目标的预期结果做比较,而Mock就是预先设定下层依赖的行为表现。

  • Mock的流程

    1、将测试目标的依赖对象进行mock,设定其预期的行为表现。
    2、 对测试目标进行测试。
    3、检测测试结果,检查在依赖对象的预期行为下,测试目标的结果是否符合预期。

  • Mock的使用场景

    1、多人协作时,可以通过mock进行无等待的测试先行。
    2、当测试目标的依赖对象需要访问外部的服务,而外部服务不易获得时,可以通过mock来模拟服务可用。
    3、当在排查不容易复现的问题场景时,通过mock来模拟问题。

1.web层测试

当你想对 Spring MVC 控制器编写单元测试代码时,可以使用@WebMvcTest注解。它提供了自配置的 MockMvc,可以不需要完整启动 HTTP 服务器就可以快速测试 MVC 控制器。

@WebMvcTest(TestController.class)
class TestControllerTests { 
   

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private TOrderInfoService tOrderInfoService;

    @Test
    public void query() throws Exception { 
   
        TOrderInfo tOrderInfo = new TOrderInfo();
        tOrderInfo.setId(1L);
        tOrderInfo.setMerId("QRC123456789012");
        Mockito.when(tOrderInfoService.selectByPrimaryKey(1L)).thenReturn(tOrderInfo);
        mockMvc.perform(MockMvcRequestBuilders.get("/query")).andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("QRC123456789012")));
    }
}

使用@WebMvcTest注解时,只有一部分的 Bean 能够被扫描得到,它们分别是:

  • @Controller
  • @ControllerAdvice
  • @JsonComponent
  • Filter
  • WebMvcConfigurer
  • HandlerMethodArgumentResolver

其他常规的@Component(包括@Service、@Repository等)Bean 则不会被加载到 Spring 测试环境上下文中。

如果测试的 MVC 控制器中需要@ComponentBean 的参与,你可以使用@MockBean注解来协助完成
如果使用了mybatis的@MapperScan注解,则mapper也会自动装配,但是却么有DataSource,测试会失败,可以先注释掉@MapperScan注解

2.mybtis mapper 测试

@MybatisTest
// 连接真实的数据库,否则连接一个内存中的库,测试会失败
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class TOrderInfoMapperTests { 
   

    @Autowired
    TOrderInfoMapper tOrderInfoMapper;

    @Test
    // 测试数据不回滚
    @Rollback(false)
    // 事务是只读的,如果有更新操作,方法将失败
    // @Transactional(readOnly = true)
    public void  test3() throws Exception { 
   
        TOrderInfo orderInfo = new TOrderInfo();
        orderInfo.setMerId("133333333");
        orderInfo.setId(2L);
        int  i = tOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);
        Assertions.assertThat(i).isEqualTo(1);
    }
}

3.测试任意的bean

service层并不作为一种特殊的层,所以没有什么注解能表示“只构建service层的bean”这种概念。

这里将介绍另一种通用的测试场景,我要测试的是一个普通的bean,没有什么特殊的角色,比如不是担当特殊处理的controller,也不是负责持久化的dao组件,我们要测试的只是一个普通的bean。

上文中我们使用@SpringBootTest的默认机制,它去查找@SpringBootApplication的配置,据此构建Spring的上下文。查看@SpringBootTest的doc,其中有一句是:

Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.

这表示我们可以通过classes属性来指定Configuration类,或者定义内嵌的Configuration类来改变默认的配置。

@SpringBootTest
public class TOrderInfoServiceTest { 
   
    @Configuration
    static class TOrderInfoServiceConfig { 
   
        @Bean
        public TOrderInfoService cityService() { 
   
            return new TOrderInfoService();
        }
    }

    @Autowired
    private TOrderInfoService tOrderInfoService;

    @MockBean
    private TOrderInfoMapper tOrderInfoMapper;

    @Test
    public void test() { 
   
        TOrderInfo tOrderInfo = new TOrderInfo();
        tOrderInfo.setId(1L);
        tOrderInfo.setMerId("QRC123456789012");
        Mockito.when(tOrderInfoMapper.selectByPrimaryKey(1L)).thenReturn(tOrderInfo);

        TOrderInfo tOrderInfo1 = tOrderInfoService.selectByPrimaryKey(1L);
        Assertions.assertThat(tOrderInfo1).isNotNull();
    }
}

4.Mock操作

单元测试中,需要对测试目标的依赖进行mock,这里有必要对mock的细节介绍下。上文单元测试部分已对Mock的逻辑、流程和使用场景进行了介绍,此处专注于实践层面进行说明。

  • 根据方法参数设定预期行为
    一般的mock是对方法级别的mock,在方法有入参的情况下,方法的行为可能会跟方法的具体参数值有关。比如一个除法的方法,传入参数4、2得结果2,传入参数8、2得结果4,传入参数2、0得异常

mock可以针对不同的参数值设定不同的预期,如下所示:

@SpringBootTest
public class MathServiceTest { 
   

    @Configuration
    static class ConfigTest { 
   }

    @MockBean
    private MathService mathService;

    @Test
    public void testDivide() { 
   
        Mockito.when(mathService.divide(4, 2))
                .thenReturn(2);

        Mockito.when(mathService.divide(8, 2))
                .thenReturn(4);

        Mockito.when(mathService.divide(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(0))) // 必须同时用matchers语法
                .thenThrow(new RuntimeException("error"));

        Assertions.assertThat(mathService.divide(4, 2))
                .isEqualTo(2);

        Assertions.assertThat(mathService.divide(8, 2))
                .isEqualTo(4);

        Assertions.assertThatExceptionOfType(RuntimeException.class)
                .isThrownBy(() -> { 
   
                    mathService.divide(3, 0);
                })
                .withMessageContaining("error");
    }
}

上面的测试可能有些奇怪,mock的对象也同时作为测试的目标。这是因为我们的目的在于介绍mock,所以简化了测试流程。

从上述测试用例可以看出,我们除了可以指定具体参数时的行为,也可以指定参数满足一定匹配规则时的行为。

  • 有返回的方法
    对于有返回的方法,mock时可以设定的行为有:

返回设定的结果,如:

when(taskService.findResourcePool(any()))
        .thenReturn(resourcePool);

直接抛出异常,如:

when(taskService.createTask(any(), any(), any()))
        .thenThrow(new RuntimeException("zz"));

实际调用真实的方法,如:

when(taskService.createTask(any(), any(), any()))
        .thenCallRealMethod();

注意,调用真实的方法有违mock的本义,应该尽量避免。如果要调用的方法中调用了其他的依赖,需要自行注入其他的依赖,否则会空指针。

  • 无返回的方法
    对于无返回的方法,mock时可以设定的行为有:
    直接抛出异常,如:
doThrow(new RuntimeException("test"))
        .when(taskService).saveToDBAndSubmitToQueue(any());

四.相关注解的汇总

  • @SpringBootTest:
    spring的注解,通过扫描应用程序中的配置来构建测试用的Spring上下文。

  • @AutoConfigureMockMvc:
    spring的注解,能够自动配置MockMvc对象实例,用来在模拟测试环境中发送http请求。

  • @WebMvcTest:
    spring的注解,切片测试的一种。使之替换@SpringBootTest能将构建bean的范围限定于web层,但是web层的下层依赖bean,需要通过mock来模拟。也可以通过参数指定只实例化web层的某一个到多个controller。具体可参考Auto-configured Spring MVC Tests。

  • @RestClientTest:
    spring的注解,切片测试的一种。如果应用程序作为客户端访问其他Rest服务,可以通过这个注解来测试客户端的功能。具体参考Auto-configured REST Clients。

  • @MybatisTest:
    mybatis按照spring的习惯开发的注解,切片测试的一种。使之替换@SpringBootTest,能够将构建bean的返回限定于mybatis-mapper层。具体可参考mybatis-spring-boot-test-autoconfigure。

五.参考网站

https://www.jianshu.com/p/2e84bd5dc9d6
http://fanlychie.github.io/post/spring-boot-testing.html
https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/html/spring-boot-features.html#boot-features-testing
http://mybatis.org/spring-boot-starter/mybatis-spring-boot-test-autoconfigure/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/129066.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号