vendredi 6 décembre 2019

Invoking by reflection a private static method

When I call java.lang.reflect.Method.invoke() method on a private static method, I get a "java.lang.IllegalArgumentException : wrong number of arguments". I think I respect javadoc specification, did I something wrong ? Thanks for any help.

Here is my code :

public class MyClass {
    ....
    private static Object myMethod(String[] stringArray) {...}
}

I want to test myMethod() in a JUnit test class :

public class MyClassTest {
    private static String[] myArray = {"A", "B", "C"};
    @Test
    public void myMethodTest() {
        Method method = Class.forName("mypackage.MyClass").getDeclaredMethod("myMethod", myArray.getClass());
        method.setAccessible(true);
        method.invoke(null, myArray);
    }
}




Aucun commentaire:

Enregistrer un commentaire