4

期望似乎对我不起作用:

    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,就好像期望根本不存在一样......

4

2 回答 2

16

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

于 2013-09-27T13:51:43.010 回答
14

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

所以设置方法如下所示:

@Before
public void setUp() throws Exception {
    userBookLimiter = EasyMock.createMock(UserBookLimiter.class);
    EasyMock.expect(userBookLimiter.getMaxNumberOfBooksAllowed()).andReturn(5);
    EasyMock.replay(userBookLimiter);
}
于 2013-09-27T13:52:41.143 回答