java 如何抑制和验证私有静态方法调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16458981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:00:07  来源:igfitidea点击:

How to suppress and verify private static method calls?

javajunitpowermockverifysuppress

提问by Malvin

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:

我目前在 JUnit 测试中遇到困难,需要一些帮助。所以我用静态方法得到了这个类,它会重构一些对象。为了简单起见,我做了一个小例子。这是我的工厂类:

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();
    }
}

And this is my Test class:

这是我的测试类:

@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);
    }
}

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().

基本上我试图实现的是应该抑制私有方法checkString()(因此不会抛出异常),并且还需要验证方法checkString()实际上是在方法factorObject()中调用的。

UPDATED: The suppression works correctly with the following code:

更新:抑制与以下代码正常工作:

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

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

...但是它为字符串“s”返回NULL。这是为什么?

回答by Malvin

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);      
    }
}

回答by Shreyos Adikari

Yo can do it like:

你可以这样做:

PowerMockito.doNothing().when(Factory.class,"checkString");

For more details you can visit :
http://powermock.googlecode.com/svn/docs/powermock-1.3.7/apidocs/org/powermock/api/mockito/PowerMockito.html

有关更多详细信息,您可以访问:http:
//powermock.googlecode.com/svn/docs/powermock-1.3.7/apidocs/org/powermock/api/mockito/PowerMockito.html

Edit:

编辑:

ClassToTest spy = spy(new ClassToTest ());
doNothing().when(spy).methodToSkip();
spy.methodToTest();