JUnit4 privateなメソッド

またサンプル。

/**
 *
 */
package sample5;
/**
 * @author ssiscirine
 *
 */
public class Sample5 {
	/**
	 * @param args
	 */
	public static String publicStaticMethod1(String param1) {
		//privateStaticMethod1(param1);
		return param1;
	}
	/**
	 * @param args
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	private static String privateStaticMethod1(String param1) throws Exception {
		//Sample5 s5 = new Sample5();
		//s5.privateMethod1(param1);
		if (param1 == null) {
			throw new Exception("パラメータがnull");
		}
		return param1;
	}
	/**
	 * @param args
	 */
	public String publicMethod1(String param1) {
		return param1;
	}
	/**
	 * @param args
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	private String privateMethod1(String param1) throws Exception {
		if (param1 == null) {
			throw new Exception("パラメータがnull");
		}
		return param1;
	}
}

@SuppressWarnings(“unused”)は、privateなメソッドなのに使ってないせい。

/**
 *
 */
package sample5;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runners.MethodSorters;
/**
 * @author slani
 *
 */
//テストケースの名前順に実行する指定
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Sample5Test {
	@Rule
	//テストケースのメソッド名を知りたくて追加。
	public TestName name = new TestName();
	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		//
		System.out.println("setUpBeforeClass() JUnitを開始します。");
	}
	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		//
		System.out.println("tearDownAfterClass() JUnitを終了します");
	}
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		//
		System.out.println(name.getMethodName() + "のための setUp() テストケースの事前処理を行います。");
	}
	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		//
		System.out.println(name.getMethodName() + "のための" + " tearDown() テストケースの事後処理を行います。");
	}
	//Sample5#publicStaticMethod1用
	/**
	 * {@link sample5.Sample5#publicStaticMethod1(java.lang.String)} のためのテスト・メソッド。
	 */
	@Test
	public final void testPublicStaticMethod1_success() {
		Sample5.publicStaticMethod1("parame1");
	}
	//Sample5#privateStaticMethod1用
	/**
	 * {@link sample5.Sample5#privateStaticMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@Test
	public final void testPrivateStaticMethod1_Success() throws NoSuchMethodException, SecurityException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Sample5 s5 = new Sample5();
		Method m = null;
		m = Sample5.class.getDeclaredMethod("privateStaticMethod1", String.class);
		m.setAccessible(true);
		//正常ケース
		String rc = (String) m.invoke(s5, "param1");
		assertEquals("param1", rc);
	}
	/**
	 * {@link sample5.Sample5#privateStaticMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	@Test
	public final void testPrivateStaticMethod1_NoSuchMethodException_01() throws SecurityException {
		Sample5 s5 = new Sample5();
		try {
			Sample5.class.getDeclaredMethod("privateStaticMethodX", String.class);
		} catch (NoSuchMethodException e1) {
			assumeNoException(e1.getLocalizedMessage(), e1);
		}
	}
	/**
	 * {@link sample5.Sample5#privateStaticMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	@Test
	public final void testPrivateStaticMethod1_NoSuchMethodException_02() throws SecurityException {
		Sample5 s5 = new Sample5();
		try {
			Sample5.class.getDeclaredMethod("privateStaticMethod1", int.class);
		} catch (NoSuchMethodException e1) {
			assumeNoException(e1.getLocalizedMessage(), e1);
		}
	}
	/**
	 * {@link sample5.Sample5#privateStaticMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@Test
	public final void testPrivateStaticMethod1_IllegalAccessException()
			throws NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		Sample5 s5 = new Sample5();
		Method m = null;
		m = Sample5.class.getDeclaredMethod("privateStaticMethod1", String.class);
		//異常ケース
		String rc = null;
		try {
			rc = (String) m.invoke(s5, "param1");
		} catch (IllegalAccessException e) {
			assumeNoException(e.getLocalizedMessage(), e);
		}
	}
	/**
	 * {@link sample5.Sample5#privateStaticMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@Test
	public final void testPrivateStaticMethod1_IllegalArgumentException()
			throws NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException {
		Sample5 s5 = new Sample5();
		Method m = null;
		m = Sample5.class.getDeclaredMethod("privateStaticMethod1", String.class);
		m.setAccessible(true);
		//異常ケース
		String rc = null;
		try {
			rc = (String) m.invoke(s5, 1, 2, 3);
		} catch (IllegalArgumentException e) {
			assumeNoException(e.getLocalizedMessage(), e);
		}
	}
	/**
	 * {@link sample5.Sample5#privateStaticMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@Test
	public final void testPrivateStaticMethod1_InvocationTargetException()
			throws NoSuchMethodException, SecurityException, IllegalAccessException {
		Sample5 s5 = new Sample5();
		Method m = null;
		m = Sample5.class.getDeclaredMethod("privateStaticMethod1", String.class);
		m.setAccessible(true);
		//異常ケース
		String rc = null;
		try {
			rc = (String) m.invoke(s5, (String) null);
		} catch (InvocationTargetException e) {
			assumeNoException("target : " + e.getTargetException() + ", msg : " + e.getLocalizedMessage(), e);
		}
	}
	//Sample5#publicMethod1用
	/**
	 * {@link sample5.Sample5#publicMethod1(java.lang.String)} のためのテスト・メソッド。
	 */
	@SuppressWarnings("static-access")
	@Test
	public final void testPublicMethod1_success() {
		Sample5 s5 = new Sample5();
		s5.publicMethod1("parame1");
	}
	//Sample5#privateMethod1用
	/**
	 * {@link sample5.Sample5#privateMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@Test
	public final void testPrivateMethod1_Success() throws NoSuchMethodException, SecurityException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Sample5 s5 = new Sample5();
		Method m = null;
		m = Sample5.class.getDeclaredMethod("privateMethod1", String.class);
		m.setAccessible(true);
		//正常ケース
		String rc = (String) m.invoke(s5, "param1");
		assertEquals("param1", rc);
	}
	/**
	 * {@link sample5.Sample5#privateMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	@Test
	public final void testPrivateMethod1_NoSuchMethodException_01() throws SecurityException {
		Sample5 s5 = new Sample5();
		try {
			Sample5.class.getDeclaredMethod("privateMethodX", String.class);
		} catch (NoSuchMethodException e1) {
			assumeNoException(e1.getLocalizedMessage(), e1);
		}
	}
	/**
	 * {@link sample5.Sample5#privateMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	@Test
	public final void testPrivateMethod1_NoSuchMethodException_02() throws SecurityException {
		Sample5 s5 = new Sample5();
		try {
			Sample5.class.getDeclaredMethod("privateMethod1", int.class);
		} catch (NoSuchMethodException e1) {
			assumeNoException(e1.getLocalizedMessage(), e1);
		}
	}
	/**
	 * {@link sample5.Sample5#privateMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@Test
	public final void testPrivateMethod1_IllegalAccessException()
			throws NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		Sample5 s5 = new Sample5();
		Method m = null;
		m = Sample5.class.getDeclaredMethod("privateMethod1", String.class);
		//異常ケース
		String rc = null;
		try {
			rc = (String) m.invoke(s5, "param1");
		} catch (IllegalAccessException e) {
			assumeNoException(e.getLocalizedMessage(), e);
		}
	}
	/**
	 * {@link sample5.Sample5#privateMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@Test
	public final void testPrivateMethod1_IllegalArgumentException()
			throws NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException {
		Sample5 s5 = new Sample5();
		Method m = null;
		m = Sample5.class.getDeclaredMethod("privateMethod1", String.class);
		m.setAccessible(true);
		//異常ケース
		String rc = null;
		try {
			rc = (String) m.invoke(s5, 1, 2, 3);
		} catch (IllegalArgumentException e) {
			assumeNoException(e.getLocalizedMessage(), e);
		}
	}
	/**
	 * {@link sample5.Sample5#privateMethod1(java.lang.String)} のためのテスト・メソッド。
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	@Test
	public final void testPrivateMethod1_InvocationTargetException()
			throws NoSuchMethodException, SecurityException, IllegalAccessException {
		Sample5 s5 = new Sample5();
		Method m = null;
		m = Sample5.class.getDeclaredMethod("privateMethod1", String.class);
		m.setAccessible(true);
		//異常ケース
		String rc = null;
		try {
			rc = (String) m.invoke(s5, (String) null);
		} catch (InvocationTargetException e) {
			assumeNoException("target : " + e.getTargetException() + ", msg : " + e.getLocalizedMessage(), e);
		}
	}
}

比較のためpublicなメソッドの呼び出しも混ぜてある。
publicなメソッドなら普通に呼び出せるが、privateなメソッドはクラスの外からは普段見えないものだから・・・
 Method m = {使いたいメソッドのクラス名}.class.getDeclaredMethod(“使いたいメソッドの名前”, 引数1.class, 引数2.class, … );
な感じでprivateなメソッドの情報を取得し、
m.setAccessible(true);
で、privateなメソッドに一時的にクラスの外から使用できるようにしてから・・・
 rc = (戻り値のキャスト) m.invoke(s5, (パラメータ1のキャスト) パラメータ1, (パラメータ2のキャスト) パラメータ2, …);
な感じでメソッドを呼び出す。
しかし、戻り値もパラメータもキャストを使うので、ミスった時はどうなるのか?
調べてみたら・・・ソースが長くなってしまった。
そのcatchの処理も、想定通りのExceptionが通ればいいが最初は大抵想定外な結果になるのでfailではなくassumeNoexceptionを使い、想定内ならIgnore、想定外ならFailになるようにした。
ちょっと判りにくいけど、InvocationTargetExceptionは呼び出し先でExceptionがThrowされた場合に処理されるので、Sample5.javaでパラメータがnullだったらExceptionをThrowしている。

setUpBeforeClass() JUnitを開始します。
testPrivateMethod1_IllegalAccessExceptionのための setUp() テストケースの事前処理を行います。
testPrivateMethod1_IllegalAccessExceptionのための tearDown() テストケースの事後処理を行います。
testPrivateMethod1_IllegalArgumentExceptionのための setUp() テストケースの事前処理を行います。
testPrivateMethod1_IllegalArgumentExceptionのための tearDown() テストケースの事後処理を行います。
testPrivateMethod1_InvocationTargetExceptionのための setUp() テストケースの事前処理を行います。
testPrivateMethod1_InvocationTargetExceptionのための tearDown() テストケースの事後処理を行います。
testPrivateMethod1_NoSuchMethodException_01のための setUp() テストケースの事前処理を行います。
testPrivateMethod1_NoSuchMethodException_01のための tearDown() テストケースの事後処理を行います。
testPrivateMethod1_NoSuchMethodException_02のための setUp() テストケースの事前処理を行います。
testPrivateMethod1_NoSuchMethodException_02のための tearDown() テストケースの事後処理を行います。
testPrivateMethod1_Successのための setUp() テストケースの事前処理を行います。
testPrivateMethod1_Successのための tearDown() テストケースの事後処理を行います。
testPrivateStaticMethod1_IllegalAccessExceptionのための setUp() テストケースの事前処理を行います。
testPrivateStaticMethod1_IllegalAccessExceptionのための tearDown() テストケースの事後処理を行います。
testPrivateStaticMethod1_IllegalArgumentExceptionのための setUp() テストケースの事前処理を行います。
testPrivateStaticMethod1_IllegalArgumentExceptionのための tearDown() テストケースの事後処理を行います。
testPrivateStaticMethod1_InvocationTargetExceptionのための setUp() テストケースの事前処理を行います。
testPrivateStaticMethod1_InvocationTargetExceptionのための tearDown() テストケースの事後処理を行います。
testPrivateStaticMethod1_NoSuchMethodException_01のための setUp() テストケースの事前処理を行います。
testPrivateStaticMethod1_NoSuchMethodException_01のための tearDown() テストケースの事後処理を行います。
testPrivateStaticMethod1_NoSuchMethodException_02のための setUp() テストケースの事前処理を行います。
testPrivateStaticMethod1_NoSuchMethodException_02のための tearDown() テストケースの事後処理を行います。
testPrivateStaticMethod1_Successのための setUp() テストケースの事前処理を行います。
testPrivateStaticMethod1_Successのための tearDown() テストケースの事後処理を行います。
testPublicMethod1_successのための setUp() テストケースの事前処理を行います。
testPublicMethod1_successのための tearDown() テストケースの事後処理を行います。
testPublicStaticMethod1_successのための setUp() テストケースの事前処理を行います。
testPublicStaticMethod1_successのための tearDown() テストケースの事後処理を行います。
tearDownAfterClass() JUnitを終了します

Junitのビューを見るとxxxxx_success は〇、他はIgnoreになっている。
次は mockかな・・・




コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA