PowerMock,mockito,验证静态方法 [英] PowerMock, mockito, verify static method

查看:1991
本文介绍了PowerMock,mockito,验证静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让PowerMock与mockito一起使用,我在这里关注文档: http://code.google.com/p/powermock/wiki/MockitoUsage13



为了简化一下,我们假设我有一个静态方法:

  StaticObj.put(String key,String val){...} 

和班级要测试的是这样的:

  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我的单元测试我想验证当它们不为空或为null时,为K和X调用StaticObj.put,所以我做这样的事情:

  public void testNormalCase(){
//假设某些@Before函数已经发生了静态的模拟设置..
Params params = new Params(k, x);
ClassToTest classToTest = new ClassToTest();
classToTest.doSomething(params);

/ /现在我要验证:
PowerMockito.verifyStatic(times(1));
StaticObj.put(k1,k1);

PowerMockito.verifyStatic(times(1));
StaticObj.put(x1,x);
}

这是有效的,这是我所期待的。什么行不通,如果我注释掉K的验证,那么X的验证失败了!错误消息表明(x1,x)是预期但得到(k1,k)。为什么是这样?我没有正确编码吗?



此外,它让我相信以下类型的测试可能会完全错误地传递:

  public void testOtherCase(){
//假设某些@Before函数已经发生了静态的模拟设置..
Params params = new Params(k,null);
ClassToTest classToTest = new ClassToTest();
classToTest.doSomething();

//现在我要验证:

PowerMockito.verifyStatic(never());
StaticObj.put(eq(x1),anyString());
}

例如。我想知道powermock是否看到k1,决定从未调用过x1并且通过。 (?)



为了说明一般情况,我有一个静态方法,称为N次(其中N根据输入参数变化)。我想验证它是在正确的情况下调用的(可以通过输入参数确定)。似乎powermock不能很好地解决这个问题,除非我误解。



感谢任何想法!

解决方案

我仔细阅读了这个问题和问题,但不确定我是否清楚地理解了它们 - 根据我的理解,当你传递k和x但只验证k时,powermock会引发异常是正确的。



因为你在模拟静态方法StaticObj.put,当你传递参数k和x并用



$ b <$验证它时p $ p> PowerMockito.verifyStatic(次(1));
StaticObj.put(k1,k1);

PowerMockito.verifyStatic(times(1));
StaticObj.put(x1,x);

这应该有效。当您验证参数k和x并验证k被注释掉时。

  // PowerMockito.verifyStatic(times(1) ); 
// StaticObj.put(k1,k1);

PowerMockito.verifyStatic(times(1));
StaticObj.put(x1,x);

Powermock首先会得到put(k1...)的调用,所以验证x会引发错误。您的验证过程已按顺序排列。


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.

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:

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! The error message indicates that ("x1", "x") is expected but got ("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());
}

E.g. I wonder if powermock sees "k1", decides that "x1" was never called, and passes. (?)

To state it generally, I have a static method that is called N times (where N changes depending on the input params). 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.

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.

Because you are mocking the static method StaticObj.put, when you pass parameter k and x and verify it with

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.

// 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. Your verification process is sequenced.

这篇关于PowerMock,mockito,验证静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆