java 有没有办法使用 EasyMock 部分模拟对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10825284/
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-10-31 02:44:48  来源:igfitidea点击:

Is there a way to partially mock an object using EasyMock?

javatestingmockingeasymock

提问by allyourcode

E.g. let's say I have this class:

例如,假设我有这门课:

public class Foo Implements Fooable {
  public void a() {
    // does some stuff
    bar = b();
    // moar coadz
  }
  public Bar b() {
    // blah
  }
  // ...
}

And I want to test Foo.a. I want to mock Foo.b, because I'm testing that method separately. What I'm imagining is something like this:

我想测试Foo.a. 我想模拟Foo.b,因为我正在单独测试该方法。我的想象是这样的:

public class FooTest extends TestCase {
  public void testA() {
    Fooable foo = createPartialMock(
      Fooable.class,  // like with createMock
      Foo  // class where non-mocked method implementations live
    );

    // Foo's implementation of b is not used.
    // Rather, it is replaced with a dummy implementation
    // that records calls that are supposed to be made;
    // and returns a hard coded value (i.e. new Bar()).
    expect(foo.b()).andReturn(new Bar());

    // The rest is the same as with createMock:
    //   1. Stop recording expected calls.
    //   2. Run code under test.
    //   3. Verify that recorded calls were made.
    replay(foo);
    foo.a();
    verify(foo);
  }
}

I know I can write my own Foosubclass to do this sort of thing for me. But I don't want to do that if I don't have to, because it's tedious i.e. should be automated.

我知道我可以编写自己的Foo子类来为我做这种事情。但如果我不需要,我不想这样做,因为它很乏味,即应该是自动化的。

采纳答案by raddykrish

I guess you can do that using the EasyMock extensions library. You can find a simple example here in this Partial Mocking

我想你可以使用 EasyMock 扩展库来做到这一点。你可以在这个部分模拟中找到一个简单的例子

回答by Rajani Karuturi

In EasyMock 3.0+, you can create Partial mock using the mockbuilder

在 EasyMock 3.0+ 中,您可以使用mockbuilder创建 Partial mock

EasyMock.createMockBuilder(class).addMockedMethod("MethodName").createMock();

回答by user1504462

The OP appears(?) to be suggesting that subclassing is somehow more difficult or tedious than partial mocking. I suggest it's worth rethinking that.

OP 似乎(?)暗示子类化在某种程度上比部分模拟更困难或更乏味。我建议值得重新考虑。

For example, in the test class:

例如,在测试类中:

  Foo dummyFoo = new Foo() {
      @Override public Bar b() { return new Bar(); }
   };

does what the OP states, seems simpler, and less prone to other problems (forgetting to replay/verify/etc.), than using EasyMock.

与使用 EasyMock 相比,执行 OP 所述的操作,看起来更简单,并且不太容易出现其他问题(忘记重放/验证/等)。

回答by jhericks

I would find a way to upgrade to JUnit 4, and use classextensions. (Actually, I would use Mockito instead of EasyMock, but let's not rewrite your whole test suite.) If you can't, then you could always create your own spy thusly:

我会找到一种方法来升级到 JUnit 4,并使用类扩展。(实际上,我会使用 Mockito 而不是 EasyMock,但我们不要重写整个测试套件。)如果不能,那么您始终可以这样创建自己的间谍:

public class FooTest extends TestCase {
    public static class FooSpy extends Foo {
        private final Fooable mockFoo;

        FooSpy(Fooable mockFoo) {
            this.mockFoo = mockFoo;
        }

        public Bar b() {
            return mockFoo.b();
        }
    }

    public void testA() {
        Fooable mockFoo = createMock(Foo.class);
        Fooable fooSpy = new FooSpy(mockFoo);

        // Foo's implementation of b is not used.
        // Rather, it is replaced with a dummy implementation
        // that records calls that are supposed to be made;
        // and returns a hard coded value (i.e. new Bar()).
        expect(mockFoo.b()).andReturn(new Bar());

        // The rest is the same as with createMock:
        // 1. Stop recording expected calls.
        // 2. Run code under test.
        // 3. Verify that recorded calls were made.
        replay(mockFoo);
        foo.a();
        verify(foo);
    }

}