Search code examples
javapowermockwhite-boxwhite-box-testing

Why does PowerMock's example for use of Whitebox.invokeConstructor() throw a ConstructorNotFoundException?


When I then try to run the second example from PowerMock's Bypass Encapsulation docs, using PowerMock 1.5.2 (which we use at my company), I immediately get a ConstructorNotFoundException thrown. I tried switching to version 1.6.2, with the same result.

Any ideas what I might be doing wrong? (I'm not using any of the PowerMock annotations, as per example, and am running Java 1.7.) I'm sure it must be a simple oversight on my part...

Here's my POM for the example from the docs:

<?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>

And here's the test class:

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();
    }
}

Here's the exception in all its glory:

org.powermock.reflect.exceptions.ConstructorNotFoundException: Failed to find a constructor with parameter types: [[Ljava.lang.Class;, java.lang.Integer] at org.powermock.reflect.internal.WhiteboxImpl.invokeConstructor(WhiteboxImpl.java:1354) at org.powermock.reflect.Whitebox.invokeConstructor(Whitebox.java:511) at Test.test(Test.java:6) ...

Any ideas? I'm sure the thing I'm missing is super simple...


Solution

  • This must be a mistake in the example. Looking at the signature of public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] parameterTypes, Object[] arguments), you should pass an array of Objects as the last argument. I have modified the example a bit to illustrate this.

    The test:

    @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();
    }
    

    The class:

    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;
       }
    }
    

    Test output:

    int 43
    Integer 43