1
希望瞭解如何在將新對象傳遞給控制器時編寫JUnit測試用例。如何爲新對象使用Junit?
以下是Spring MVC控制器中傳遞新PriceIncrease對象的一段代碼。
@RequestMapping(method=RequestMethod.GET, value="/priceincrease")
public String showPriceIncreasePage(Map<String, Object> map){
map.put("priceIncrease", new PriceIncrease());
logger.info("Returning Price Increase home page");
return priceIncreasePage;
}
單元測試我有,這當然是不正確的,因爲它會失敗。
@Test
public void testShowPriceIncreasePage(){
String viewName = inventoryController.showPriceIncreasePage(model);
/* Expecting to return the new object of PriceIncrease*/
assertEquals(new PriceIncrease(), model.get("priceIncrease"));
//assertEquals(InventoryController.priceIncreasePage, viewName);
}
我們應該在什麼時候使用以確保在Junit測試中返回相同的對象。我不確定這是否正確。
@Test
public void testShowPriceIncreasePage(){
String viewName = inventoryController.showPriceIncreasePage(model);
/* Expecting to return the new object of PriceIncrease*/
PriceIncrease priceIncrease = new PriceIncrease();
when(model.get("priceIncrease")).thenReturn(priceIncrease);
assertEqual(priceIncrease, model.get("priceIncrease"));
}
在此先感謝