SAStrutsを勉強してみる (5) - @Mockを使う -

2012-10-06T00:00:00+00:00 Java SAStruts

詳しい事は http://s2container.seasar.org/2.4/ja/S2JUnit4.html#mockAnnotation を読めば良いはずなので省略。一応、これはSAStrutsの機能ではなくS2Containerな機能な模様

SampleTestCase.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.seasar.framework.unit.Seasar2;
import org.seasar.framework.unit.annotation.Mock;
import org.seasar.framework.unit.annotation.Mocks;

import sample.entity.Sample;
import sample.service.SampleService;

import static org.junit.Assert.*;

@RunWith(Seasar2.class)
public class SampleTestCase {

    private SampleService service;

    @Test
    @Mocks(
        @Mock(
            target = SampleService.class,
            pointcut = "getSamples",
            returnValue = "{ new sample.entity.Sample() }"
        )
    )
    public void testAdd() {
        Sample sample = new Sample();
        sample.data = "test";

        assertNotNull(service.createSample(sample));

        // このテストは失敗する
        assertSame(service.getSamples().size(), 4);
    }
}

まぁ上記したURLに詳しい事書いてるので省略。returnValueではなく例外を送出させたい場合には@Mockアノテーションのthrowableに例外を指定すれば良い模様

android.support.v13.dreams.BasicDream SAStrutsを勉強してみる (4) - S2JUnit4でテスト -