I am having a class :
GP_CategoryService.java Function -->
public JSONObject deleteCategory(GP_CategorySubcategoryBean bean) {
JSONObject data = new JSONObject();
DirectoryCategoryMaster oCategory = getCategoryMaster(bean);
if (oCategory.getDirCategoryId() != null) {
boolean isDeleted = delete(oCategory);
data.put(ConstantUtil.STATUS, ConstantUtil.SUCCESS);
data.put(ConstantUtil.DATA, "Category deleted successfully");
}
}
I have 2 inner function calls :
- getCategoryMaster(bean)
- delete(oCategory)
These are basically DAO calls, updating the Database directly. Now I want to mock these 2 fucntions alone such that whenever my test function is running, it should return true.
I have written my test function as below :
@Test
public void deleteCategoryTestDAOV() {
JSONObject expected = new JSONObject();
expected.put(ConstantUtil.STATUS, ConstantUtil.SUCCESS);
expected.put(ConstantUtil.DATA, "Category deleted successfully");
bean.setCategoryId(1);
bean.setCategoryName("Test");
DirectoryCategoryMaster master=new DirectoryCategoryMaster();
master.setDirCategoryId(1);
GP_CategoryService mock = spy(new GP_CategoryService());
when(mock.delete(master)).thenReturn(true);
when(mock.getCategoryMaster(bean)).thenReturn(master);
JSONObject actual=new JSONObject();
actual=mock.deleteCategory(bean);
assertEquals(expected.toJSONString(), actual.toJSONString());
}
But when I am running the test class, its executing the actual fucntion, mock is not working. Can anhyone please help me to resolve this issue?
Thanks in advance!