日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
有啥不同?來看看SpringBoot基于JUnit5實現(xiàn)單元測試

 目錄

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:

 
 
 
  1. @Test  
  2. @Ignore  
  3. public void testMethod() {  
  4.    // ...  

JUnit 5:

 
 
 
  1. @Test  
  2. @Disabled("explanation")  
  3. public void testMethod() {  
  4.    // ...  

2. RunWith 配置

JUnit 4:

 
 
 
  1. @RunWith(SpringRunner.class)  
  2. @SpringBootTest  
  3. public class ApplicationTests {  
  4.     @Test  
  5.     public void contextLoads() {  
  6.     }  

JUnit 5:

 
 
 
  1. @ExtendWith(SpringExtension.class)  
  2. @SpringBootTest 
  3. public class ApplicationTests {  
  4.     @Test  
  5.     public void contextLoads() {  
  6.     }  

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 如下。

 
 
 
  1.   
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     4.0.0  
  4.       
  5.         org.springframework.boot  
  6.         spring-boot-starter-parent  
  7.         2.2.6.RELEASE  
  8.           
  9.       
  10.     tutorial.spring.boot  
  11.     spring-boot-junit5  
  12.     0.0.1-SNAPSHOT  
  13.     spring-boot-junit5  
  14.     Demo project for Spring Boot Unit Test with JUnit 5  
  15.       
  16.         1.8  
  17.       
  18.       
  19.           
  20.             org.springframework.boot  
  21.             spring-boot-starter-web  
  22.           
  23.           
  24.             org.springframework.boot  
  25.             spring-boot-starter-test  
  26.             test  
  27.              
  28.                    
  29.                     org.junit.vintage  
  30.                     junit-vintage-engine  
  31.                   
  32.               
  33.           
  34.       
  35.       
  36.           
  37.               
  38.                 org.springframework.boot  
  39.                 spring-boot-maven-plugin  
  40.               
  41.           
  42.      
  43.  

3.工程創(chuàng)建好之后自動生成了一個測試類。

 
 
 
  1. package tutorial.spring.boot.junit5;  
  2. import org.junit.jupiter.api.Test;  
  3. import org.springframework.boot.test.context.SpringBootTest;  
  4. @SpringBootTest  
  5. class SpringBootJunit5ApplicationTests {  
  6.     @Test  
  7.     void contextLoads() {  
  8.     }  

這個測試類的作用是檢查應(yīng)用程序上下文是否可正常啟動。@SpringBootTest 注解告訴 Spring Boot 查找?guī)?@SpringBootApplication 注解的主配置類,并使用該類啟動 Spring 應(yīng)用程序上下文。Java知音公眾號內(nèi)回復(fù)“后端面試”, 送你一份Java面試題寶典

4.補充待測試應(yīng)用邏輯代碼

4.1. 定義 Service 層接口

 
 
 
  1. package tutorial.spring.boot.junit5.service;  
  2. public interface HelloService { 
  3.     String hello(String name);  

4.2. 定義 Controller 層

 
 
 
  1. package tutorial.spring.boot.junit5.controller;  
  2. import org.springframework.web.bind.annotation.GetMapping;  
  3. import org.springframework.web.bind.annotation.PathVariable;  
  4. import org.springframework.web.bind.annotation.RestController;  
  5. import tutorial.spring.boot.junit5.service.HelloService;  
  6. @RestController  
  7. public class HelloController {  
  8.     private final HelloService helloService;  
  9.     public HelloController(HelloService helloService) { 
  10.         this.helloService = helloService;  
  11.     }  
  12.     @GetMapping("/hello/{name}")  
  13.     public String hello(@PathVariable("name") String name) {  
  14.         return helloService.hello(name);  
  15.     }  

4.3. 定義 Service 層實現(xiàn)

 
 
 
  1. package tutorial.spring.boot.junit5.service.impl;  
  2. import org.springframework.stereotype.Service;  
  3. import tutorial.spring.boot.junit5.service.HelloService;  
  4. @Service 
  5. public class HelloServiceImpl implements HelloService {  
  6.     @Override  
  7.     public String hello(String name) {  
  8.         return "Hello, " + name;  
  9.     }  

5.編寫發(fā)送 HTTP 請求的單元測試。

 
 
 
  1. package tutorial.spring.boot.junit5;  
  2. import org.assertj.core.api.Assertions;  
  3. import org.junit.jupiter.api.Test;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.boot.test.context.SpringBootTest;  
  6. import org.springframework.boot.test.web.client.TestRestTemplate;  
  7. import org.springframework.boot.web.server.LocalServerPort;  
  8. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)  
  9. public class HttpRequestTest {  
  10.     @LocalServerPort  
  11.     private int port;  
  12.     @Autowired  
  13.     private TestRestTemplate restTemplate; 
  14.     @Test  
  15.     public void testHello() {  
  16.         String requestResult = this.restTemplate.getForObject("http://127.0.0.1:" + port + "/hello/spring",  
  17.                 String.class);  
  18.         Assertions.assertThat(requestResult).contains("Hello, spring");  
  19.     }  

說明:

  •  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 完成類似功能,代碼如下: 
 
 
 
  1. package tutorial.spring.boot.junit5.controller;  
  2. import org.assertj.core.api.Assertions;  
  3. import org.junit.jupiter.api.Test;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;  
  6. import org.springframework.boot.test.context.SpringBootTest;  
  7. import org.springframework.test.web.servlet.MockMvc;  
  8. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;  
  9. import org.springframework.test.web.servlet.result.MockMvcResultHandlers;  
  10. import org.springframework.test.web.servlet.result.MockMvcResultMatchers;  
  11. @SpringBootTest  
  12. @AutoConfigureMockMvc  
  13. public class HelloControllerTest {  
  14.     @Autowired  
  15.     private HelloController helloController;  
  16.     @Autowired  
  17.     private MockMvc mockMvc;  
  18.     @Test  
  19.     public void testNotNull() {  
  20.         Assertions.assertThat(helloController).isNotNull();  
  21.     }  
  22.     @Test  
  23.     public void testHello() throws Exception {  
  24.         this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))  
  25.                 .andDo(MockMvcResultHandlers.print())  
  26.                 .andExpect(MockMvcResultMatchers.status().isOk())  
  27.                 .andExpect(MockMvcResultMatchers.content().string("Hello, spring")); 
  28.     }  

以上測試方法屬于整體測試,即將應(yīng)用上下文全都啟動起來,還有一種分層測試方法,譬如僅測試 Controller 層。

6.分層測試。

 
 
 
  1. package tutorial.spring.boot.junit5.controller;  
  2. import org.assertj.core.api.Assertions;  
  3. import org.junit.jupiter.api.Test;  
  4. import org.mockito.Mockito;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;  
  7. import org.springframework.boot.test.mock.mockito.MockBean;  
  8. import org.springframework.test.web.servlet.MockMvc;  
  9. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;  
  10. import org.springframework.test.web.servlet.result.MockMvcResultHandlers;  
  11. import org.springframework.test.web.servlet.result.MockMvcResultMatchers;  
  12. import tutorial.spring.boot.junit5.service.HelloService;  
  13. @WebMvcTest  
  14. public class HelloControllerTest {  
  15.     @Autowired  
  16.     private HelloController helloController;  
  17.     @Autowired  
  18.     private MockMvc mockMvc; 
  19.     @MockBean  
  20.     private HelloService helloService;  
  21.     @Test  
  22.     public void testNotNull() {  
  23.         Assertions.assertThat(helloController).isNotNull();  
  24.     }  
  25.     @Test  
  26.     public void testHello() throws Exception {  
  27.         Mockito.when(helloService.hello(Mockito.anyString())).thenReturn("Mock hello");  
  28.         this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))  
  29.                 .andDo(MockMvcResultHandlers.print())  
  30.                 .andExpect(MockMvcResultMatchers.status().isOk())  
  31.                 .andExpect(MockMvcResultMatchers.content().string("Mock hello"));  
  32.     }  

說明:

@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