Mockito单元测试的预期结果与实际结果相同,但测试失败

时间:2020-11-02 21:35:51

标签: java unit-testing mockito verify

我有以下测试方法,用于验证是否已调用服务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)

但据我所知它们是完全一样的!有人遇到过这个吗?

0 个答案:

没有答案