然后,当我尝试使用PowerMock的旁路封装文档 (我们在公司使用的PowerMock 1.5.2 )运行第二个示例时,我会立即得到一个ConstructorNotFoundException。我尝试切换到1.6.2版本,结果也是一样。
知道我可能做错了什么吗?(例如,我没有使用任何PowerMock注释,而是运行Java1.7。)我相信这一定是我的疏忽.
下面是我的POM文档中的示例:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>PowerMock</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-mockito-release-full</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
</project>这是考试课:
import org.powermock.reflect.Whitebox;
public class Test {
@org.junit.Test
public void test() throws Exception {
PrivateConstructorInstantiationDemo instance = Whitebox.invokeConstructor(PrivateConstructorInstantiationDemo.class, new Class<?>[]{Integer.class}, 43);
System.out.println();
}
}这是它所有荣耀的一个例外:
org.powermock.reflect.exceptions.ConstructorNotFoundException:未能找到具有参数类型的构造函数:[Ljava.lang.Class;,java.lang.Integer at org.powermock.reflect.Whitebox.invokeConstructor(Whitebox.java:511) at Test.test(Test.java:6) .
有什么想法吗?我肯定我错过的是非常简单的..。
发布于 2015-06-09 11:37:44
这一定是这个例子中的一个错误。查看public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] parameterTypes, Object[] arguments)的签名,您应该传递一个对象数组作为最后一个参数。我对这个例子作了一点修改,以说明这一点。
测试
@org.junit.Test
public void test() throws Exception {
PrivateConstructorInstantiationDemo instance1 = Whitebox.invokeConstructor(PrivateConstructorInstantiationDemo.class, new Class<?>[]{Integer.TYPE}, new Object[]{43});
PrivateConstructorInstantiationDemo instance2 = Whitebox.invokeConstructor(PrivateConstructorInstantiationDemo.class, new Class<?>[]{Integer.class}, new Object[]{43});
System.out.println();
}类:
public static class PrivateConstructorInstantiationDemo {
private final int state;
private PrivateConstructorInstantiationDemo(int state) {
this.state = state;
System.out.println("int " + state);
}
private PrivateConstructorInstantiationDemo(Integer state) {
this.state = state;
System.out.println("Integer " + state);
// do something else
}
public int getState() {
return state;
}
}测试输出:
int 43
Integer 43https://stackoverflow.com/questions/30730236
复制相似问题