测试调用本机方法的代码
我有一个这样的类:
public final class Foo
{
public native int getBar();
public String toString()
{
return "Bar: " + getBar();
}
}
请注意,getBar() 是使用 JNI 实现的,并且该类是final。我想编写一个 junit 测试来测试 toString() 方法。为此,我需要模拟 getBar() 方法,然后运行原始的 toString() 方法来检查输出。
我的第一个想法是这肯定是不可能的,但后来我发现 PowerMock 它支持测试最终类和根据功能列表的本机方法。但到目前为止我还没有成功。我所做的最好的事情是模拟整个类,但随后测试测试了模拟的 toString() 方法,而不是真正的方法,这没有多大意义。
那么我如何使用 PowerMock 来测试上面的这个 toString() 方法呢?我更喜欢将 PowerMock 与 Mockito 一起使用,但如果这是不可能的,我使用 EasyMock 代替。
I have a class like this:
public final class Foo
{
public native int getBar();
public String toString()
{
return "Bar: " + getBar();
}
}
Please note that getBar() is implemented with JNI and that the class is final. I want to write a junit test to test the toString() method. For this I need to mock the getBar() method and then run the original toString() method to check the output.
My first thought was that this must be impossible but then I found PowerMock which supports testing final classes and native methods according to the feature list. But so far I had no success with it. The best thing I managed was mocking the complete class but then the test tested the mocked toString() method instead of the real one which doesn't make much sense.
So how can I use PowerMock to test this toString() method from above? I prefer using PowerMock with Mockito but if this is not possible I have no problem with using EasyMock instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到了。我的做法是正确的。我唯一错过的是告诉模拟对象在调用 toString() 时调用原始方法。所以它的工作原理是这样的:
Found it. The way I was doing it was correct. The only thing I missed was telling the mock object to call the original method when toString was called(). So it works like this:
或者将 JMockit 与动态部分模拟结合使用:
Or use JMockit with dynamic partial mocking:
或者使用策略模式:
单元测试时,注入一个模拟
IBarStrategy
实例,然后就可以测试类Foo
。Or use Strategy Pattern:
When unit test, inject a mock
IBarStrategy
instance, then you could test classFoo
.