How to use EasyMock expect

会有一股神秘感。 提交于 2020-01-13 11:23:36

问题


The expect doesn't seem to work for me:

    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());
    }

}

It's supposed to be 5, but I'm getting 0 as if the expect wouldn't be there at all...


回答1:


You need to call the replay method on your mock object, so that it starts returning what you configured it to.




回答2:


Okay, after analysing I finally got it to work by adding EasyMock.replay(userBookLimiter);

So the setup method looks like this:

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


来源:https://stackoverflow.com/questions/19052445/how-to-use-easymock-expect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!