如何使用 easymock 模拟类中的静态方法?
假设我有一个像这样的类:
public class StaticDude{
public static Object getGroove() {
// ... some complex logic which returns an object
};
}
如何使用简单的模拟来模拟静态方法调用? StaticDude.getGroove()
。
我正在使用 Easy Mock 3.0
Suppose I have a class like so:
public class StaticDude{
public static Object getGroove() {
// ... some complex logic which returns an object
};
}
How do I mock the static method call using easy mock? StaticDude.getGroove()
.
I am using easy mock 3.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不确定如何使用纯 EasyMock,但请考虑使用 EasyMock 的 PowerMock 扩展。
它有很多很酷的功能,可以满足您的需求 -
https://github.com/jayway/powermock/wiki/MockStatic
Not sure how to with pure EasyMock, but consider using the PowerMock extensions to EasyMock.
It has a lot of cool functions for doing just what you need -
https://github.com/jayway/powermock/wiki/MockStatic
Easymock 是一个“针对接口(以及通过类扩展的对象)”的测试框架,因此您可以模拟没有接口的类。考虑为静态类创建一个带有访问器的接口对象,然后模拟该访问器。
编辑:顺便说一句,我不建议做静态类。如果您正在进行 TDD,最好将所有内容都连接起来。
Easymock is a testing framework for "for interfaces (and objects through the class extension)" so you can mock a class without an interface. Consider creating an interfaced object with an accessor for your static class and then mock that acessor instead.
EDIT: Btw, I wouldn't recommend doing static classes. It is better to have everything interfaced if you are doing TDD.
以防万一 PowerMock 由于任何原因不可用:
您可以将静态调用移至某个方法,在测试类中的被测试类的实例化中重写此方法,在测试类中创建本地接口并在重写中使用其方法方法:
Just in Case PowerMock is unavailable for any reason:
You could move the static call to a method, override this method in the instantiation of the tested class in the test class, create a local interface in the test class and use its method in the overidden method:
一般来说,不使用某种访问器就不可能模拟静态方法,这似乎违背了使用静态方法的目的。这可能非常令人沮丧。
据我所知,有一个名为“TypeMock Isolator”的工具,它使用某种撒旦魔法来模拟静态方法,但该工具非常昂贵。
问题是,我不知道如何重写静态方法。您不能将其声明为虚拟的。您不能将其包含在界面中。
很抱歉成为一个消极的内莉。
Generally speaking, it is not possible to mock a static method without using some sort of accessor, which seems to defeat the purpose of using a static method. It can be quite frustrating.
There is one tool that I know of called "TypeMock Isolator" which uses some sort of Satanic Magic to mock static methods, but that tool is quite expensive.
The problem is, I know of no way to override a static method. You can't declare it virtual. you can't include it in an interface.
Sorry to be a negative nelly.
添加一个示例,说明如何使用 EasyMock / PowerMock 沿着注入类的常规模拟实现静态模拟,因为链接的示例仅显示静态模拟。
使用
PowerMockRunner
时,@Mock
服务不会连接到@TestSubject
服务进行测试。假设我们有一个要测试的服务,ServiceOne:
它调用我们想要模拟的另一个服务,ServiceTwo:
它调用一个最终类静态方法,ServiceUtils:
当调用
ServiceOne.methodToTest()
我们得到"+ServiceTwoReturn"
作为回报。使用 EasyMock 进行 Junit 测试,仅模拟注入的 ServiceTwo Spring 服务:
使用 EasyMock 进行 Junit 测试PowerMock,模拟注入的 ServiceTwo Spring 服务以及最终类及其静态方法:
Adding an exemple on how to implements static mock along regular mock of injected classes with EasyMock / PowerMock, since the linked exemple only shows static mock.
And with the
PowerMockRunner
the@Mock
services are not wired on the@TestSubject
service to test.Let say we have a service we want to test, ServiceOne :
Which calls another service that we will want to mock, ServiceTwo :
And which calls a final class static method, ServiceUtils :
When calling
ServiceOne.methodToTest()
we get"+ServiceTwoReturn"
as a return.Junit Test with EasyMock, mocking only the injected ServiceTwo Spring service :
Junit Test with EasyMock & PowerMock, mocking the injected ServiceTwo Spring service but also the final class and its Static method :