Java PowerMock多个匹配构造函数;(可变长度数组构造函数与无参数构造函数)

Java PowerMock多个匹配构造函数;(可变长度数组构造函数与无参数构造函数),java,unit-testing,junit,powermock,Java,Unit Testing,Junit,Powermock,我试图在ResourceConfig.class中模拟没有参数的构造函数。 ResourceConfig恰好有两个构造函数:(除其他构造函数外): PowerMock(1.7.3)未能获得正确的构造函数。 我会认为这是一个错误;但也许有一个解决办法(?) 代码: 这将产生: org.powermock.reflect.exceptions.ToomanyConstructors取消异常: 找到多个匹配的构造函数,请指定参数 参数类型,以便PowerMock可以确定使用哪种方法 指。类中的匹配构造

我试图在ResourceConfig.class中模拟没有参数的构造函数。 ResourceConfig恰好有两个构造函数:(除其他构造函数外):

PowerMock(1.7.3)未能获得正确的构造函数。 我会认为这是一个错误;但也许有一个解决办法(?)

代码:

这将产生:

org.powermock.reflect.exceptions.ToomanyConstructors取消异常: 找到多个匹配的构造函数,请指定参数 参数类型,以便PowerMock可以确定使用哪种方法 指。类中的匹配构造函数 org.glassfish.jersey.server.ResourceConfig是:
org.glassfish.jersey.server.ResourceConfig()
org.glassfish.jersey.server.ResourceConfig([Ljava.lang.Class;.Class)

在 org.powermock.reflect.internal.ConstructorFinder.ThroweExceptionWhenMultipleConstructorMatchesFound(ConstructorFinder.java:89)


有什么想法吗?

您可以抑制多个构造函数,例如:

@Test
public void toStackOvflow2() throws Exception {
    ResourceConfig resConf = mock(ResourceConfig.class);

    // suppress TooManyConstructorsFoundException
    MemberModifier.suppress(MemberMatcher.constructorsDeclaredIn(ResourceConfig.class));
    whenNew(ResourceConfig.class).withNoArguments().thenReturn(resConf);

    // verifying that the expected ResourceConfig instance is returned when using the default ctor ...
    assertSame(resConf, new ResourceConfig());
}
此测试通过时:

  • PowerMock 1.7.3
  • 泽西岛2.26

您可以抑制多个构造函数,例如:

@Test
public void toStackOvflow2() throws Exception {
    ResourceConfig resConf = mock(ResourceConfig.class);

    // suppress TooManyConstructorsFoundException
    MemberModifier.suppress(MemberMatcher.constructorsDeclaredIn(ResourceConfig.class));
    whenNew(ResourceConfig.class).withNoArguments().thenReturn(resConf);

    // verifying that the expected ResourceConfig instance is returned when using the default ctor ...
    assertSame(resConf, new ResourceConfig());
}
此测试通过时:

  • PowerMock 1.7.3
  • 泽西岛2.26

可以接收
字符串
Uri
文件
类的示例:

whenNew(File.class).withParameterTypes(String.class).withArguments(any(String.class)).thenReturn(mockFile);
最佳方法(如果您知道本例中的字符串值):


可以接收
字符串
Uri
文件
类的示例:

whenNew(File.class).withParameterTypes(String.class).withArguments(any(String.class)).thenReturn(mockFile);
最佳方法(如果您知道本例中的字符串值):


这对我不起作用,这对我不起作用。
whenNew(File.class).withArguments(eq("SOME STRING")).thenReturn(mockFile);