How to suppress and verify private static method calls?

强颜欢笑 提交于 2020-01-14 08:43:28

问题


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

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

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?


回答1:


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



回答2:


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

Edit:

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


来源:https://stackoverflow.com/questions/16458981/how-to-suppress-and-verify-private-static-method-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!