【自分用】Junitでのテスト方法

備忘録として残してます

Eclipse4.4
JUnit4
//テスト対象のクラス
public class Sample1 {

    public int publicVariable;
    private int privateVariable;

    //デフォルトコンストラクタ
    public Sample1(){
        this.publicVariable=1;
        this.privateVariable=2;
    }

    //引数ありコンストラクタ
    public Sample1(int a,int b){
        this.publicVariable=a;
        this.privateVariable=b;
    }

    //publicなメソッド
    public int publicMethod(){
        return publicVariable+privateVariable;
    }

    //privateなメソッド
    private int privateMethod(){
        return publicVariable*10+privateVariable;
    }

    //publicな引数ありメソッド
    public int publicMethodWithArgument(int c,int d){
        return c+d;
    }

    //privateな引数ありメソッド
    private int privateMethodWithArgument(int c,int d){
        return c*10+d;
    }
}

テストコード

//テストコード
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.Test;


public class Sample1Test {


    private Sample1 targetClass=new Sample1();

    //testで呼んだメソッドの戻り値
    private int testResult;

    @BeforeClass
    public static void init(){
        System.out.println("テスト開始\n");
    }

    @AfterClass
    public static void exit(){
        System.out.println("テスト終了");
    }

    @Before
    public void setup(){
        System.out.println("テストケースごとに開始前に呼ばれます");
        testResult=0;
    }
    @After
    public void end(){
        System.out.println("テストケースごとに開始後に呼ばれます");
        System.out.println("----------\n");
        testResult=0;
    }

    @Test
    public void test1() {

        testResult=targetClass.publicMethod();
        System.out.println("test1の結果 : "+testResult);

    }

    @Test
    public void test2(){

        try{
            //privateメソッドにアクセスできるようにする
            Method targetMethod=Sample1.class.getDeclaredMethod("privateMethod");
            targetMethod.setAccessible(true);

            testResult=(int)targetMethod.invoke(targetClass);
        }
        catch(NoSuchMethodException e){
            System.out.println(e.toString());
        }
        catch(InvocationTargetException e){
            System.out.println(e.toString());
        }
        catch(IllegalAccessException e){
            System.out.println(e.toString());
        }

        System.out.println("test2の結果 : "+testResult);
    }

    @Test
    public void test3(){

        testResult=targetClass.publicMethodWithArgument(4,5);
        System.out.println("test3の結果 : "+testResult);
    }

    @Test
    public void test4(){

        try{
            //privateメソッドにアクセスできるようにする
            Method targetMethod=Sample1.class.getDeclaredMethod("privateMethodWithArgument",int.class,int.class);
            targetMethod.setAccessible(true);

            testResult=(int)targetMethod.invoke(targetClass,4,5);
        }
        catch(NoSuchMethodException e){
            System.out.println(e.toString());
        }
        catch(InvocationTargetException e){
            System.out.println(e.toString());
        }
        catch(IllegalAccessException e){
            System.out.println(e.toString());
        }

        System.out.println("test4の結果 : "+testResult);
    }
}

実行結果

テスト開始

テストケースごとに開始前に呼ばれます
test1の結果 : 3
テストケースごとに開始後に呼ばれます
----------

テストケースごとに開始前に呼ばれます
test2の結果 : 12
テストケースごとに開始後に呼ばれます
----------

テストケースごとに開始前に呼ばれます
test3の結果 : 9
テストケースごとに開始後に呼ばれます
----------

テストケースごとに開始前に呼ばれます
test4の結果 : 45
テストケースごとに開始後に呼ばれます
----------

テスト終了