reflectionpowermockitowhite-box-testing

PowerMockito - Whitebox Get Constructor of inner class with int[] parameter


As the title mentions, I'm using PowerMockito to test a class that contains an inner private class. The inner class has a constructor that has an 'int[]' parameter. Below is the code.

final Class clazz = Whitebox.getInnerClassType(SomeClass.class, "InnerClass");
final Constructor constructor = Whitebox.getConstructor(clazz, int[].class);
final Object innerClass = constructor.newInstance(SORT_ORDER);

//This is the TARGET INNER CLASS' CONSTRUCTOR
public InnerClass(int[] sortOrder) {
    super(sortOrder);
}

The code throws

org.powermock.reflect.exceptions.ConstructorNotFoundException: Failed to lookup constructor with parameter types [ [I ] in class


Solution

  • Class clazz = Whitebox.getInnerClassType(SomeClass.class, "InnerClass");
    Constructor constructor = Whitebox.getConstructor(clazz, SomeClass.class);
    InnerClassType innerClass = (InnerClassType) constructor.newInstance(new 
    SomeClass());
    

    Since my inner class was not static, it required a reference of the outer class.