如何使用EasyMock期望

期望对我来说似乎不起作用:

package com.jjs.caf.library.client.drafting; import static org.junit.Assert.*; import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; import com.jjs.caf.library.client.CustomerManager; import com.jjs.caf.library.client.UserBookLimiter; public class DraftTest { UserBookLimiter userBookLimiter; int expected = 5; @Before public void setUp() throws Exception { userBookLimiter = EasyMock.createMock(UserBookLimiter.class); EasyMock.expect(userBookLimiter.getMaxNumberOfBooksAllowed()).andReturn(5); } @Test public final void test() { assertEquals(expected, userBookLimiter.getMaxNumberOfBooksAllowed()); } } 

它应该是5,但我得到0,好像期望不会在那里…

您需要在模拟对象上调用replay方法,以便它开始返回您配置它的内容。

好的,经过分析,我终于通过添加来实现它

  EasyMock.replay(userBookLimiter); 

所以setup方法如下所示:

 @Before public void setUp() throws Exception { userBookLimiter = EasyMock.createMock(UserBookLimiter.class); EasyMock.expect(userBookLimiter.getMaxNumberOfBooksAllowed()).andReturn(5); EasyMock.replay(userBookLimiter); }