我有一个正在测试的Spring Boot项目,并且在控制器中有此get方法:
@GetMapping("/updatecrime/{id}")
public String updateCrime(@PathVariable Long id) {
Crime oldCrime = new Crime(id);
this.service.addCrime(id, oldCrime);
Crime newCrime = new Crime(oldCrime.getId(), oldCrime.getZipCode(), oldCrime.getTotPopulation(),
oldCrime.getMedianAge(), oldCrime.getTotMales(), 10, oldCrime.getTotHouseholds(),
oldCrime.getAvgHouseholdSize());
return new Gson().toJson(this.service.updateCrime(id, oldCrime, newCrime));
}
我检查了我的测试的覆盖范围,并且该方法涵盖了最后一行的预期效果:
return new Gson().toJson(this.service.updateCrime(id, oldCrime, newCrime))
我需要涵盖哪种断言?这是我对该方法的测试:
this.objectMapper = new ObjectMapper();
try {
ResultActions resultActions = this.mvc
.perform(MockMvcRequestBuilders.get("/updatecrime/5"));
MvcResult result = resultActions.andReturn();
String contentString = result.getResponse().getContentAsString();
Crime crime = objectMapper.readValue(contentString, Crime.class);
assertTrue(crime.getTotFemales() == 10);
} catch (Exception e) {
e.printStackTrace();
}