In computer programming, unit testing is a software testing method, which is used to test a single unit of source code, a collection of one or more computer program modules, and related control data, use process and operation process to determine whether they are suitable for use. In short, when we are doing unit tests, we only test one code unit, only one method at a time, excluding all other components that interact with the component under test.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> //Add Maven dependency
1, Unit test of Controller layer
Return entity class: import com.google.gson.JsonElement; import lombok.Data; import lombok.extern.slf4j.Slf4j; import java.util.List; @Slf4j @Data public class PhpResponse { private String error; private String warning; private String status; private String total; private String total_found; private String time; private List<?> words; private List<JsonElement> matches; }
Unit test class parent class: import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringRunner.class) @SpringBootTest(classes = SearchProxyApplicationTest.class) @Slf4j public class SearchProxyApplicationTest { public MockMvc mockMvc; public String url; public String json; public Gson gson = new Gson(); @Autowired private WebApplicationContext webApplicationContext; @Before public void init() { log.info("*****Start unit test******"); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @After public void after() { log.info("*****End of test*****"); } }
Interface test class: import com.google.gson.Gson; import com.haodf.search.proxy.SearchProxyApplication; import com.haodf.search.proxy.controller.sphinx.DoctorinfoSearchdFromSphinxController; import com.haodf.search.proxy.fromsphinx.SearchProxyApplicationTest; import com.haodf.search.proxy.model.PhpResponse; import lombok.extern.slf4j.Slf4j; import org.junit.After; 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.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; 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.ResultActions; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @Slf4j @RunWith(SpringRunner.class) @SpringBootTest(classes = SearchProxyApplication.class) @WebAppConfiguration public class DoctorInfoSearchdTest extends SearchProxyApplicationTest { @Test public void searchDoctorInfo() throws Exception{ url = "/doctorinfo/searchDoctorInfo"; json = "{\"name\":\"\",\"doctorNameRule\":null,\"username\":\"zh19670204\",\"city\":{\"\\'qita\\'\":\"\"},\"facultyname\":\"\",\"isMatchPrecisely\":\"0\",\"filters\":{\"caseopened\":[1]},\"offset\":0,\"limit\":20,\"sortBy\":\"casemarkrank desc, activity desc, adminlevel desc,activityTime desc\"}"; ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post(url) .param("requestData", json)); MvcResult mvcResult = resultActions.andReturn(); String result = mvcResult.getResponse().getContentAsString(); log.info("result:{}", result); PhpResponse response = gson.fromJson(result, PhpResponse.class); Assert.assertTrue(response.getMatches().size() > 0); } @Test public void searchDoctorByCondition() throws Exception{ url = "/doctorinfo/searchDoctorByCondition"; json = "{\"filters\":{\"isOpenRecipe\":[1],\"facultyid\":[\"6002000\"]},\"limit\":10,\"offset\":0}\n"; ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post(url) .param("requestData", json)); MvcResult mvcResult = resultActions.andReturn(); String result = mvcResult.getResponse().getContentAsString(); log.info("result:{}", result); PhpResponse response = gson.fromJson(result, PhpResponse.class); Assert.assertTrue(response.getMatches().size() > 0); } }