ログ日記

作業ログと日記とメモ

S2Wicket利用時のテストケース

WicketSeasar2でのテストの書き方メモ。
S2WicketのS2WicketFilterを使っている。
HTMLファイルをWEB-INFなどに入れている場合はMarkupNotFoundExceptionが発生するのでEclipseJUnitのプロパティで作業ディレクトリをsrc/main/webappなどにしておく。

public class UserPageTest extends S2TestCase {
    private WicketTester tester;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        include("app.dicon");
        tester = new WicketTester(new MyWebApp());
        tester.getApplication().getComponentInstantiationListeners().add(new ComponentInjectionListener());
    }

    public void login(){

    }
    public void testLogin(){
        tester.startPage(UserPage.class);
        tester.assertRenderedPage(LoginPage.class);

        FormTester form = tester.newFormTester("entity.account");
        assertNotNull(form);
        form.setValue("id", "test");
        form.setValue("password", "test");
        form.submit();

        tester.assertRenderedPage(UserPage.class);
    }
}


EasyMockを使ってログイン処理をMock化。

public class UserPageTest extends S2TigerTestCase {
    private WicketTester tester;

    @EasyMock(register=true)
    private AccountService accountService;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        include("app.dicon");
        tester = new WicketTester(new MyWebApp());
        tester.getApplication().getComponentInstantiationListeners().add(new ComponentInjectionListener());
    }

    private Account setUpAccount(String[] roles){
        Account account = new Account();
        account.accountId = 1;
        account.enable = true;
        account.name = "admin";
        account.loginId = "admin";

        account.accountRoleList = new ArrayList<AccountRole>();
        for (String v: roles){
            AccountRole role = new AccountRole();
            role.accountId = account.accountId;
            role.account = account;
            role.roleValue = v;
            account.accountRoleList.add(role);
        }
        return account;
    }

    public void recordLogin(){
        Account account = setUpAccount(new String[]{"admin", "user"});
        expect(accountService.login("admin", "admin")).andReturn(account);
        expect(accountService.findById(1)).andReturn(account);
    }

    public void testLogin(){
        tester.startPage(UserPage.class);
        tester.assertRenderedPage(LoginPage.class);

        FormTester form = tester.newFormTester("entity.account");
        assertNotNull(form);
        form.setValue("id", "admin");
        form.setValue("password", "admin");
        form.submit();

        tester.assertRenderedPage(UserPage.class);
    }
}


追記:テストをぽちぽち追加していくと、うまく動かないことが度々ある。今は見送り。