Garmaine Staff asked 2 years ago

I'm trying to call the second method of this class, and always get null. Note that it returns new User() however in the test class I always get null:

@Stateless
public class UserDAO2 {

    public Connection getConnFromPool(int i) {
        return null;
    }

    public User readByUserid(String s) {
        System.out.println("In DAO 2");
        Connection c = getConnFromPool(1);
        return new User();
    }
}

And the test class:

@RunWith(MockitoJUnitRunner.class)
public class UserBeanUnitTest {

    @InjectMocks
    private UserDAO2 dao2;

    @Before
    public void setup() {
        dao2 = Mockito.mock(UserDAO2.class);
        MockitoAnnotations.initMocks(this);
    }


    @Test
    public void testBean() {

        Mockito.when(dao2.getConnFromPool(1)).thenReturn(null);

        User expectedUser = new User();
        expectedUser.setSk(1);
        expectedUser.setFirstName("David");
        expectedUser.setLastName("Gahan");
        expectedUser.setUserid("user1");

        User user = dao2.readByUserid("user1"); // <-- this method always returns null

        assertThat(user).isEqualTo(expectedUser); // <-- test fails as expectedUser != null

    }

}

Also, note that System.out.println is never printed. How to fix this to actually make a call to dao.readByUserid() ?