PowerMock 多个匹配的构造函数; (可变长度数组构造函数与无参数构造函数)
PowerMock multiple matching constructors; (variable length array constructor vs no-param constructor)
我正在尝试在 ResourceConfig.class 中模拟没有参数的构造函数。
碰巧 ResourceConfig 有两个构造函数:(除其他外):
public ResourceConfig()
public ResourceConfig(Class... class)
PowerMock (1.7.3) 无法获得正确的构造函数。
我认为这是一个错误;但也许有解决方案(?)
代码:
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.whenNew;
@RunWith(PowerMockRunner.class)
@PrepareForTest( ResourceConfig.class )
public class WhosebugTest {
@Test
public void toStackOvflow2() throws Exception {
ResourceConfig resConf = mock(ResourceConfig.class);
whenNew(ResourceConfig.class).withNoArguments().thenReturn(resConf);
//WHATEVER...
}
}
这会产生:
org.powermock.reflect.exceptions.TooManyConstructorsFoundException:
Several matching constructors found, please specify the argument
parameter types so that PowerMock can determine which method you're
referring to. Matching constructors in class
org.glassfish.jersey.server.ResourceConfig were:
org.glassfish.jersey.server.ResourceConfig( )
org.glassfish.jersey.server.ResourceConfig( [Ljava.lang.Class;.class )
at
org.powermock.reflect.internal.ConstructorFinder.throwExceptionWhenMultipleConstructorMatchesFound(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
可以接收 String
或 Uri
的 File
class 示例:
whenNew(File.class).withParameterTypes(String.class).withArguments(any(String.class)).thenReturn(mockFile);
最佳方法(如果您知道这种情况下的字符串值):
whenNew(File.class).withArguments(eq("SOME STRING")).thenReturn(mockFile);
我正在尝试在 ResourceConfig.class 中模拟没有参数的构造函数。 碰巧 ResourceConfig 有两个构造函数:(除其他外):
public ResourceConfig()
public ResourceConfig(Class... class)
PowerMock (1.7.3) 无法获得正确的构造函数。 我认为这是一个错误;但也许有解决方案(?)
代码:
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.whenNew;
@RunWith(PowerMockRunner.class)
@PrepareForTest( ResourceConfig.class )
public class WhosebugTest {
@Test
public void toStackOvflow2() throws Exception {
ResourceConfig resConf = mock(ResourceConfig.class);
whenNew(ResourceConfig.class).withNoArguments().thenReturn(resConf);
//WHATEVER...
}
}
这会产生:
org.powermock.reflect.exceptions.TooManyConstructorsFoundException: Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're referring to. Matching constructors in class org.glassfish.jersey.server.ResourceConfig were:
org.glassfish.jersey.server.ResourceConfig( )
org.glassfish.jersey.server.ResourceConfig( [Ljava.lang.Class;.class )at org.powermock.reflect.internal.ConstructorFinder.throwExceptionWhenMultipleConstructorMatchesFound(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
可以接收 String
或 Uri
的 File
class 示例:
whenNew(File.class).withParameterTypes(String.class).withArguments(any(String.class)).thenReturn(mockFile);
最佳方法(如果您知道这种情况下的字符串值):
whenNew(File.class).withArguments(eq("SOME STRING")).thenReturn(mockFile);