本文介紹 Spring Boot 2 基于 JUnit 5 的單元測(cè)試實(shí)現(xiàn)方案粥诫。
目錄
- 簡(jiǎn)介
- JUnit 4 和 JUnit 5 的差異
- 忽略測(cè)試用例執(zhí)行
-
RunWith
配置 -
@Before
谤辜、@BeforeClass
哼审、@After
掘猿、@AfterClass
被替換
- 開發(fā)環(huán)境
- 示例
簡(jiǎn)介
Spring Boot 2.2.0 版本開始引入 JUnit 5 作為單元測(cè)試默認(rèn)庫(kù)鹊碍,在 Spring Boot 2.2.0 版本之前律歼,spring-boot-starter-test
包含了 JUnit 4 的依賴,Spring Boot 2.2.0 版本之后替換成了 Junit Jupiter乍桂。
JUnit 4 和 JUnit 5 的差異
1. 忽略測(cè)試用例執(zhí)行
JUnit 4:
@Test
@Ignore
public void testMethod() {
// ...
}
JUnit 5:
@Test
@Disabled("explanation")
public void testMethod() {
// ...
}
2. RunWith
配置
JUnit 4:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void contextLoads() {
}
}
JUnit 5:
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void contextLoads() {
}
}
3. @Before
冲杀、@BeforeClass
效床、@After
、@AfterClass
被替換
-
@BeforeEach
替換@Before
-
@BeforeAll
替換@BeforeClass
-
@AfterEach
替換@After
-
@AfterAll
替換@AfterClass
開發(fā)環(huán)境
- JDK 8
示例
創(chuàng)建 Spring Boot 工程权谁,參考:IntelliJ IDEA 創(chuàng)建 Spring Boot 工程剩檀。
添加
spring-boot-starter-web
依賴,最終pom.xml
如下旺芽。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>tutorial.spring.boot</groupId>
<artifactId>spring-boot-junit5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-junit5</name>
<description>Demo project for Spring Boot Unit Test with JUnit 5</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 工程創(chuàng)建好之后自動(dòng)生成了一個(gè)測(cè)試類沪猴。
package tutorial.spring.boot.junit5;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootJunit5ApplicationTests {
@Test
void contextLoads() {
}
}
這個(gè)測(cè)試類的作用是檢查應(yīng)用程序上下文是否可正常啟動(dòng)。@SpringBootTest
注解告訴 Spring Boot 查找?guī)?@SpringBootApplication
注解的主配置類采章,并使用該類啟動(dòng) Spring 應(yīng)用程序上下文运嗜。
- 補(bǔ)充待測(cè)試應(yīng)用邏輯代碼
4.1. 定義 Service 層接口
package tutorial.spring.boot.junit5.service;
public interface HelloService {
String hello(String name);
}
4.2. 定義 Controller 層
package tutorial.spring.boot.junit5.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import tutorial.spring.boot.junit5.service.HelloService;
@RestController
public class HelloController {
private final HelloService helloService;
public HelloController(HelloService helloService) {
this.helloService = helloService;
}
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
return helloService.hello(name);
}
}
4.3. 定義 Service 層實(shí)現(xiàn)
package tutorial.spring.boot.junit5.service.impl;
import org.springframework.stereotype.Service;
import tutorial.spring.boot.junit5.service.HelloService;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "Hello, " + name;
}
}
- 編寫發(fā)送 HTTP 請(qǐng)求的單元測(cè)試。
package tutorial.spring.boot.junit5;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testHello() {
String requestResult = this.restTemplate.getForObject("http://127.0.0.1:" + port + "/hello/spring",
String.class);
Assertions.assertThat(requestResult).contains("Hello, spring");
}
}
說明:
-
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
使用本地的一個(gè)隨機(jī)端口啟動(dòng)服務(wù)悯舟; -
@LocalServerPort
相當(dāng)于@Value("${local.server.port}")
担租; - 在配置了
webEnvironment
后,Spring Boot 會(huì)自動(dòng)提供一個(gè)TestRestTemplate
實(shí)例抵怎,可用于發(fā)送 HTTP 請(qǐng)求奋救。 - 除了使用
TestRestTemplate
實(shí)例發(fā)送 HTTP 請(qǐng)求外,還可以借助org.springframework.test.web.servlet.MockMvc
完成類似功能反惕,代碼如下:
package tutorial.spring.boot.junit5.controller;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {
@Autowired
private HelloController helloController;
@Autowired
private MockMvc mockMvc;
@Test
public void testNotNull() {
Assertions.assertThat(helloController).isNotNull();
}
@Test
public void testHello() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("Hello, spring"));
}
}
以上測(cè)試方法屬于整體測(cè)試尝艘,即將應(yīng)用上下文全都啟動(dòng)起來,還有一種分層測(cè)試方法姿染,譬如僅測(cè)試 Controller 層背亥。
- 分層測(cè)試。
package tutorial.spring.boot.junit5.controller;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import tutorial.spring.boot.junit5.service.HelloService;
@WebMvcTest
public class HelloControllerTest {
@Autowired
private HelloController helloController;
@Autowired
private MockMvc mockMvc;
@MockBean
private HelloService helloService;
@Test
public void testNotNull() {
Assertions.assertThat(helloController).isNotNull();
}
@Test
public void testHello() throws Exception {
Mockito.when(helloService.hello(Mockito.anyString())).thenReturn("Mock hello");
this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("Mock hello"));
}
}
說明:
-
@WebMvcTest
注釋告訴 Spring Boot 僅實(shí)例化 Controller 層,而不去實(shí)例化整體上下文隘梨,還可以進(jìn)一步指定僅實(shí)例化 Controller 層的某個(gè)實(shí)例:@WebMvcTest(HelloController.class)
程癌; - 因?yàn)橹粚?shí)例化了 Controller 層,所以依賴的 Service 層實(shí)例需要通過
@MockBean
創(chuàng)建轴猎,并通過Mockito
的方法指定 Mock 出來的 Service 層實(shí)例在特定情況下方法調(diào)用時(shí)的返回結(jié)果嵌莉。