如何使用 jMock 的 ClassImposteriser 进行 Android 单元测试?
在我的单元测试中,我尝试了以下操作:
import org.jmock.Mockery;
import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser;
public class MyActivityTest extends ActivityUnitTestCase<MyActivity> {
private Mockery context = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
...
}
我的预期用途是模拟我的项目的应用程序子类。但是,当我运行测试时,我收到 java.lang.ExceptionInInitializerError。我可以不使用 ClassImposteriser 扩展来运行 Android 单元测试吗?
In my unit test, I've tried the following:
import org.jmock.Mockery;
import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser;
public class MyActivityTest extends ActivityUnitTestCase<MyActivity> {
private Mockery context = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
...
}
My intended use is to mock my project's Application subclass. However, when I run my tests, I get an java.lang.ExceptionInInitializerError. Can I not use the ClassImposteriser extension for running Android unit tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我几个月前广泛研究过的事情。不幸的是,dalvik VM 目前不支持模拟具体类所需的字节码操作。
因此您将无法使用任何模拟库来模拟类。您必须为要在 Android 测试中模拟的每个类提取一个接口,然后模拟该接口。
有关 davlik 限制的进一步阅读:
This is something I looked into extensively several months ago. Unfortunately the dalvik VM currently does not support the bytecode manipulations that are required to mock concrete classes.
So you won't be able to use any mocking library to mock a class. You'll have to extract an interface for each class you want to mock in your android tests and mock the interface instead.
Some further reading about the davlik limitations: