Springboot 中使用單元測試

參考自官網:Spring1.59的文檔html

1、測試 Testing

Springboot 測試支持由兩個模塊提供;java

spring-boot-test 包含核心項目,而spring-boot-test-autoconfigure支持測試的自動配置。web

大多數開發人員只使用spring-boot-starter-test就能夠。它能夠導入Spring Boot測試模塊以及JUnit,AssertJ,Hamcrest和其餘一些有用的庫。算法

2、 測試範圍依賴關係 Test scope dependencies

當你pom中引入了 spring-boot-starter-testspring

他將會自動引入下面的library庫json

若是這些並不能知足你,你能夠本身附加測試依賴項。api

3、測試springboot Testing Spring Boot applications

Spring Boot提供了一個@SpringBootTest註解,當您須要Spring Boot功能時,它能夠用做標準spring-test @ContextConfiguration註釋的替代方法。註解的工做原理是經過SpringApplication在測試中建立ApplicationContext。springboot

因此Spring1.4以上的版本通常狀況下是這樣的:服務器

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootStarterTests {

普通Spring項目中的測試通常狀況下是這樣的:mvc

@RunWith(SpringRunner.class)
@ContextConfiguration(locations={"classpath:spring-servlet.xml", "classpath:spring-dao-test.xml", "classpath:spring-service-test.xml"})
public class MemberTest {

您可使用@SpringBootTestwebEnvironment屬性來進一步優化測試的運行方式:

  • MOCK : 加載一個WebApplicationContext並提供一個模擬servlet環境。嵌入式servlet容器在使用此註釋時不會啓動。若是servlet API不在你的類路徑上,這個模式將透明地回退到建立一個常規的非web應用程序上下文。能夠與@AutoConfigureMockMvc結合使用,用於基於MockMvc的應用程序測試。
  • RANDOM_PORT : 加載一個EmbeddedWebApplicationContext並提供一個真正的servlet環境。嵌入式servlet容器啓動並在隨機端口上偵聽。
  • DEFINED_PORT : 加載一個EmbeddedWebApplicationContext並提供一個真正的servlet環境。嵌入式servlet容器啓動並監聽定義的端口(即從application.properties或默認端口8080)。
  • NONE : 使用SpringApplication加載ApplicationContext,但不提供任何servlet環境(模擬或其餘)。

示例:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MemberTest {

 

注意:

一、若是你的測試是@Transactional,默認狀況下它會在每一個測試方法結束時回滾事務。 可是,因爲使用RANDOM_PORT或DEFINED_PORT這種安排隱式地提供了一個真正的servlet環境,因此HTTP客戶端和服務器將在不一樣的線程中運行,從而分離事務。 在這種狀況下,在服務器上啓動的任何事務都不會回滾。

二、除了@SpringBootTest以外,還提供了許多其餘註釋來測試應用程序的更具體的切片。 詳情請參閱下文。

三、不要忘記還要在測試中添加@RunWith(SpringRunner.class),不然註釋將被忽略。

 

4、檢測測試配置

若是您熟悉Spring測試框架,則可使用@ContextConfiguration(classes = ...)來指定要加載哪一個Spring @Configuration。 或者,您可能常常在測試中使用嵌套的@Configuration類。

在測試Spring Boot應用程序時,這一般不是必需的。 Spring Boot的@ *測試註解自動搜索您的主要配置,只要您沒有明肯定義一個。

搜索算法從包含測試的包開始工做,直到找到@SpringBootApplication或@SpringBootConfiguration註解。 只要你以合理的方式構建你的代碼,你的主要配置一般是能夠找到的。

 

5、使用隨機端口 Working with random ports

若是您須要啓動完整的運行服務器進行測試,咱們建議您使用隨機端口。 若是您使用@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT),則每次運行測試時都會隨機選取一個可用端口。

@LocalServerPort註解能夠用來注入用於測試的實際端口。 爲了方便起見,須要對已啓動的服務器進行REST調用的測試還能夠@Autowire一個TestRestTemplate,它將解析到正在運行的服務器的相關連接。

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class RandomPortExampleTests {

	@Autowired
	private TestRestTemplate restTemplate;

	@Test
	public void exampleTest() {
		String body = this.restTemplate.getForObject("/", String.class);
		assertThat(body).isEqualTo("Hello World");
	}

}

 

後面還有一些測試Springmvc url , json 序列化的。因爲目前沒用到就不繼續寫了。改天來填坑

相關文章
相關標籤/搜索