Java 如何使用 EasyMock 期望

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19052445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:43:29  来源:igfitidea点击:

How to use EasyMock expect

javaeasymock

提问by Arturas M

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...

它应该是 5,但我得到 0,好像期望根本不存在......

采纳答案by Arturas M

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

好的,经过分析,我终于通过添加使其工作 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);
}

回答by kgautron

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

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