[英]How to test with PowerMock a method which calls another private void method from the same class?
I have a class which have some methods like in the example. 我有一个类,它有一些像示例中的方法。
public class TestClass {
public boolean aMethod()
{
voidMethod();
return true;
}
private void voidMethod()
{
... does something ...
}
... other methods ...
}
I want to test aMethod
with powermock and all methods should work normally except the voidMethod
. 我想用powermock测试aMethod
,除了voidMethod
之外,所有方法都应该正常工作。 I've created a partial mock of TestClass
to make voidMethod
do nothing.But I don't know how to expect call of this method. 我已经创建了一个部分模拟TestClass
来使voidMethod
什么都不做。但是我不知道怎么期望调用这个方法。
testObject = createPartialMock(TestClass.class, "voidMethod");
expectPrivate(testObject, "voidMethod");
I'm getting an error on second line: 我在第二行收到错误:
The method expect(T) in the type EasyMock is not applicable for the arguments (void) EasyMock类型中的方法expect(T)不适用于参数(void)
How can I fix this issue? 我该如何解决这个问题?
It looks like PowerMock can't mock a private void method (only private methods with a value returned). 看起来PowerMock不能模拟私有void方法(只返回返回值的私有方法)。 They should really provide a more useful compiler error to explicitly say that. 它们应该真正提供更有用的编译器错误来明确说明。 EasyMock doesn't mock private methods at all. EasyMock根本不会模拟私有方法。 @StanislawLeventhal's answer references the syntax for expecting (recording) calls to a non-private void method using EasyMock. @StanislawLeventhal的答案引用了使用EasyMock预期(记录)对非私有void方法的调用的语法。
What you should do first is consider whether what you are trying to do makes sense, and what really needs to be mocked. 你应该首先考虑的是考虑你要做的事情是否有意义,以及真正需要嘲笑的是什么。 Consider it a code smell when you are mocking a private method; 当你嘲笑私有方法时,请考虑它的代码味道; maybe you need to do it, but usually, there is a way to avoid it. 也许你需要这样做,但通常,有一种方法可以避免它。 It sounds obvious to think critically about why you are mocking, but it's easy to get confused and miss things. 批评性地思考你为什么嘲笑这一点听起来很明显,但很容易让人感到困惑和错过。
Why should you have a private method in your test class that you can't directly call and need to mock instead, but which doesn't return a value? 为什么你的测试类中有一个私有方法,你不能直接调用,而是需要模拟而不是返回值? Mocking is normally used to control what is returned from a method; 模拟通常用于控制从方法返回的内容; here, you are basically saying that this method must be called, but you want to override its implementation with nothing (if I understand correctly). 在这里,你基本上是说必须调用这个方法,但是你想要什么都不覆盖它的实现(如果我理解的话)。 I'm not sure I can think of a situation where this would be useful or necessary in a test class. 我不确定我能想到这在测试类中有用或必要的情况。 The example you provided is too generalized to understand the reasoning behind, but you should try to see if there is a better way to achieve your goal. 您提供的示例过于笼统,无法理解背后的推理,但您应该尝试查看是否有更好的方法来实现您的目标。
Use simple call like this: 使用这样的简单调用:
testObject.voidMethod(); // don't use "expect" for voids
expectLastCall().times(3); // use this for expectations
And don't forget reply()
after all you expectations and verify()
after running tested code. 而且不要忘了reply()
所有你期待之后和verify()
运行测试后的代码之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.