简体   繁体   English

如何确定JMock模拟对象在模拟哪个类?

[英]How can I determine which class a JMock mock object is mocking?

Say I've got a mock setup like this: 说我有一个这样的模拟设置:

JUnit4Mockery context = new JUnit4Mockery();
MyInterface mock = context.mock(MyInterface.class);

And later I want to examine my mock object to find out what class it's mocking: 然后,我想检查我的模拟对象,以找出其模拟的类:

Class mockedClass = mock.??? //would return MyInterface.class

I didn't see anything obvious in the JMock (2.5.1) javadocs about how to do this - the signature for the mock method is 我没有在JMock(2.5.1)javadocs中看到有关如何执行此操作的任何明显内容- mock方法的签名是

<T> T mock (Class<T> typeToMock)

In previous versions (I looked at 1.2.0) you would create a Mock object directly, and one of its methods was 在以前的版本(我看过1.2.0)中,您将直接创建Mock对象,其方法之一是

Class getMockedType()

What I'm trying to achieve is a unit testing framework for using DI inside my unit tests. 我想要实现的是一个在单元测试中使用DI的单元测试框架。 (I'm using Guice 3.0.) Having DI in the tests is a restriction of the application server/platform I'm working with - the objects I'm testing are subclasses of a multiton that has its own Injector , which is what I'm trying to populate. (我正在使用Guice 3.0。)在测试中使用DI是我正在使用的应用程序服务器/平台的限制-我正在测试的对象是具有自己的Injector的multiton的子类,这就是我所要解决的问题。正在尝试填充。

I'd prefer not to have to create an anonymous instance of AbstractModule in every test, so I'm trying to build something like this (this seems like it would have worked in 1.2): 我希望不必在每次测试中都创建一个AbstractModule的匿名实例,因此我正在尝试构建类似的东西(似乎在1.2中就可以了):

public class MockModule extends AbstractModule {
    private Iterable<Mock> mocks;

    public MockModule(Iterable<Mock> mocks) {
        this.mocks = mocks;
    }

    protected void configure() {
        for (Mock mock : mocks) {
            bind(mock.getMockedType()).toInstance(mock);
        }
    }
}

The only thing missing is the answer (if there is one) to this question. 唯一缺少的是这个问题的答案(如果有的话)。

RESPONSE TO ACCEPTED ANSWER 对已接受答案的回应

Here is what I ended up creating for this use case: 这是我最终为该用例创建的内容:

import java.lang.reflect.Proxy;
import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class MockModule extends AbstractModule {
    private final Iterable mocks;

    public MockModule(Object mock) {
        mocks = Lists.newArrayList(mock);
    }

    public MockModule(Iterable mocks) {
        this.mocks = mocks;
    }

    protected void configure() {
        for (Object mock : mocks) {
            Class superclass = mock.getClass().getSuperclass();
            if (superclass != Object.class && superclass != Proxy.class) {
                bind(superclass).toInstance(mock);
                continue;
            }
            Class[] interfaces = mock.getClass().getInterfaces();
            if (interfaces.length > 0) {
                bind(interfaces[0]).toInstance(mock);
            }
        }
    }
}
Class mockedClass = mock.getClass().getInterfaces()[0];
System.out.println("Class is " + mockedClass.getCanonicalName());

Will print: 将打印:

Interface is MyInterface

There are possibly more robust ways to do it, but it gets the job done for this particular JMock version at least. 可能有更健壮的方法可以执行此操作,但至少可以完成此特定JMock版本的工作。

I'm not sure I understand your motivations for doing this. 我不确定我是否理解您这样做的动机。 JMock is conventionally used to unit test an object, or small cluster of objects, which means that everything is new'ed up in the test. 传统上,JMock用于对一个对象或一小组小型对象进行单元测试,这意味着测试中的所有内容都是新的。 DI usually kicks in at the level of integration or acceptance testing, when exercising something that looks more like a real component. 当执行看起来更像是真实组件的东西时,DI通常会进入集成或验收测试的级别。

What are you trying to show by using DI within the tests? 您想通过在测试中使用DI来显示什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM