Spring Boot 集成 JUnit 单元测试

Spring Boot 集成 JUnit 单元测试为自己的应用编写单元测试是一个很好的习惯。在Java开发中最流行的测试工具非JUnit莫属,它已经成为Java单元测试的事实标准。Spring

大家好,欢迎来到IT知识分享网。

为自己的应用编写单元测试是一个很好的习惯。在Java开发中最流行的测试工具非JUnit莫属,它已经成为Java单元测试的事实标准。Spring Boot测试模块不仅集成JUnit框架,还提供了许多实用程序和注释,方便我们测试应用。

1. 添加依赖

在 pom.xml 文件中引入 spring-boot-starter-test

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${version}</version> <scope>test</scope> </dependency>

Spring Boot 2.2.x 开始集成的是JUnit 5。如果之前是使用的JUnit 4,可以使用JUnit 5中提供的老式引擎运行,需要添加 junit-vintage-engine 依赖。

Spring Boot 2.2.x发布很久了,现在最新稳定版是2.4.x。旧的总要被替代,所以本篇只用JUnit 5,关于JUnit 4的文章相信网上很多,官方也有给出使用说明,请自行查找。

2. 编写单元测试

@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class JUnitTest { @Test public void test() { // 测试代码 } }

@SpringBootTest 重要参数

  • args
    应用程序参数,如:args = “–app.test=one”
  • classes
    Spring Boot应用启动入口类名,该参数不指定时由Spring Boot默认查找。
  • webEnvironment
    默认情况下@SpringBootTest不会启动服务器。当测试Web应用时,需指定该参数以便加载上下文环境。

WebEnvironment枚举值说明:

  • MOCK
    默认值,加载WebApplicationContext并提供模拟Web环境。使用此注释时,不会启动嵌入式服务器。
  • RANDOM_PORT
    启动应用并随机监听一个端口。
  • DEFINED_PORT
    启动应用并监听自定义的端口(来自application.properties)或使用默认端口8080。
  • NONE
    ApplicationContext通过使用加载,SpringApplication但不提供任何网络环境(模拟或其他方式)。

@Test

注意 JUnit 5 的 @Test 注解在 org.junit.jupiter.api 包下。


如果应用使用Spring MVC和 Spring WebFlux,则优先MVC。测试WebFlux应用必须设置:

@SpringBootTest(properties = "spring.main.web-application-type=reactive") public class MyWebFluxTests { }

3. 自动装配测试

有时候我们只需要测试框架模块集成是否正常,不需要加载整个项目。可以使用 spring-boot-test-autoconfigure 模块中一些注解。整个框架被“切片”成独立的测试模块。

JSON 测试

测试JSON序列化与反序列化。如果是GSON或JSONB,使用 @GsonTester 或 @JsonbTester 注解。

/** * @author Engr-Z * @since 2021/1/18 */ @JsonTest public class MyJsonTest { @Autowired private JacksonTester<Map> json; @Test void testSerialize() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("name", "攻城狮·正"); map.put("websit", "engr-z.com"); Assertions.assertThat(this.json.write(map)).isEqualToJson("expected.json"); Assertions.assertThat(this.json.write(map)).hasJsonPathStringValue("@.make"); Assertions.assertThat(this.json.write(map)).extractingJsonPathStringValue("@.make") .isEqualTo("Honda"); } @Test void testDeserialize() throws Exception { String content = "{\"name\":\"攻城狮·正\",\"website\":\"engr-z.com\"}"; Assertions.assertThat(this.json.parse(content)); Assertions.assertThat(this.json.parseObject(content).get("website")).isEqualTo("engr-z.com"); } }

Spring MVC 测试

测试 /demo/hello 接口是否正常

/** * @author Engr-Z * @since 2021/1/18 */ @WebMvcTest(DemoController.class) public class SpringMVCTest { @Autowired private MockMvc mvc; @Test void test() throws Exception { RequestBuilder builder = MockMvcRequestBuilders.get("/demo/hello"); ResultActions resultActions = mvc.perform(builder); int status = resultActions.andReturn().getResponse().getStatus(); Assertions.assertEquals(200, status); } }

Spring WebFlux 测试

/** * @author Engr-Z * @since 2021/1/18 */ @WebFluxTest(DemoController.class) public class SpringWebFluxTest { @Autowired private WebTestClient webClient; @Test void test() throws Exception { webClient.get().uri("/demo/webflux") .accept(MediaType.TEXT_PLAIN) .exchange() .expectStatus().isOk(); } }

JDBC 测试

@JdbcTest @Transactional(propagation = Propagation.NOT_SUPPORTED) class JdbcTransactionalTests { }

自动装配还支持 JPA,Redis,Rest Client 等模块测试。更多请参考:Auto-configured Tests

@MockBean 和 @SpyBean

如果一个服务依赖于远程调用的结果。为了不影响我们做单元测试,可以使用@MockBean。以下是官方代码示例:

@SpringBootTest class MyTests { @MockBean private RemoteService remoteService; @Autowired private Reverser reverser; @Test void exampleTest() { // RemoteService has been injected into the reverser bean BDDMockito.given(this.remoteService.someCall()).willReturn("mock"); String reverse = reverser.reverseSomeCall(); Assertions.assertThat(reverse).isEqualTo("kcom"); } }

@SpyBean 和 @MockBean 不同之处是:对于未指定mock的方法,spy默认会调用真实的方法,有返回值的返回真实的返回值,而mock默认不执行,有返回值的,默认返回null


本篇只介绍 Spring Boot 集成 JUnit 单元测试,关于 JUnit 用法会在以后篇章详细讲解。


除非注明,否则均为攻城狮·正原创文章,转载请注明出处。

本文链接:https://engr-z.com/103.html

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/52080.html

(0)
上一篇 2024-08-10 18:45
下一篇 2024-08-16 16:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信