Mockito在调用EJB时始终返回null作为结果

huangapple go评论74阅读模式
英文:

Mockito always returns null as a result of calling an EJB

问题

我尝试调用该类的第二个方法,但始终返回 null。请注意,尽管它返回 new User(),但在测试类中我始终得到 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();
    }
}

测试类:

@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"); // <-- 该方法始终返回 null

        assertThat(user).isEqualTo(expectedUser); // <-- 测试失败,因为 expectedUser != null

    }
}

另外,请注意 System.out.println 从未打印出。如何修复以实际调用 dao.readByUserid()

英文:

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(&quot;In DAO 2&quot;);
        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(&quot;David&quot;);
		expectedUser.setLastName(&quot;Gahan&quot;);
		expectedUser.setUserid(&quot;user1&quot;);
		
		User user = dao2.readByUserid(&quot;user1&quot;); // &lt;-- this method always returns null
		
		assertThat(user).isEqualTo(expectedUser); // &lt;-- 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() ?

答案1

得分: 1

如果您需要测试某个类的方法,并且在该方法内部调用了同一类的另一个方法,而您想要模拟该方法,那么您需要使用 @Spy

@RunWith(MockitoJUnitRunner.class)
public class UserDAO2Test {

	@InjectMocks
	@Spy
	private UserDAO2 dao;

	@Test
	public void testBean() {

		Mockito.doReturn(null).when(dao).getConnFromPool(1);

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

		User user = dao.readByUserid("user1");

		assertThat(user).isEqualTo(expectedUser);
	}
}

请注意,我稍微修改了模拟 getConnFromPool 的那一行,因为在使用这种技术时需要这样做。

有关使用 Spy 的详细信息,请参阅文档

英文:

If you need to test the method of some class, and inside of it the other method of the same class is called which you want to mock, then you need to use @Spy:

@RunWith(MockitoJUnitRunner.class)
public class UserDAO2Test {

	@InjectMocks
	@Spy
	private UserDAO2 dao;

	@Test
	public void testBean() {

		Mockito.doReturn(null).when(dao).getConnFromPool(1);

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

		User user = dao.readByUserid(&quot;user1&quot;);

		assertThat(user).isEqualTo(expectedUser);
	}
}

Note that I slightly modified the line with mocking getConnFromPool because it's required when you use that technique.

See docs for spying.

huangapple
  • 本文由 发表于 2020年4月11日 04:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61148341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定