新聞中心
目錄

10年積累的成都網(wǎng)站設(shè)計、成都網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有于洪免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
- 簡介
- JUnit 4 和 JUnit 5 的差異
- 忽略測試用例執(zhí)行
- RunWith 配置
- @Before、@BeforeClass、@After、@AfterClass 被替換
- 開發(fā)環(huán)境
- 示例
簡介
Spring Boot 2.2.0 版本開始引入 JUnit 5 作為單元測試默認(rèn)庫,在 Spring Boot 2.2.0 版本之前,spring-boot-starter-test 包含了 JUnit 4 的依賴,Spring Boot 2.2.0 版本之后替換成了 Junit Jupiter。
JUnit 4 和 JUnit 5 的差異
1. 忽略測試用例執(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
示例
1.創(chuàng)建 Spring Boot 工程。
2.添加 spring-boot-starter-web 依賴,最終 pom.xml 如下。
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE tutorial.spring.boot spring-boot-junit5 0.0.1-SNAPSHOT spring-boot-junit5 Demo project for Spring Boot Unit Test with JUnit 5 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
3.工程創(chuàng)建好之后自動生成了一個測試類。
- package tutorial.spring.boot.junit5;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
- @SpringBootTest
- class SpringBootJunit5ApplicationTests {
- @Test
- void contextLoads() {
- }
- }
這個測試類的作用是檢查應(yīng)用程序上下文是否可正常啟動。@SpringBootTest 注解告訴 Spring Boot 查找?guī)?@SpringBootApplication 注解的主配置類,并使用該類啟動 Spring 應(yīng)用程序上下文。Java知音公眾號內(nèi)回復(fù)“后端面試”, 送你一份Java面試題寶典
4.補充待測試應(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 層實現(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;
- }
- }
5.編寫發(fā)送 HTTP 請求的單元測試。
- 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 使用本地的一個隨機端口啟動服務(wù);
- @LocalServerPort 相當(dāng)于 @Value("${local.server.port}");
- 在配置了 webEnvironment 后,Spring Boot 會自動提供一個 TestRestTemplate 實例,可用于發(fā)送 HTTP 請求。
- 除了使用 TestRestTemplate 實例發(fā)送 HTTP 請求外,還可以借助 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"));
- }
- }
以上測試方法屬于整體測試,即將應(yīng)用上下文全都啟動起來,還有一種分層測試方法,譬如僅測試 Controller 層。
6.分層測試。
- 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 僅實例化 Controller 層,而不去實例化整體上下文,還可以進(jìn)一步指定僅實例化 Controller 層的某個實例:@WebMvcTest(HelloController.class);
因為只實例化了 Controller 層,所以依賴的 Service 層實例需要通過 @MockBean 創(chuàng)建,并通過 Mockito 的方法指定 Mock 出來的 Service 層實例在特定情況下方法調(diào)用時的返回結(jié)果。
分享文章:有啥不同?來看看SpringBoot基于JUnit5實現(xiàn)單元測試
文章來源:http://m.5511xx.com/article/cdsghsc.html


咨詢
建站咨詢
