How to test private methods with arrays or variadic arguments in JUnit

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();
    }
}

If the argument is an array, specify 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 OK Java Reflection Memo (Hishidama's Java Reflection Memo)

When the argument is a variable length argument, do the same as for an array

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

How to test private methods with arrays or variadic arguments in JUnit
How to test private scope with JUnit
Test private methods in JUnit
Test private methods in JUnit
[Java] Test private methods with JUnit
How to filter JUnit Test in Gradle
[Java] How to test for null with JUnit
How to test interrupts during Thread.sleep with JUnit
JUnit 5: How to write test cases in enum
If you want to satisfy the test coverage of private methods in JUnit
Summary of root classes in various languages
[Swift] Variadic arguments
Parallel and parallel processing in various languages (Java edition)
Various pensions in Japan
Various threads in java
Use constructor with arguments in cucumber-picocontainer
How memory works in object-oriented languages
How to test private methods with arrays or variadic arguments in JUnit
Things to keep in mind when testing private methods in JUnit
How to test a private method with RSpec for yourself
How to run JUnit in Eclipse
Control test order in Junit4 with enumeration
How to fix system date in JUnit
How to test a private method in Java and partially mock that method
How to use "sign_in" in integration test (RSpec)
How to perform UT with Excel as test data with Spring Boot + JUnit5 + DBUnit
How to write test code with Basic authentication
Visualize test methods running in TestNG with listeners
Mixin test cases with JUnit 5 and default methods
How to access Java Private methods and fields
How to use a structure with variable length array or bit field in Ruby-FFI
How to embed JavaScript variables in HTML with Thymeleaf
How to implement UICollectionView in Swift with code only
How to call functions in bulk with Java reflection
[Java] How to omit the private constructor in Lombok
How to switch Tomcat context.xml with WTP in Eclipse
How to unit test with JVM with source using RxAndroid
How to use Z3 library in Scala with Eclipse
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
How to delete untagged images in bulk with Docker
How to use JDD library in Scala with Eclipse
How to test file upload screen in Spring + Selenium
How to convert A to a and a to A using AND and OR in Java
How to query Array in jsonb with Rails + postgres
Summary of how to implement default arguments in Java
How to use JUnit 5
Unit test with Junit.
How to separate words in names in classes, methods, and variables
How to pass an object to Mapper in MyBatis without arguments
How to handle exceptions coolly with Java 8 Stream or Optional
How to run only specific files with gem's rake test
How to get values in real time with TextWatcher (Android)
How to mock some methods of the class under test
Mapping to a class with a value object in How to MyBatis
How to erase test image after running Rspec test with CarrierWave
How to set up a proxy with authentication in Feign
[Java] I want to test standard input & standard output with JUnit
[Java] How to search for a value in an array (or list) with the contains method
How to deal with the event that Committee :: InvalidRequest occurs in committee during Rspec file upload test
Test Web API with junit
About validation methods in JUnit
How to use JUnit (beginner)
How to number (number) with html.erb
How to update with activerecord-import
How to write Junit 5 organized
How to migrate from JUnit4 to JUnit5
[Creating] How to use JUnit