如何确定 JMock 模拟对象正在模拟哪个类?
假设我有一个像这样的模拟设置:
JUnit4Mockery context = new JUnit4Mockery();
MyInterface mock = context.mock(MyInterface.class);
后来我想检查我的模拟对象以找出它正在模拟哪个类:
Class mockedClass = mock.??? //would return MyInterface.class
我在 JMock (2.5.1) javadoc 中没有看到任何关于如何执行此操作的明显信息 - mock
方法的签名是
<T> T mock (Class<T> typeToMock)
在以前的版本中(我查看了 1.2.0),您将直接创建一个 Mock
对象,其方法之一是
Class getMockedType()
What I'm试图实现的目标是一个单位在我的单元测试中使用 DI 的测试框架。 (我使用的是 Guice 3.0。)在测试中使用 DI 是对我正在使用的应用程序服务器/平台的限制 - 我正在测试的对象是具有自己的 Injector,这就是我要填充的内容。
我不想在每个测试中都创建 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);
}
}
}
唯一缺少的是答案(如果有)是一)这个问题。
对已接受答案的响应
这是我最终为此用例创建的内容:
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);
}
}
}
}
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
<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
Class getMockedType()
What I'm trying to achieve is a unit testing framework for using DI inside my unit tests. (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.
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):
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);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将打印:
可能有更强大的方法来做到这一点,但它至少可以完成这个特定的 JMock 版本的工作。
Will print:
There are possibly more robust ways to do it, but it gets the job done for this particular JMock version at least.
我不确定我是否理解您这样做的动机。 JMock 通常用于对一个对象或一小群对象进行单元测试,这意味着测试中的所有内容都是新的。当练习看起来更像真实组件的东西时,DI 通常在集成或验收测试级别开始。
您想通过在测试中使用 DI 来展示什么?
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. DI usually kicks in at the level of integration or acceptance testing, when exercising something that looks more like a real component.
What are you trying to show by using DI within the tests?