項目環(huán)境
- SpringBoot project
- Controller 層的測試
SpringBoot test 涉及的幾個方面
- 如何在 test 環(huán)境下引入/創(chuàng)建 SpringBoot 的 application context,如需要某個 controller 的 bean
- 要不要/及如何開啟 Tomcat 服務(wù)器來驗證實際Http請求,Controller層的測試可以不開啟server嗎,如果不開啟如何測
- 開啟Tomcat和將所有組件全部注入開銷較大,有沒有方式針對特定Controller所需bean 進(jìn)行初始化
- full Spring application context 和 特定 application context 的概念
- Service 和 Dao 要不要依存在 Tomcat/mysql 下來測試
- Controller/Service/Dao 分層測試 VS 集成測試
前提
- 最開始幾個例子 都不涉及 組件依賴,即 @controller 沒有 autowire @Service 組件
- 后面會涉及 依賴注入的問題
代碼
pom 依賴
<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>
</dependency>
</dependencies>
Controller
@RestController
@RequestMapping
public class BookController {
@RequestMapping("/books")
public String book() {
System.out.println("controller");
return "book";
}
}
Test1 引入Spring上下文,但不啟動tomcat
@RunWith(SpringRunner.class)
@SpringBootTest //引入Spring上下文 -> 上下文中的 bean 可用,自動注入
public class BookControllerTest {
@Autowired
private BookController bookController; //自動注入
@Test
public void testControllerExists() {
Assert.assertNotNull(bookController);
}
}
解釋
- @SpringBootTest
- 告知Spring boot尋找main configuration class(主要配置類)
- 默認(rèn)是 @SpringBootApplication 所修飾的類
- 通過該主類 start Spring Application Context
- bean 可以注入
- Spring 提供的測試支持可以將 Application Context 緩存起來,這會使同一個配置類下的上下文資源在不同test case 間共享,所有測試只會產(chǎn)生一次啟動應(yīng)用的開銷
- @Autowired 注解的bean 在 測試方法運行前被注入
- 運行測試,通過 console 可以看到沒有 Tomcat 的日志打出,Tomcat server未啟動
Test2 引入Spring上下文,且啟動Tomcat 模擬生產(chǎn)環(huán)境,接收Http請求
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) //RANDOM_PORT 啟動Tomcat
public class BookControllerTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate testRestTemplate;
@Test
public void testBook() {
Assert.assertEquals(this.testRestTemplate.getForObject("http://localhost:" + port + "/books", String.class), "book");
}
}
解釋
- webEnvironment=RANDOM_PORT 啟動一個隨意端口的Tomcat
- @LocalServerPort 自動注入隨機端口
- @TestRestTemlpate Spring boot 提供一個TestRestTemplate,作為 Http Client
- 存在啟動Tomcat的開銷
Test3 引入Spring上下文,不啟動Tomcat, 由 MockMVC 發(fā)送請求
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc //啟動自動配置MockMVC
public class BookControllerTest {
@Autowired
private MockMvc mockMvc; //只需 autowire
@Test
public void testBook2() throws Exception {
this.mockMvc.perform(get("/books"))
.andExpect(status().is(200))
.andDo(MockMvcResultHandlers.print())
.andExpect(content().string("book"))
.andReturn();
}
}
解釋
- @AutoConfigureMockMvc 啟動自動配置 MockMvc
- mockmvc可執(zhí)行 http client 的功能
- print 打印 mock http 詳細(xì)信息
- console 沒有打印 Tomcat日志信息,Tomcat 不啟動
- full Spring application context is started
Test4 只引入Web 層 的Spring上下文,不啟動Tomcat, 由 MockMVC 發(fā)送請求
@RunWith(SpringRunner.class)
//@SpringBootTest //full Spring application context
@WebMvcTest
public class BookControllerTest {
@Autowired
private MockMvc mockMvc; //只需 autowire
@Autowired //可以正常注入
private BookController bookController;
//@Autowired //另建@Service BookService 類,編譯通過,但test運行時異常,不能注入 web 層以外的 bean
private BookService bookService;
@Test
public void testBook2() throws Exception {
this.mockMvc.perform(get("/books"))
.andExpect(status().is(200))
.andDo(MockMvcResultHandlers.print())
.andExpect(content().string("book"))
.andReturn();
}
}
解釋
- @WebMvcTest 和 @SpringBootTest 性質(zhì)一樣,都是為了start 應(yīng)用上下文
- @WebMvcTest 只能 啟動 web 層的上下文
- 能初始化:@Controller, @ControllerAdvice, @JsonComponent Filter, WebMvcConfigurer and HandlerMethodArgumentResolver beans
- 不能初始化: @Component, @Service or @Repository beans
- 還能初始化: Spring Security and MockMvc
- @SpringBootTest 可以啟動所有上下文,資源開銷不一樣
Test5 只引入Web 層 特定Controller 的Spring上下文,不啟動Tomcat, 由 MockMVC 發(fā)送請求
@RunWith(SpringRunner.class)
@WebMvcTest(BookControler.class) //明確指定引入的 哪個web controller 上下文
public class BookControllerTest {
@Autowired
private MockMvc mockMvc; //只需 autowire
@Autowired //可以正常注入
private BookController bookController;
//@Autowired //另建@RestController BookController2 類,編譯通過,但test運行時異常,不能注入 Book Controller以外的 bean
private BookController2 BookController2;
@Test
public void testBook2() throws Exception {
this.mockMvc.perform(get("/books"))
.andExpect(status().is(200))
.andDo(MockMvcResultHandlers.print())
.andExpect(content().string("book"))
.andReturn();
}
}
解釋
- @WebMvcTest(SthController.class) 未加 class 屬性的 時候注入所有 controller, 指明具體controller 則限制注入的對象
- 資源開銷不一樣
Test6 前5個例子都未涉及Controller的依賴問題,現(xiàn)在 controller 依賴 service
添加service
@Service
public class BookService { //添加 Service 層
public String addBook() {
return "book";
}
}
更改Controller,注入 service
@RestController
@RequestMapping
public class BookController {
@Autowired
private BookService bookService; //注入 依賴 service bean
@RequestMapping("/books")
public String addBook() {
return bookService.addBook();
}
}
Test 6-1 web layer application context
@RunWith(SpringRunner.class)
@WebMvcTest(BookControler.class) //明確指定引入的 哪個web controller 上下文
public class BookControllerTest {
@Autowired
private MockMvc mockMvc; //只需 autowire
@MockBean //mock 偽造 一個 bookService bean 否則,上下文環(huán)境中不存在,因為指定了 @WebMvcTest,否則應(yīng)用啟動異常
private BookService bookService;
@Autowired //可以正常注入
private BookController bookController;
@Test
public void testBook2() throws Exception {
//因為是mock出的bookService
//同時為了將測試范圍限定在 controller 層,所以將 service 層的調(diào)用固定化
//相當(dāng)于 service 層的邏輯沒有測試直接返回一個假定的結(jié)果
when(bookService.addBook()).thenReturn("book");
this.mockMvc.perform(get("/books"))
.andExpect(status().is(200))
.andDo(MockMvcResultHandlers.print())
.andExpect(content().string("book"))
.andReturn();
}
}
Test 6-2 full Spring application context
@RunWith(SpringRunner.class)
@SpringBootTest // 開啟 full Spring application context
@AutoConfigureMockMvc //啟動自動配置MockMVC
public class BookControllerTest {
@Autowired
private MockMvc mockMvc; //只需 autowire
//@MockBean //不需要偽造 一個 bookService bean 因為上下文環(huán)境中存在,因為指定了 @SpringBootTest
//private BookService bookService;
@Autowired //可以正常注入
private BookController bookController;
@Test
public void testBook2() throws Exception {
this.mockMvc.perform(get("/books"))
.andExpect(status().is(200))
.andDo(MockMvcResultHandlers.print())
.andExpect(content().string("book"))
.andReturn();
}
}
解釋
- 當(dāng)存在 組件 依賴時,如何初始化依賴組件的問題
- @WebMvcTest(SthController.class),指定的Controller 若需要其它 依賴,必須 @MockBean 偽造一個
- 否則,可用 full Spring Application context