Unit testing PowerMock:如何模拟类中具有相同名称的静态方法?

Unit testing PowerMock:如何模拟类中具有相同名称的静态方法?,unit-testing,mockito,powermock,Unit Testing,Mockito,Powermock,今天我正在处理一个类,它有两个静态方法,它们具有相同的名称和不同的参数类型。当我试图模仿其中一种方法时,我遇到了这个问题 这是要模拟的类: //RequestUtil.java, I want to mock the second config method public class RequestUtil { public static void config(String request, List parameters){} public static void confi

今天我正在处理一个类,它有两个静态方法,它们具有相同的名称和不同的参数类型。当我试图模仿其中一种方法时,我遇到了这个问题

这是要模拟的类:

//RequestUtil.java, I want to mock the second config method
public class RequestUtil {
    public static void config(String request, List parameters){}
    public static void config(HttpServletRequest request, List parameters){}
}
这是测试类:

//RequestUtilTest.java
@RunWith(PowerMockRunner.class)
@PrepareForTest(RequestUtil.class)
public class RequestUtilTest {
    //this test will throw NullPointException
    @Test
    public void testConfig() throws Exception {
        mockStatic(RequestUtil.class);
        doNothing().when(RequestUtil.class, "config", any(HttpServletRequest.class), anyList());
    }
} 
运行此测试,它将引发异常:

java.lang.NullPointerException
    at java.lang.Class.isAssignableFrom(Native Method)
    at org.powermock.reflect.internal.WhiteboxImpl.checkIfParameterTypesAreSame(WhiteboxImpl.java:2432)
    at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1934)
    at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:1025)
    at org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:948)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:882)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:859)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
    at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:106)
   ...
此异常是由以下原因引起的:

doNothing().when(RequestUtil.class, "config", any(HttpServletRequest.class), anyList());
但是,如果我模拟第一个config方法,这意味着将此行替换为:

doNothing().when(RequestUtil.class, "config", anyString(), anyList());
一切都好

RequestUtil类定义中配置方法的顺序与此问题无关。无论config(HttpServletRequest,List)是RequestUtil的第一个或第二个配置方法,config(HttpServletRequest,List)的模拟都将失败

此外,如果我将HttpServletRequest修改为另一个“更简单”的类型,例如int,那么这个问题就会消失

这似乎是PowerMock的一个bug,但我不确定。我搜索了Google和stackoverflow,但没有关于这个问题的帖子或讨论。有人能帮我吗

我使用的测试框架:

JUnit: 4.10
PowerMock: 1.5.4
Mockito: 1.9.5

这似乎是一个带有

您可以通过使用查找方法对象并显式模拟此方法来绕过它

...
import org.powermock.reflect.Whitebox;

//RequestUtilTest.java
@RunWith(PowerMockRunner.class)
@PrepareForTest(RequestUtil.class)
public class RequestUtilTest {
    //this test will throw NullPointException
    @Test
    public void testConfig() throws Exception {
        mockStatic(RequestUtil.class);
        Method method = Whitebox.getMethod(RequestUtil.class, "config", HttpServletRequest.class, List.class);
        doNothing().when(RequestUtil.class, method);
    }
} 
在stackoverflow之前已被询问过