Hi everyone,
I have tried to use the invokeConstructor() method on my own class, and it didn't work.
My own class A extends another one (B). A has one constructor declared. Its constructor is private and takes one argument of type T. B has no explicit constructors declared at all, but lives in a sub-package.
When I tried to run Whitebox.invokeConstructor(A.class, new T()); I got an exception org.powermock.reflect.exceptions.TooManyConstructorsFoundException: Could not determine which constructor to execute. Please specify the parameter types by hand.
I then tried Whitebox.invokeConstructor(A.class, new Class<?>[]{T.class}, new T()); and got yet another exception telling me org.powermock.reflect.exceptions.ConstructorNotFoundException: Failed to find a constructor with parameter types: [[Ljava.lang.Class;, com.example.bar.T]
Running A.class.getDeclaredConstructors(); returned just the one constructor I was expecting, private com.example.foo.A(com.example.bar.T).
Any ideas what I might be doing wrong? (I'm not using any of the PowerMock annotations, as per example.)
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:
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:9)
...
Any ideas?
Kind regards,
Christian