I studied at How to test private methods with JUnit --Qiita, but ...
environment OS : macOS Hight Sierra Eclipse : Neon.3 Release (4.6.3) JUnit : 4 JDK : 1.8.45
The test target is a private method that takes an array or variadic argument.
package variable.length.argument;
public class WhatVariableLengthArgument {
    /**
     *Display variadic arguments.
     * @param description description.
     * @param args variadic arguments.
     */
    private void printLengthArgs(String description, Object... args) {
        System.out.print(description + " : ");
        for (Object object : args) {
            System.out.print(object + ",");
        }
        System.out.println();
    }
    /**
     *Display an array of arguments.
     * @param description Array description.
     * @param array array.
     */
    private void printArrayArgs(String description, String[] array) {
        System.out.print(description + " : ");
        for (String s : array) {
            System.out.print(s + ",");
        }
        System.out.println();
    }
}
type name []. Class ingetDeclaredMethod ()If the argument is an array
public class WhatVariableLengthArgumentTest {
    /**Instance to be tested. */
    WhatVariableLengthArgument testInstance = new WhatVariableLengthArgument();
    @Test
public void Testing methods that take an array as an argument() throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Execute a method that takes an array as an argument("Argument is an array", new String[] { "1st", "Second" });
    }
Execute a method that takes a private void array as an argument(String description, String[] array)
            throws NoSuchMethodException, SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        String methodName = "printArrayArgs";
        Method method =Get a method that takes an array as an argument(methodName);
        method.invoke(this.testInstance, description, array);
    }
private Method Get a method that takes an array as an argument(String methodName)
            throws NoSuchMethodException, SecurityException {
        //Array is also a normal String[].Can be written as class.
        Method method = WhatVariableLengthArgument.class.getDeclaredMethod(
                methodName, String.class, String[].class);
        method.setAccessible(true);
        return method;
    }
}
Class arrClass = String []. Class; // Array is OKJava Reflection Memo (Hishidama's Java Reflection Memo)
If the argument is a variadic argument
    WhatVariableLengthArgument testInstance = new WhatVariableLengthArgument();
    @Test
public void Testing methods that take variadic arguments() throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Execute a method that takes a variadic argument("Variadic argument:One String", "1st");
Execute a method that takes a variadic argument("Variadic argument:2 Strings", "1st", "Second");
    }
private void Executes a method that takes a variadic argument(String description, Object... args)
            throws NoSuchMethodException, SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        String methodName = "printLengthArgs";
        Method method =Get a method that takes a variadic argument(methodName);
        method.invoke(this.testInstance, description, args);
    }
private Method Get a method that takes a variadic argument(String methodName)
            throws NoSuchMethodException, SecurityException {
        //Variadic arguments can be written in the same way as arrays because they become arrays when compiled..
        Method method = WhatVariableLengthArgument.class.getDeclaredMethod(
                methodName, String.class, Object[].class);
        method.setAccessible(true);
        return method;
    }
Methods with variadic arguments If you want to get a method with variable length argument by getMethod (), specify an array as the argument type. Java Reflection Memo (Hishidama's Java Reflection Memo)
Recommended Posts