首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么PowerMock使用Whitebox.invokeConstructor()的示例抛出一个ConstructorNotFoundException?

为什么PowerMock使用Whitebox.invokeConstructor()的示例抛出一个ConstructorNotFoundException?
EN

Stack Overflow用户
提问于 2015-06-09 11:04:13
回答 1查看 2.4K关注 0票数 0

然后,当我尝试使用PowerMock的旁路封装文档 (我们在公司使用的PowerMock 1.5.2 )运行第二个示例时,我会立即得到一个ConstructorNotFoundException。我尝试切换到1.6.2版本,结果也是一样。

知道我可能做错了什么吗?(例如,我没有使用任何PowerMock注释,而是运行Java1.7。)我相信这一定是我的疏忽.

下面是我的POM文档中的示例:

代码语言:javascript
复制
<?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>

这是考试课:

代码语言:javascript
复制
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) .

有什么想法吗?我肯定我错过的是非常简单的..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-09 11:37:44

这一定是这个例子中的一个错误。查看public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] parameterTypes, Object[] arguments)的签名,您应该传递一个对象数组作为最后一个参数。我对这个例子作了一点修改,以说明这一点。

测试

代码语言:javascript
复制
@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();
}

类:

代码语言:javascript
复制
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;
   }
}

测试输出:

代码语言:javascript
复制
int 43
Integer 43
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30730236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档