欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot Test及注解的使用詳解

 更新時(shí)間:2023年07月11日 10:16:56   作者:qq_44783481  
這篇文章主要介紹了SpringBoot Test及注解的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、Spring Boot Test介紹

Spring Test與JUnit等其他測(cè)試框架結(jié)合起來,提供了便捷高效的測(cè)試手段。

而Spring Boot Test 是在Spring Test之上的再次封裝,增加了切片測(cè)試,增強(qiáng)了mock能力。

整體上,Spring Boot Test支持的測(cè)試種類,

大致可以分為如下三類

  • 單元測(cè)試:一般面向方法,編寫一般業(yè)務(wù)代碼時(shí),測(cè)試成本較大。涉及到的注解有@Test。
  • 切片測(cè)試:一般面向難于測(cè)試的邊界功能,介于單元測(cè)試和功能測(cè)試之間。涉及到的注解有@RunWith @WebMvcTest等。
  • 功能測(cè)試:一般面向某個(gè)完整的業(yè)務(wù)功能,同時(shí)也可以使用切面測(cè)試中的mock能力,推薦使用。涉及到的注解有@RunWith @SpringBootTest等。

功能測(cè)試過程中的幾個(gè)關(guān)鍵要素及支撐方式如下

  • 測(cè)試運(yùn)行環(huán)境:通過@RunWith 和 @SpringBootTest啟動(dòng)spring容器。
  • mock能力:Mockito提供了強(qiáng)大mock功能。
  • 斷言能力:AssertJ、Hamcrest、JsonPath提供了強(qiáng)大的斷言能力。

二、快速開始

增加spring-boot-starter-test依賴,使用@RunWith和@SpringBootTest注解,即可開始測(cè)試。

添加依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

一旦依賴了spring-boot-starter-test,下面這些類庫將被一同依賴進(jìn)去:

  • JUnit:java測(cè)試事實(shí)上的標(biāo)準(zhǔn),默認(rèn)依賴版本是4.12(JUnit5和JUnit4差別比較大,集成方式有不同)。
  • Spring Test & Spring Boot Test:Spring的測(cè)試支持。
  • AssertJ:提供了流式的斷言方式。
  • Hamcrest:提供了豐富的matcher。
  • Mockito:mock框架,可以按類型創(chuàng)建mock對(duì)象,可以根據(jù)方法參數(shù)指定特定的響應(yīng),也支持對(duì)于mock調(diào)用過程的斷言。
  • JSONassert:為JSON提供了斷言功能。
  • JsonPath:為JSON提供了XPATH功能。

1. 單元測(cè)試

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTests {
    @Autowired
    private UserService userService;
    @Test
    public void testAddUser() {
        User user = new User();
        user.setName("john");
        user.setAddress("earth");
        userService.add(user);
    }
}

@RunWith是Junit4提供的注解,將Spring和Junit鏈接了起來。假如使用Junit5,不再需要使用@ExtendWith注解,@SpringBootTest和其它@*Test默認(rèn)已經(jīng)包含了該注解。

@SpringBootTest替代了spring-test中的@ContextConfiguration注解,目的是加載ApplicationContext,啟動(dòng)spring容器。

使用@SpringBootTest時(shí)并沒有像@ContextConfiguration一樣顯示指定locations或classes屬性,原因在于@SpringBootTest注解會(huì)自動(dòng)檢索程序的配置文件,檢索順序是從當(dāng)前包開始,逐級(jí)向上查找被@SpringBootApplication或@SpringBootConfiguration注解的類。

2. 功能測(cè)試

一般情況下,使用@SpringBootTest后,Spring將加載所有被管理的bean,基本等同于啟動(dòng)了整個(gè)服務(wù),此時(shí)便可以開始功能測(cè)試。

由于web服務(wù)是最常見的服務(wù),且我們對(duì)于web服務(wù)的測(cè)試有一些特殊的期望,所以@SpringBootTest注解中,給出了webEnvironment參數(shù)指定了web的environment,

該參數(shù)的值一共有四個(gè)可選值:

  • MOCK:此值為默認(rèn)值,該類型提供一個(gè)mock環(huán)境,可以和@AutoConfigureMockMvc或@AutoConfigureWebTestClient搭配使用,開啟Mock相關(guān)的功能。注意此時(shí)內(nèi)嵌的服務(wù)(servlet容器)并沒有真正啟動(dòng),也不會(huì)監(jiān)聽web服務(wù)端口。
  • RANDOM_PORT:?jiǎn)?dòng)一個(gè)真實(shí)的web服務(wù),監(jiān)聽一個(gè)隨機(jī)端口。
  • DEFINED_PORT:?jiǎn)?dòng)一個(gè)真實(shí)的web服務(wù),監(jiān)聽一個(gè)定義好的端口(從application.properties讀?。?/li>
  • NONE:?jiǎn)?dòng)一個(gè)非web的ApplicationContext,既不提供mock環(huán)境,也不提供真實(shí)的web服務(wù)。

注:如果當(dāng)前服務(wù)的classpath中沒有包含web相關(guān)的依賴,spring將啟動(dòng)一個(gè)非web的ApplicationContext,此時(shí)的webEnvironment就沒有什么意義了。

3. 切片測(cè)試

所謂切片測(cè)試,官網(wǎng)文檔稱為 “slice” of your application,實(shí)際上是對(duì)一些特定組件的稱呼。這里的slice并非單獨(dú)的類(畢竟普通類只需要基于JUnit的單元測(cè)試即可),而是介于單元測(cè)試和集成測(cè)試中間的范圍。

slice是指一些在特定環(huán)境下才能執(zhí)行的模塊,比如MVC中的Controller、JDBC數(shù)據(jù)庫訪問、Redis客戶端等,這些模塊大多脫離特定環(huán)境后不能獨(dú)立運(yùn)行,假如spring沒有為此提供測(cè)試支持,開發(fā)者只能啟動(dòng)完整服務(wù)對(duì)這些模塊進(jìn)行測(cè)試,這在一些復(fù)雜的系統(tǒng)中非常不方便,所以spring為這些模塊提供了測(cè)試支持,使開發(fā)者有能力單獨(dú)對(duì)這些模塊進(jìn)行測(cè)試。

通過@*Test開啟具體模塊的測(cè)試支持,開啟后spring僅加載相關(guān)的bean,無關(guān)內(nèi)容不會(huì)被加載。

使用@WebMvcTest用來校驗(yàn)controllers是否正常工作的示例:

@RunWith(SpringRunner.class)
@WebMvcTest(IndexController.class)
public class SpringBootTest {
    @Autowired
    private MockMvc mvc;
    @Test
    public void testExample() throws Exception {
        //groupManager訪問路徑
        //param傳入?yún)?shù)
        MvcResult result=mvc.perform(MockMvcRequestBuilders.post("/groupManager").param("pageNum","1").param("pageSize","10")).andReturn();
        MockHttpServletResponse response = result.getResponse();
        String content = response.getContentAsString();
        List<JtInfoDto> jtInfoDtoList = GsonUtils.toObjects(content, new TypeToken<List<JtInfoDto>>() {}.getType());
        for(JtInfoDto infoDto : jtInfoDtoList){
            System.out.println(infoDto.getJtCode());
        }
    }
}

使用@WebMvcTest和MockMvc搭配使用,可以在不啟動(dòng)web容器的情況下,對(duì)Controller進(jìn)行測(cè)試(注意:僅僅只是對(duì)controller進(jìn)行簡(jiǎn)單的測(cè)試,如果Controller中依賴用@Autowired注入的service、dao等則不能這樣測(cè)試)。

三、注解詳解

Spring為了避免的繁瑣難懂的xml配置,引入大量annotation進(jìn)行系統(tǒng)配置,確實(shí)減輕了配置工作量。

由此,理解這些annotation變得尤為重要,一定程度上講,對(duì)Spring Boot Test的使用,就是對(duì)其相關(guān)annotation的使用。

1. 按功能分類

從功能上講,Spring Boot Test中的注解主要分如下幾類:

  • 配置類型:@TestConfiguration等。提供一些測(cè)試相關(guān)的配置入口。
  • mock類型:@MockBean等。提供mock支持。
  • 啟動(dòng)測(cè)試類型:@SpringBootTest。以Test結(jié)尾的注解,具有加載applicationContext的能力。
  • 自動(dòng)配置類型:@AutoConfigureJdbc等。以AutoConfigure開頭的注解,具有加載測(cè)試支持功能的能力。

(1) 配置類型的注解

  • @TestComponent:該注解是另一種@Component,在語義上用來指定某個(gè)Bean是專門用于測(cè)試的。該注解適用于測(cè)試代碼和正式混合在一起時(shí),不加載被該注解描述的Bean,使用不多。
  • @TestConfiguration:該注解是另一種@TestComponent,它用于補(bǔ)充額外的Bean或覆蓋已存在的Bean。在不修改正式代碼的前提下,使配置更加靈活。
  • @TypeExcludeFilters:用來排除@TestConfiguration和@TestComponent。適用于測(cè)試代碼和正式代碼混合的場(chǎng)景,使用不多。
  • @OverrideAutoConfiguration:可用于覆蓋@EnableAutoConfiguration,與ImportAutoConfiguration結(jié)合使用,以限制所加載的自動(dòng)配置類。在不修改正式代碼的前提下,提供了修改配置自動(dòng)配置類的能力。
  • @PropertyMapping:定義@AutoConfigure*注解中用到的變量名稱,例如在@AutoConfigureMockMvc中定義名為spring.test.mockmvc.webclient.enabled的變量。一般不使用。

使用@SpringBootApplication啟動(dòng)測(cè)試或者生產(chǎn)代碼,被@TestComponent描述的Bean會(huì)自動(dòng)被排除掉。如果不是則需要向@SpringBootApplication添加TypeExcludeFilter。

(2) mock類型的注解

  • @MockBean:用于mock指定的class或被注解的屬性。
  • @MockBeans:使@MockBean支持在同一類型或?qū)傩陨隙啻纬霈F(xiàn)。
  • @SpyBean:用于spy指定的class或被注解的屬性。
  • @SpyBeans:使@SpyBean支持在同一類型或?qū)傩陨隙啻纬霈F(xiàn)

@MockBean和@SpyBean這兩個(gè)注解,在mockito框架中本來已經(jīng)存在,且功能基本相同。Spring Boot Test又定義一份重復(fù)的注解,目的在于使MockBean和SpyBean被ApplicationContext管理,從而方便使用。

MockBean和SpyBean功能非常相似,都能模擬方法的各種行為。不同之處在于MockBean是全新的對(duì)象,跟正式對(duì)象沒有關(guān)系;而SpyBean與正式對(duì)象緊密聯(lián)系,可以模擬正式對(duì)象的部分方法,沒有被模擬的方法仍然可以運(yùn)行正式代碼。

(3) 自動(dòng)配置類型的注解(@AutoConfigure*)

  • @AutoConfigureJdbc 自動(dòng)配置JDBC
  • @AutoConfigureCache 自動(dòng)配置緩存
  • @AutoConfigureDataLdap 自動(dòng)配置LDAP
  • @AutoConfigureJson 自動(dòng)配置JSON
  • @AutoConfigureJsonTesters 自動(dòng)配置JsonTester
  • @AutoConfigureDataJpa 自動(dòng)配置JPA
  • @AutoConfigureTestEntityManager 自動(dòng)配置TestEntityManager
  • @AutoConfigureRestDocs 自動(dòng)配置Rest Docs
  • @AutoConfigureMockRestServiceServer 自動(dòng)配置 MockRestServiceServer
  • @AutoConfigureWebClient 自動(dòng)配置 WebClient
  • @AutoConfigureWebFlux 自動(dòng)配置 WebFlux
  • @AutoConfigureWebTestClient 自動(dòng)配置 WebTestClient
  • @AutoConfigureMockMvc 自動(dòng)配置 MockMvc
  • @AutoConfigureWebMvc 自動(dòng)配置WebMvc
  • @AutoConfigureDataNeo4j 自動(dòng)配置 Neo4j
  • @AutoConfigureDataRedis 自動(dòng)配置 Redis
  • @AutoConfigureJooq 自動(dòng)配置 Jooq
  • @AutoConfigureTestDatabase 自動(dòng)配置Test Database,可以使用內(nèi)存數(shù)據(jù)庫

這些注解可以搭配@*Test使用,用于開啟在@*Test中未自動(dòng)配置的功能。

例如@SpringBootTest和@AutoConfigureMockMvc組合后,就可以注入org.springframework.test.web.servlet.MockMvc。

自動(dòng)配置類型有兩種方式:

  • 功能測(cè)試(即使用@SpringBootTest)時(shí)顯示添加。
  • 一般在切片測(cè)試中被隱式使用,例如@WebMvcTest注解時(shí),隱式添加了@AutoConfigureCache、@AutoConfigureWebMvc、@AutoConfigureMockMvc

(4) 啟動(dòng)測(cè)試類型的注解(@*Test)

所有的@*Test注解都被@BootstrapWith注解,它們可以啟動(dòng)ApplicationContext,是測(cè)試的入口,所有的測(cè)試類必須聲明一個(gè)@*Test注解。

  • @SpringBootTest 自動(dòng)偵測(cè)并加載@SpringBootApplication或@SpringBootConfiguration中的配置,默認(rèn)web環(huán)境為MOCK,不監(jiān)聽任務(wù)端口
  • @DataRedisTest 測(cè)試對(duì)Redis操作,自動(dòng)掃描被@RedisHash描述的類,并配置Spring Data Redis的庫
  • @DataJpaTest 測(cè)試基于JPA的數(shù)據(jù)庫操作,同時(shí)提供了TestEntityManager替代JPA的EntityManager
  • @DataJdbcTest 測(cè)試基于Spring Data JDBC的數(shù)據(jù)庫操作
  • @JsonTest 測(cè)試JSON的序列化和反序列化
  • @WebMvcTest 測(cè)試Spring MVC中的controllers
  • @WebFluxTest 測(cè)試Spring WebFlux中的controllers
  • @RestClientTest 測(cè)試對(duì)REST客戶端的操作
  • @DataLdapTest 測(cè)試對(duì)LDAP的操作
  • @DataMongoTest 測(cè)試對(duì)MongoDB的操作
  • @DataNeo4jTest 測(cè)試對(duì)Neo4j的操作

除了@SpringBootTest之外的注解都是用來進(jìn)行切面測(cè)試的,他們會(huì)默認(rèn)導(dǎo)入一些自動(dòng)配。

一般情況下,推薦使用@SpringBootTest而非其它切片測(cè)試的注解,簡(jiǎn)單有效。若某次改動(dòng)僅涉及特定切片,可以考慮使用切片測(cè)試。

@SpringBootTest是這些注解中最常用的一個(gè),其中包含的配置項(xiàng)如下:

  • value 指定配置屬性
  • properties 指定配置屬性,和value意義相同
  • classes 指定配置類,等同于@ContextConfiguration中的class,若沒有顯示指定,將查找嵌套的@Configuration類,然后返回到SpringBootConfiguration搜索配置
  • webEnvironment 指定web環(huán)境,可選值有:MOCK、RANDOM_PORT、DEFINED_PORT、NONE

webEnvironment詳細(xì)說明:

  • MOCK 此值為默認(rèn)值,該類型提供一個(gè)mock環(huán)境,此時(shí)內(nèi)嵌的服務(wù)(servlet容器)并沒有真正啟動(dòng),也不會(huì)監(jiān)聽web端口。
  • RANDOM_PORT 啟動(dòng)一個(gè)真實(shí)的web服務(wù),監(jiān)聽一個(gè)隨機(jī)端口。
  • DEFINED_PORT 啟動(dòng)一個(gè)真實(shí)的web服務(wù),監(jiān)聽一個(gè)定義好的端口(從配置中讀?。?/li>
  • NONE 啟動(dòng)一個(gè)非web的ApplicationContext,既不提供mock環(huán)境,也不提供真是的web服務(wù)。

2. 相互之間的搭配組合

package sample.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import sample.test.domain.VehicleIdentificationNumber;
import sample.test.service.VehicleDetails;
import sample.test.service.VehicleDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import static org.mockito.BDDMockito.given;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureTestDatabase
public class SampleTestApplicationWebIntegrationTests {
    private static final VehicleIdentificationNumber VIN = new VehicleIdentificationNumber(
            "01234567890123456");
    @Autowired
    private TestRestTemplate restTemplate;
    @MockBean
    private VehicleDetailsService vehicleDetailsService;
    @Before
    public void setup() {
        given(this.vehicleDetailsService.getVehicleDetails(VIN))
                .willReturn(new VehicleDetails("Honda", "Civic"));
    }
    @Test
    public void test() {
        this.restTemplate.getForEntity("/{username}/vehicle", String.class, "sframework");
    }
}
  • @RunWith(SpringRunner.class)是JUnit的注解,作用是關(guān)聯(lián)Spring Boot Test,使運(yùn)行JUnit時(shí)同時(shí)啟動(dòng)Spring
  • @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 作用是啟動(dòng)Spring的ApplicationContext,參數(shù)webEnvironment指定了運(yùn)行的web環(huán)境
  • @AutoConfigureTestDatabase 作用是啟動(dòng)一個(gè)內(nèi)存數(shù)據(jù)庫,不使用真實(shí)的數(shù)據(jù)庫

其中@RunWith和@Test必須存在,@AutoConfigure可以同時(shí)配置任意多個(gè),而配置類型的注解可以在需要時(shí)添加。

3. 相似注解的區(qū)別于聯(lián)系

(1) @TestComment vs @Comment

  • @TestComponent是另一種@Component,在語義上用來指定某個(gè)Bean是專門用于測(cè)試的

使用@SpringBootApplication服務(wù)時(shí),@TestComponent會(huì)被自動(dòng)排除

(2) @TestConfiguration vs @Configuration

  • @TestConfiguration是Spring Boot Boot Test提供的,@Configuration是Spring Framework提供的。
  • @TestConfiguration實(shí)際上是也是一種@TestComponent,只是這個(gè)@TestComponent專門用來做配置用。
  • @TestConfiguration和@Configuration不同,它不會(huì)阻止@SpringBootTest的查找機(jī)制,相當(dāng)于是對(duì)既有配置的補(bǔ)充或覆蓋。

(3) @SpringBootTest vs @WebMvcTest(或@*Test)

都可以啟動(dòng)Spring的ApplicationContext

  • @SpringBootTest自動(dòng)偵測(cè)并加載@SpringBootApplication或@SpringBootConfiguration中的配置,@WebMvcTest不偵測(cè)配置,只是默認(rèn)加載一些自動(dòng)配置。
  • @SpringBootTest測(cè)試范圍一般比@WebMvcTest大。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論