SpringBoot入門系列:第七篇 Spring Boot的測試
Spring Boot的測試,和普通專案的測試類同,可以繼續使用我們熟悉的測試工具。當然,這裡的測試,主要還是後臺程式碼的測試。
主要需要注意的地方僅有三點:
1、依賴包的引入:pom.xml中僅依賴spring-boot-starter-test,它把相關的依賴全部引入。
2、在測試類上的註解,常用的註解有三個
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
這三個註解,只要注意第二個註解括號內的Application.class就行,把它替換為專案的啟動類即可。
我們前面spring-boot-sample-mysql工程的啟動類為SpringBootSampleMysqlApplication,那麼測試類上的註解就是
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringBootSampleMysqlApplication.class)
@WebAppConfiguration
3、測試類的檔案結構,保持src/test/java和src/main/java結構一直,即:包+資料夾。
如:com.example包service中類的測試,那麼在src/test/java也是建立com.example包,再在包下建立資料夾service.
注:由於我們測試的啟動類用的是專案的啟動類,所以Spring Boot專案的測試配置檔案任然用src/main/resources的。
下面貼兩個個測試類程式碼
1、服務類的
- package com.example.service;
- import java.util.List;
- 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.SpringApplicationConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import org.springframework.test.context.web.WebAppConfiguration;
- import com.example.Application;
- import com.example.domain.TestPOJO;
- import com.example.dto.HotelDto;
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = Application.class)
- @WebAppConfiguration
- publicclass TestServicesTest {
- @Autowired
- TestServices testServices;
- @Test
- publicvoid testShow() {
- String expectedResult="hello world!";
- String result=testServices.show();
- Assert.assertTrue("資料一致", expectedResult.equals(result));
- Assert.assertFalse("資料不一致", !expectedResult.equals(result));
- }
- @Test
- publicvoid testShowDao() {
- List<TestPOJO> testPOJOList=testServices.showDao(10);
- Assert.assertTrue("資料集不對", testPOJOList.size()==1);
- Assert.assertTrue("資料一致", testPOJOList.get(0).getName().equals("nice"));
- }
- @Test
- publicvoid testFindByCountry() {
- List<HotelDto> testPOJOList=testServices.findByCountry("US");
- Assert.assertTrue("資料集不對", testPOJOList.size()==1);
- Assert.assertTrue("資料一致", testPOJOList.get(0).getCityName().equals("San Francisco"));
- }
- }
- package com.example.controller;
- import java.util.List;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.SpringApplicationConfiguration;
- import org.springframework.http.MediaType;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import org.springframework.test.context.web.WebAppConfiguration;
- import org.springframework.test.web.servlet.MockMvc;
- import org.springframework.test.web.servlet.MvcResult;
- import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
- import org.springframework.test.web.servlet.setup.MockMvcBuilders;
- import org.springframework.web.context.WebApplicationContext;
- import com.example.Application;
- import com.example.domain.TestPOJO;
- import com.example.dto.HotelDto;
- import com.example.service.TestServices;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = Application.class)
- @WebAppConfiguration
- publicclass TestControllerTest {
- MockMvc mvc;
- @Autowired
- WebApplicationContext webApplicationConnect;
- @Autowired
- TestServices testServices;
- String expectedJson;
- @Before
- publicvoid setUp() throws JsonProcessingException {
- mvc = MockMvcBuilders.webAppContextSetup(webApplicationConnect).build();
- }
- @Test
- publicvoid testShow() throws Exception {
- String expectedResult = "hello world!";
- String uri = "/show";
- MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON))
- .andReturn();
- int status = mvcResult.getResponse().getStatus();
- String content = mvcResult.getResponse().getContentAsString();
- Assert.assertTrue("錯誤,正確的返回值為200", status == 200);
- Assert.assertFalse("錯誤,正確的返回值為200", status != 200);
- Assert.assertTrue("資料一致", expectedResult.equals(content));
- Assert.assertFalse("資料不一致", !expectedResult.equals(content));
- }
- protected String Obj2Json(Object obj) throws JsonProcessingException {
- ObjectMapper mapper=new ObjectMapper();
- return mapper.writeValueAsString(obj);
- }
- @Test
- publicvoid testShowDaoInt() throws Exception {
- List<TestPOJO> testPOJOList = testServices.showDao(10);
- expectedJson = Obj2Json(testPOJOList);
- String uri="/showDao?age=10";
- MvcResult mvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn();
- int status=mvcResult.getResponse().getStatus();
- String content=mvcResult.getResponse().getContentAsString();
- Assert.assertTrue("錯誤,正確的返回值為200", status == 200);
- Assert.assertFalse("錯誤,正確的返回值為200", status != 200);
- Assert.assertTrue("資料一致", expectedJson.equals(content));
- Assert.assertFalse("資料不一致", !expectedJson.equals(content));
- }
- @Test
- publicvoid testShowDaoString() throws Exception {
- List<HotelDto> hotelDtoList=testServices.findByCountry("US");
- expectedJson = Obj2Json(hotelDtoList);
- String uri="/country/US";
- MvcResult mvcResult=mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)).andReturn();
- int status=mvcResult.getResponse().getStatus();
- String content=mvcResult.getResponse().getContentAsString();
- Assert.assertTrue("錯誤,正確的返回值為200", status == 200);
- Assert.assertFalse("錯誤,正確的返回值為200", status != 200);
- Assert.assertTrue("資料一致", expectedJson.equals(content));
- Assert.assertFalse("資料不一致", !expectedJson.equals(content));
- }
- }