Whitebox.invokeConstructor() throws ConstructorNotFoundException for example code from docs (for 1.5.2, 1.6.2 with Java 1.7)

293 views
Skip to first unread message

Christian Balzer

unread,
Jun 9, 2015, 10:50:45 AM6/9/15
to powe...@googlegroups.com
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.classnew 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).

I then tried to run the second example from https://code.google.com/p/powermock/wiki/BypassEncapsulation#Instantiate_a_class_with_a_private_constructor, using PowerMock 1.5.2 (which we use at my company), and immediately got the second error returned. I then switched to 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.)

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
Reply all
Reply to author
Forward
0 new messages