[英]PowerMock, mockito, verify static method
I'm trying to get PowerMock to work with mockito, and I'm following the documentation here: http://code.google.com/p/powermock/wiki/MockitoUsage13 .我试图让 PowerMock 与 mockito 一起工作,我正在关注这里的文档: http : //code.google.com/p/powermock/wiki/MockitoUsage13 。
To simplify a bit, lets say that I have a static method:为了简化一点,假设我有一个静态方法:
StaticObj.put(String key, String val) { ... }
And the class to be tested does something like this:要测试的类会执行以下操作:
public class ClassToTest {
public void doSomething(Params p) {
if (StringUtils.isNotBlank(p.getK()) StaticObj.put("k1", p.getK());
if (StringUtils.isNotBlank(p.getX()) StaticObj.put("x1", p.getX());
}
}
In my unit test I'd like to verify that StaticObj.put is called for K and X when they are not blank or null, so I do something like this:在我的单元测试中,我想验证当 K 和 X 不为空或不为空时是否为 K 和 X 调用了 StaticObj.put,因此我执行以下操作:
public void testNormalCase() {
// assume that mocking setup for statics already happened in some @Before function..
Params params = new Params("k", "x");
ClassToTest classToTest = new ClassToTest();
classToTest.doSomething(params);
// now I want to verify:
PowerMockito.verifyStatic(times(1));
StaticObj.put("k1", "k1");
PowerMockito.verifyStatic(times(1));
StaticObj.put("x1", "x");
}
This works, and it's what I'd expect.这有效,这正是我所期望的。 What doesn't work, is if I comment out the verification for K, then the verification for X fails!
不起作用的是,如果我注释掉 K 的验证,则 X 的验证失败! The error message indicates that ("x1", "x") is expected but got ("k1", "k").
错误消息表明 ("x1", "x") 是预期的,但得到了 ("k1", "k")。 Why is this?
为什么是这样? Am I not coding this correctly?
我没有正确编码吗?
Also it leads me to believe that the following type of test, which passes, might pass for the wrong reason entirely:这也让我相信以下类型的测试通过,可能完全出于错误的原因通过:
public void testOtherCase() {
// assume that mocking setup for statics already happened in some @Before function..
Params params = new Params("k", null);
ClassToTest classToTest = new ClassToTest();
classToTest.doSomething();
// now I want to verify:
PowerMockito.verifyStatic(never());
StaticObj.put(eq("x1"), anyString());
}
Eg I wonder if powermock sees "k1", decides that "x1" was never called, and passes.例如,我想知道 powermock 是否看到“k1”,决定“x1”从未被调用,然后通过。 (?)
(?)
To state it generally, I have a static method that is called N times (where N changes depending on the input params).概括地说,我有一个静态方法,它被称为 N 次(其中 N 根据输入参数而变化)。 And I want to verify that it was called in the correct cases (which can be determined by input params).
我想验证它是否在正确的情况下被调用(可以由输入参数确定)。 It seems like powermock doesn't handle this well, unless I misunderstand.
除非我误解,否则似乎 powermock 不能很好地处理这个问题。
Thanks for any ideas!感谢您的任何想法!
I read this question and the issue carefully but not sure if I understood them clearly - From my understanding, it's correct that powermock raise the exception when you pass k and x but only verify k.我仔细阅读了这个问题和问题,但不确定我是否清楚地理解它们 - 根据我的理解,当您传递 k 和 x 但仅验证 k 时,powermock 引发异常是正确的。
Because you are mocking the static method StaticObj.put, when you pass parameter k and x and verify it with因为你在模拟静态方法StaticObj.put,当你传递参数k和x并用
PowerMockito.verifyStatic(times(1));
StaticObj.put("k1", "k1");
PowerMockito.verifyStatic(times(1));
StaticObj.put("x1", "x");
This should work.这应该有效。 And when you verify parameter k and x with verification for k is commented out.
当您验证参数 k 和 x 时, k 的验证被注释掉。
// PowerMockito.verifyStatic(times(1));
// StaticObj.put("k1", "k1");
PowerMockito.verifyStatic(times(1));
StaticObj.put("x1", "x");
Powermock will get the invocation with put("k1"...) first apparently, so the verification of x will raise an error. Powermock 显然会首先使用 put("k1"...) 获得调用,因此 x 的验证将引发错误。 Your verification process is sequenced.
您的验证过程是有序的。
I don't know as of which version, but PowerMockito.verifyStatic(VerificationMode)
is deprecated.我不知道是哪个版本,但
PowerMockito.verifyStatic(VerificationMode)
已被弃用。 Just wanted to point that out to anyone else finding this years after the last post.只是想向在上一篇文章发布后几年发现这一点的其他人指出这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.