控制器:
@ApiOperation(value = " update record", response = APIResponse.class)
@ApiResponses(value = {@ApiResponse(code =200, message = "OK"),
@ApiResponses(value = {@ApiResponse(code =500, message = "Internal server error"),
@ApiResponses(value = {@ApiResponse(code =404, message = "NO_RECORD")})
@PutMapping(value = "/update/{id}")
@ResponseBody
public ResponseEntity<APIResponse> updateRecord(HttpServletRequest request, @RequestBody RecordDTO input, @PathVariable(value="id") int code){
APIResponse response = null;
try{
response = service.updateRecord(code, input);
}
catch(JSONException e){
log.error("Error Parsing JSON");
response = new APIResponse(HttpStatus.INTERNAL_SERVER_ERROR, ERROR_JSON_PARSING, ERROR);
}
return new ResponseEntity<>(response, HttpStatus.OK);
}
我的测试用例foor控制器:
@Test
public void update() throws Exception{
RecordDTO recordDto = new RecordDTO();
Object mapper = new ObjectMapper();
String value = mapper.writeValueAsString(StationDTO);
given(service.updateRecord(anyInt(), any(RecordDTO.class))).willThrow(JSONException.class);
mockMvc.perform(put(baseUrl + "/update/12")
.contentType(MediaType.APPLICATION_JSON).content(value))
.andExpect(status().isInternalservererror())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.status",Matchers.is("INTERNAL_SERVER_ERROR")))
.andExpect(jsonPath("$.message",Matchers.is("ERROR_JSON_PARSING")))
.andExpect(jsonPath("$.resposeStatus",Matchers.is("ERROR")));
APIResponse response = new APIResponse(HttpStatus.OK, SUCCESS, SUCCESS, null);
given(service.updateRecord(anyInt(), any(RecordDTO.class))).willReturn(response);
mockMvc.perform(put(baseUrl + "/update/12")
.contentType(MediaType.APPLICATION_JSON).content(value))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.status",Matchers.is("OK")))
.andExpect(jsonPath("$.message",Matchers.is("SUCCESS")))
.andExpect(jsonPath("$.resposeStatus",Matchers.is("SUCCESS")));
}
DTO:
public class RecordDTO{
private String id;
private String name;
private String number;
}
我得到java.lang断言错误,预期为500,但实际为200。我不知道这个测试用例出了什么问题。。有没有其他方法来编写测试用例?你也可以推荐任何平台,在那里我可以获得如何编写测试用例的知识,然后发表评论。谢谢你的帮助!
您的模拟service
似乎没有注入控制器。
替代解决方案(我假设您使用Spring Boot(:
- 禁用AutowireRequireInitializer。这将阻止在控制器中加载所有依赖项
- 在ControllerTest内部类中创建:
private static ServiceImplMock entends ServiceImpl
- 现在,在
ServiceMock
中重写updateRecord
方法来执行测试用例
@Override
public APIResponse updateRecord(int code, RecordDTO input) throws JSONException {
if(code == 12) throw new JSONException(...)
else your_business_logic
}
- 现在,将此
ServiceImplMock
添加到您的@SpringBootTest
中
@SpringBootTest(classes = {
Controller.class,
ControllerTest.ServiceImplMock.class,
...
})
@AutoConfigureMockMvc
@ContextConfiguration( initializers = {DisableAutowireRequireInitializer.class })
class ControllerTest {
- 现在,您的测试用例应该可以工作了(删除
given(...).willThrow(...);
,因为我们不再需要它了(
你能推荐任何平台吗?在那里我可以了解如何编写测试用例,然后评论
https://www.baeldung.com/junit
https://www.baeldung.com/spring-boot-testing
https://mkyong.com/spring-boot/spring-boot-junit-5-mockito/