java EasyMock——测试类中的模拟方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13592302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EasyMock -- mock methods within tested class?
提问by Jeremy
In my code I sometimes call public or private methods within the same class. These methods aren't a good candidate for being pulled out into their own class. Each of these methods that I call are tested in their own unit test.
在我的代码中,我有时会在同一个类中调用公共或私有方法。这些方法不适合被拉入它们自己的类中。我调用的这些方法中的每一个都在它们自己的单元测试中进行了测试。
So, if I have a method in my class A that calls each of those methods also in class A, is there some way to mock the calls? I can certainly cut and paste my expectations/mock behavior, but not only is that tedious, it obfuscates the point of the test, violates modularity, and makes testing more difficulty because of the inability to control what is returned.
因此,如果我的 A 类中有一个方法在 A 类中也调用了这些方法中的每一个,那么是否有某种方法可以模拟这些调用?我当然可以剪切和粘贴我的期望/模拟行为,但这不仅乏味,而且混淆了测试的重点,违反了模块化,并且由于无法控制返回的内容而使测试变得更加困难。
If not, what is the usual solution to this kind of thing?
如果不是,这种事情通常的解决方案是什么?
采纳答案by ach
It sounds like you're looking for Partial Mocks... here's one blog post that covers them: http://www.jroller.com/alessiopace/entry/partial_mocks_with_easymock
听起来您正在寻找部分模拟......这是一篇涵盖它们的博客文章:http: //www.jroller.com/alessiopace/entry/partial_mocks_with_easymock
This requires the EasyMock ClassExtension, which unfortunately can't mock private methods however.
这需要 EasyMock ClassExtension,不幸的是它不能模拟私有方法。
回答by Jeremy
This can be done with EasyMock 2.2 class extension, or EasyMock 3.0 and on (which includes class extension.)
这可以通过 EasyMock 2.2 类扩展或 EasyMock 3.0 及更高版本(包括类扩展)来完成。
Partial mocking is documented here:
部分模拟记录在此处:
http://www.easymock.org/EasyMock2_2_2_ClassExtension_Documentation.html
http://www.easymock.org/EasyMock2_2_2_ClassExtension_Documentation.html
The syntax is fairly simple. You specify the class you're mocking and which methods you're mocking. In this example, imagine that the class is "Dog" and it has two methods, "eat" and "eatUntilFull". You might put this code in the eatUntilFull test:
语法相当简单。您指定要模拟的类以及要模拟的方法。在这个例子中,假设类是“Dog”,它有两个方法,“eat”和“eatUntilFull”。您可以将此代码放在eatUntilFull 测试中:
mockDog = createMockBuilder(Dog.class).addMockedMethod("eat").createMock();
You can then treat that like any other mock.
然后,您可以像对待任何其他模拟一样对待它。
Caveats:
注意事项:
1) Calling one method within your class from another might be an indication of poor design -- can you abstract out that logic to another class?
1)从另一个类中调用一个方法可能表明设计很差——你能将该逻辑抽象到另一个类吗?
2) Even if you can't, there may be no problem in letting your method call another method itself during the test. This may be the preferred behavior.
2)即使你不能,在测试期间让你的方法调用另一个方法本身可能没有问题。这可能是首选行为。
3) You still can't target private methods, so you may want to set them as package-private instead of private.
3) 您仍然不能以私有方法为目标,因此您可能希望将它们设置为包私有而不是私有。
回答by mbelow
Generally speaking, if you are facing the need to mock a private method (or a public method on the same class you're testing), you should really consider moving the code in this method to another class.
一般来说,如果您需要模拟私有方法(或您正在测试的同一类上的公共方法),您应该真正考虑将此方法中的代码移动到另一个类。
From the test's point of view, it should be of no interest howthe method you are testing archives the expected state (whether it calls some other method or not). The essential point of interest should be the resulting change of state a method performs, not which methods it calls to do so.
从测试的角度来看,您正在测试的方法如何存档预期状态(无论它是否调用其他方法)应该无关紧要。重要的兴趣点应该是一个方法执行的状态的结果变化,而不是它调用哪些方法来这样做。