如何使非模拟功能正常运行? [英] How to make the non-mocked function run in normal behavior?

查看:53
本文介绍了如何使非模拟功能正常运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是EasyMock的新手,我有这样的情况:

I am new in EasyMock, i have a scenario like this:

我为FolderUtils.ABC()创建一个模拟.但是,在FolderUtils.class内部,当我运行此unitTest时,有许多方法将与ABC()一起使用.我只希望ABC()返回模拟值,否则它们将按其正常行为运行.我该怎么办?

I create a mock for FolderUtils.ABC(). However, inside the FolderUtils.class, there are many methods that I will use with the ABC() when i run this unitTest. I only want the ABC() return the mock values, otherwise they will run as their normal behavior. How can I do that?

FolderUtils contantsUnderTest = EasyMock.createMock(FolderUtils.class);   
EasyMock.expect(contantsUnderTest.ABC(EasyMock.notNull(UserKey.class))).andReturn("123").anyTimes();

ReflectionTestUtils.setField(field, "folderUtils", contantsUnderTest);

field.execute();

推荐答案

部分模拟确实可以解决您的问题.这是一个示例:

Partial mocks can indeed solve your problem. Here is an example:

FolderUtils contantsUnderTest = createMockBuilder(FolderUtils.class)
    .addMockedMethod("ABC")
    .createMock();
expect(contantsUnderTest.ABC(notNull(UserKey.class))).andReturn("123").anyTimes();

replay(contantsUnderTest);

assertEquals("123", contantsUnderTest.ABC(new UserKey()));
assertEquals("1", contantsUnderTest.ANOTHER_CONSTANT());

verify(contantsUnderTest);

此实现:

public class FolderUtils {
    public String ABC(UserKey userKey) {
        return "1";
    }

    public String ANOTHER_CONSTANT() {
        return "1";
    }
}

这篇关于如何使非模拟功能正常运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆