this simple mockito is failing I do not know why? I suspect set up is not complete? When I replace userService by userDAO in the test method it passes, why this happen?
public class UserServiceTest {
private static IUserService userService;
private static IUserDAO userDAO;
private User GENERIC_EMPLOYEE_1;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
userService = new UserService();
userDAO = mock(IUserDAO.class);
}
@Before
public void setUp() throws Exception {
GENERIC_EMPLOYEE_1 = new User(1, "genericEmployee1", "genericPassword", Role.EMPLOYEE);
}
@Test
public void testGetByUsernamePassesWhenUsernameExists() {
//Optional<User> u=userDAO.create(GENERIC_EMPLOYEE_1);
//assertEquals(u.get().getUsername(),"genericEmployee1");
when(userDAO.getByUsername(anyString())).thenReturn(Optional.of(GENERIC_EMPLOYEE_1));
assertEquals(Optional.of(GENERIC_EMPLOYEE_1),
userService.getByUsername(GENERIC_EMPLOYEE_1.getUsername()));
verify(userDAO).getByUsername(GENERIC_EMPLOYEE_1.getUsername());
}
}