如何使用EasyMock

时间:2013-09-27 13:47:53

标签: java 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好像期望不会在那里......

2 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:12)

好的,经过分析,我终于通过添加EasyMock.replay(userBookLimiter);

来实现它

所以设置方法如下所示:

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