我有一个GET API,它可以在控制器类中接受不同的参数。根据boolean参数,API应该返回不同的结果。我该如何编写相同的JUnit测试用例?
@CrossOrigin
@RequestMapping(value = "/api/brand/multi-get", method = RequestMethod.GET, consumes =
MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces =
MediaType.APPLICATION_JSON_VALUE)
public ResponseDTO listBrands(@RequestParam(required = false) Integer start,
@RequestParam(required = false) Integer limit,
@RequestParam(required = false) Boolean show_rejected) {
ResponseDTO responseDTO = new ResponseDTO();
System.out.println("------------------");
try {
if (start == null) {
start = 0;
}
if (limit == null) {
limit = 50;
} else if (1 <= limit && limit <= 250) {
if (show_rejected) {
List<Brand> brands = brandService.findAllBrands(start, limit);
if (brands != null && !brands.isEmpty()) {
responseDTO.setData(brands);
responseDTO.setCode(ErrorCode.error_0);
responseDTO.setMessage("Brand list");
return responseDTO;
} else {
responseDTO.setData(new ArrayList<>());
responseDTO.setCode(ErrorCode.error_0);
responseDTO.setMessage("Brand list");
return responseDTO;
}
} else {
Page<Brand> brands = brandService.findAllActiveBrands(start, limit);
if (brands != null && !brands.isEmpty()) {
responseDTO.setData(brands);
responseDTO.setCode(ErrorCode.error_0);
responseDTO.setMessage("Brand list");
return responseDTO;
} else {
responseDTO.setData(new ArrayList<>());
responseDTO.setCode(ErrorCode.error_0);
responseDTO.setMessage("Brand list");
return responseDTO;
}
}
} else {
responseDTO.setCode(ErrorCode.error_1104);
responseDTO.setMessage(ErrorCode.error_msg1104);
return responseDTO;
}
} catch (Exception e) {
e.printStackTrace();
}
return responseDTO;
}
答案 0 :(得分:0)
如果您正在使用spring框架并模拟测试,则spring-test库包含MockMvc
和MockMvcRequestBuilders
类,可用于测试任何API。您使用GET API,可以使用
public static MockHttpServletRequestBuilder get(String urlTemplate, Object... uriVars)
中的MockMvcRequestBuilders
方法
然后,您可以在public ResultActions perform(RequestBuilder requestBuilder)
中使用MockMvc
方法来启动服务。
答案 1 :(得分:0)
调用GET API并断言以获取响应详细信息。
@Test
public void postSimpleBody() throws Exception {
Response response = server.newRequest("/path").request().buildPost(Entity.text("42")).invoke();
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}
答案 2 :(得分:0)
所有你必须首先要与以下注释注释您的测试类:
@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
如果您具有身份验证,请添加:
@WithMockUser
自动装配的mockMvc执行请求:
@Autowired
private MockMvc mockMvc;
使用@MockBean
嘲笑服务层:
@MockBean
private BrandService brandService;
和注释与测试中的@Test
注释:
@Test
public void test_listBrands_when_something_do_something() {
}
然后使用Mockito在测试所涵盖的服务中模拟该方法:
List<Brand> mockBrands = new LinkedList<Brand>();
mockBrands.add(new Brand(/*...*/));
Mockito.when(brandService.findAllBrands(0, 25)).thenReturn(mockBrands);
使用类创建requestBuilder org.springframework.test.web.servlet.request.MockMvcRequestBuilders
:
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/api/brand/multi-get?start={0}&limit={1}&show_rejected={2}", 0, 25, true)
.accept(MediaType.APPLICATION_JSON);
执行请求和断言使用类结果org.springframework.test.web.servlet.result.MockMvcResultMatchers
:
mockMvc.perform(requestBuilder)
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON));
作为最后的尖端,添加此2个静态导入因此您的代码会更短:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
然后,可以做get()
,post()
,status()
,content()
,等
这是完整的示例:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class MyApiControllerTests {
@Autowired
private MockMvc mockMvc;
@MockBean
private BrandService brandService;
@Test
public void test_listBrands_when_something_do_something() {
//#region Mocks
List<Brand> mockBrands = new LinkedList<Brand>();
mockBrands.add(new Brand(/*...*/));
Mockito.when(brandService.findAllBrands(0, 25)).thenReturn(mockBrands);
//#endregion
MockHttpServletRequestBuilder requestBuilder = get("/api/brand/multi-get?start={0}&limit={1}&show_rejected={2}", 0, 25, true).accept(MediaType.APPLICATION_JSON);
mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
}