I have restful services and I want to unit test them without connecting to database, therefore I have written this piece of code:
@Before
public void setup() throws Exception {
this.mockMvc = webAppContextSetup(webApplicationContext).build();
adminDao = mock(AdminDaoImpl.class);
adminService = new AdminServiceImpl(adminDao);
}
@Test
public void getUserList_test() throws Exception {
User user = getTestUser();
List<User> expected = spy(Lists.newArrayList(user));
when(adminDao.selectUserList()).thenReturn(expected);
mockMvc.perform(get("/admin/user"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$", hasSize(1)))
;
}
The service gets called but my problem is this line of code
when(adminDao.selectUserList()).thenReturn(expected);
is not working, I mean it really calls the adminDao.select method and therefore gets the result from database. which I don't want. Do you have any idea how can I mock the method call?