如何抑制和验证私有静态方法调用? [英] How to suppress and verify private static method calls?

查看:226
本文介绍了如何抑制和验证私有静态方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在JUnit测试中遇到困难,需要一些帮助.因此,我通过静态方法获得了该类,该方法将重构一些对象.为了简化起见,我举了一个小例子.这是我的工厂班级:

I am currently stumbling in JUnit testing and need some help. So I got this class with static methods which will refactor some objects. For simplification's sake I have made a small example. This is my Factory class:

class Factory {

    public static String factorObject() throws Exception {
        String s = "Hello Mary Lou";
        checkString(s);
        return s;
    }

    private static void checkString(String s) throws Exception {
        throw new Exception();
    }
}

这是我的Test课:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Factory.class })        
public class Tests extends TestCase {

    public void testFactory() throws Exception {

        mockStatic(Factory.class);
        suppress(method(Factory.class, "checkString"));
        String s = Factory.factorObject();
        assertEquals("Hello Mary Lou", s);
    }
}

基本上,我试图实现的是应该禁止私有方法checkString()(这样就不会引发Exception),并且还需要验证方法checkString()实际上是在factorObject()方法中调用的.

Basically what I tried to achieve is that the private method checkString() should be suppressed (so the Exception is not thrown), and also need to verify that the method checkString() was actually called in the method factorObject().

已更新: 该抑制可通过以下代码正常工作:

UPDATED: The suppression works correctly with the following code:

suppress(method(Factory.class, "checkString", String.class));
String s = Factory.factorObject();

...但是它为字符串"s"返回了NULL.为什么会这样?

... however it returns me NULL for the String "s". Why is that?

推荐答案

好吧,我终于找到了解决所有问题的方法.如果有人偶然发现类似问题,则代码如下:

Ok, I finally found the solution to all problems. If anyone stumbles across similar issues here is the code:

import junit.framework.TestCase;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.times;
import static org.powermock.api.support.membermodification.MemberModifier.suppress;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Factory.class)
public class Tests extends TestCase {

    public void testFactory() throws Exception {

        mockStatic(Factory.class, Mockito.CALLS_REAL_METHODS);
        suppress(method(Factory.class, "checkString", String.class));
        String s = Factory.factorObject();
        verifyPrivate(Factory.class, times(1)).invoke("checkString", anyString()); 
        assertEquals("Hello Mary Lou", s);      
    }
}

这篇关于如何抑制和验证私有静态方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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