如何解决Mockito单元测试的预期结果与实际结果相同,但测试失败
我有以下测试方法,用于验证是否已调用服务save
方法:
@测试 公共无效testPutIngredientList()引发异常 { IngredientList IngredientListC = generateTestIngredientListC();
Mockito.doNothing().when(ingredientListService).save(ingredientListC);
MvcResult mvcResult = mvc.perform(put("/api/ingredientLists")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(ingredientListC))
).andExpect(status().isOk()).andReturn();
String actualJsonResponse = mvcResult.getResponse().getContentAsString();
String expectedJsonResponse = objectMapper.writeValueAsString(ingredientListC);
assertEquals(expectedJsonResponse,actualJsonResponse);
verify(ingredientListService).save(ingredientListC);
}
现在assertEquals(expectedJsonResponse,actualJsonResponse);
通过了,但是当它进入verify
方法时,测试失败:
参数不同!需求:IngredientList bean.save( IngredientList [id = 0,成分= [VolumeMeasurement [id = 0,成分=成分[id = 0,名称=牛奶],单位= 450.0]], 重量测量[id = 0,成分=成分[id = 0,名称=马铃薯], unit = 20000.0],WeightMeasurement [id = 0,成分=成分[id = 0, name = Corn],unit = 356.12]]])); ->在com.rest.IngredientListRestControllerTest.testPutIngredientList(IngredientListRestControllerTest.java:192) 实际的调用具有不同的参数:IngredientList bean.save( IngredientList [id = 0,成分= [VolumeMeasurement [id = 0,成分=成分[id = 0,名称=牛奶],单位= 450.0]], 重量测量[id = 0,成分=成分[id = 0,名称=马铃薯], unit = 20000.0],WeightMeasurement [id = 0,成分=成分[id = 0, name = Corn],unit = 356.12]]])); ->在com.rest.IngredientListRestController.updateIngredientList(IngredientListRestController.java:51)
但据我所知它们是完全一样的!有人遇到过这个吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。