PowerMock,mockito,验证静态方法

编程入门 行业动态 更新时间:2024-10-27 12:33:56
本文介绍了PowerMock,mockito,验证静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试让PowerMock与mockito一起使用,我在这里关注文档: code.google/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 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: code.google/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,验证静态方法

本文发布于:2023-10-31 03:53:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   方法   PowerMock   mockito

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!