LoginSignup
2
1

More than 5 years have passed since last update.

JUnitで引数に配列や可変長引数をとるprivateメソッドをテストする方法

Posted at

JUnitでprivateメソッドをテストする方法 - Qiita で勉強したけれど・・・足りないの更に勉強です。

環境
OS : macOS Hight Sierra
Eclipse : Neon.3 Release (4.6.3)
JUnit : 4
JDK : 1.8.45

テスト対象は配列や可変長引数をとるprivateメソッドです。
package variable.length.argument;

public class WhatVariableLengthArgument {
    /**
     * 可変長引数を表示する.
     * @param description 説明文.
     * @param args 可変長引数.
     */
    private void printLengthArgs(String description, Object... args) {
        System.out.print(description + " : ");
        for (Object object : args) {
            System.out.print(object + ",");
        }
        System.out.println();
    }

    /**
     * 引数の配列を表示する.
     * @param description 配列の説明.
     * @param array 配列.
     */
    private void printArrayArgs(String description, String[] array) {
        System.out.print(description + " : ");
        for (String s : array) {
            System.out.print(s + ",");
        }
        System.out.println();
    }
}

引数が配列の場合は、getDeclaredMethod()型名[].classを指定する

引数が配列の場合
public class WhatVariableLengthArgumentTest {
    /** テスト対象のインスタンス. */
    WhatVariableLengthArgument testInstance = new WhatVariableLengthArgument();

    @Test
    public void 配列を引数に取るメソッドのテスト() throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        配列を引数に取るメソッドを実行する("引数が配列", new String[] { "1番目", "2番目" });
    }

    private void 配列を引数に取るメソッドを実行する(String description, String[] array)
            throws NoSuchMethodException, SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        String methodName = "printArrayArgs";
        Method method = 配列を引数に取るメソッドを取得する(methodName);
        method.invoke(this.testInstance, description, array);
    }

    private Method 配列を引数に取るメソッドを取得する(String methodName)
            throws NoSuchMethodException, SecurityException {
        // 配列も普通にString[].classと書けます.
        Method method = WhatVariableLengthArgument.class.getDeclaredMethod(
                methodName, String.class, String[].class);
        method.setAccessible(true);
        return method;
    }
}

Class arrClass = String[].class; //配列もOK
Javaリフレクションメモ(Hishidama's Java Reflection Memo)

引数が可変長引数の場合も配列の場合と同様にする

引数が可変長引数の場合
    WhatVariableLengthArgument testInstance = new WhatVariableLengthArgument();

    @Test
    public void 可変長引数を引数に取るメソッドのテスト() throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        可変長引数を引数に取るメソッドを実行する("引数が可変長引数 : String1つ", "1番目");
        可変長引数を引数に取るメソッドを実行する("引数が可変長引数 : String2つ", "1番目", "2番目");
    }

    private void 可変長引数を引数に取るメソッドを実行する(String description, Object... args)
            throws NoSuchMethodException, SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {
        String methodName = "printLengthArgs";
        Method method = 可変長引数を引数に取るメソッドを取得する(methodName);
        method.invoke(this.testInstance, description, args);
    }

    private Method 可変長引数を引数に取るメソッドを取得する(String methodName)
            throws NoSuchMethodException, SecurityException {
        // 可変長引数はコンパイルすると配列となるので配列と同様に書けます.
        Method method = WhatVariableLengthArgument.class.getDeclaredMethod(
                methodName, String.class, Object[].class);
        method.setAccessible(true);
        return method;
    }

可変長引数ありのメソッド
可変長引数を持つメソッドをgetMethod()で取得したい場合、引数の型には配列を指定する。
Javaリフレクションメモ(Hishidama's Java Reflection Memo)

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1