1. 程式人生 > >SpringBoot入門(22)--springBoot測試

SpringBoot入門(22)--springBoot測試

  1. 新增測試依賴

Spring-boot-starter-test

Scope:test

  1. 新增測試註解

@Runwith(SrpingRunner.class)

@SpringBootTest

  1. 在測試環境單獨裝配bean

加上測試配置類需註解@TestConfiguration

而使用@Configuration是無效的

  1. 環境Evn的測試

直接使用Enviroment介面可以自動裝配,獲取屬性值

優先取測試環境的配置檔案

  1. 給配置加配置項適用於測試類

方法一:使用註解@SpringBootTest的屬性properties=”app.version=1”

方法二:使用@before介面註解,在介面中新增配置項

  1. Mapper的測試

使用註解@MockBean注入Mapper例項,使用BDDMockito這個類模擬

拋異常預測:

  1. 控制器Controller測試

依賴類TestRestTemplate工具測試控制器

@SpringBooTest載入Spring整個容器

測試一個帶引數的Controller

Get方式的引數

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class BookRestTest {
	
	@Autowired
	private TestRestTemplate testRest;

	@Test
	public void testHome() {
		String body = testRest.getForObject("/book/home", String.class);
		Assert.assertEquals("book home", body);
	}
	
	@Test
	public void testShow() {
		String body = testRest.getForObject("/book/show?id=100", String.class);
		
		Assert.assertEquals("book100", body);
	}

}
  1. 控制器Controller測試,方式二,一下方法不會載入Spring容器

使用MockMvc介面模擬測試Controller

@RunWith(SpringRunner.class)
@WebMvcTest(controllers=BookRest.class)
public class BookRestTest2 {
	
	
	@Autowired
	private MockMvc mvc;

	@Test
	public void testHome() throws Exception {
//		String body = testRest.getForObject("/book/home", String.class);
//		Assert.assertEquals("book home", body);
		
		mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk());
	}
	
	@Test
	public void testShow() throws Exception {
		mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id", "100")).andExpect(MockMvcResultMatchers.status().isOk());
		
	}

}

總結:

多個引數測試:

該種方法不會載入Spring整個容器,只會載入測試物件;

@SpringBooTest載入Spring整個容器

3、兩者結合使用:使用註解@SpringBootTest載入整個容器bean,加上@AutoConfigureMockMvc註解開啟MockMvc物件

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class BookRestTest3 {
	
	@Autowired
	private TestRestTemplate testRest;
	
	@Autowired
	private MockMvc mvc;

	@Test
	public void testHome() throws Exception {
		String body = testRest.getForObject("/book/home", String.class);
		Assert.assertEquals("book home", body);
		
//		mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk());
	}
	
	@Test
	public void testShow() throws Exception {
		mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id", "100")).andExpect(MockMvcResultMatchers.status().isOk());
		
	}

}