1

次の Struts アクションがあります。

    public ActionForward addSomething(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    SomeForm sForm = (SomeForm) form;        
    Test t = testForm.toTest();

    if (tDao.checkExistsTest(t.getTest())) {            
        return mapping.findForward("failure");          
    } else {
        t.setType(new TestType(tess));
        t.setPassword(testForm.getPassword());

        tDao.add(t);

        return mapping.findForward("success");
    }        
}

testAddSomethingSuccessメソッドをテストするために、次のコードを実行しました。

    @Test
public void testAddSomethingSuccess() throws Exception {
    form.setTest("LOL");
    form.setTestName("lol");
    form.setPassword("12345");

    ActionForward forward = action.addSomething(mapping, form, request, response);

    assertEquals(tDao.getList().get(0).getTest(), "LOL");
    assertEquals(tDao.getList().get(0).getTestName(), "lol");
    assertEquals(tDao.getList().get(0).getPassword(), "12345");

    assertEquals("success", forward.getName());
}

どのようにtestAddClientFailed()を実装できますか??? :

    @Test
public void testAddSomethingFailed() throws Exception {
    form.setTestName("lol");
    t.checkIfExists("lol");


    ActionForward forward = action.addSomething(mapping, form, request, response);

    assertEquals("failure", forward.getName());
}
4

2 に答える 2

0

tDaoモックしてcheckExistsTestreturn falseにする必要があります。

メソッドを持つクラスで

   public ActionForward addSomething(...)

(コンストラクターを介して)注入するか、セッターメソッドを介して設定する必要があります。そのメソッドの実装はTDao、そのメソッドに対してfalseを返します。

FakeDao implements TDao {
 public boolean checkExistsTest(...) {return false;}
}

または具体的な DAO をサブクラス化し、このメソッドをオーバーライドします。

また、モック フレームワークを使用して、 JMockMockitoなどの実装を提供することもできます。

于 2012-06-14T16:01:59.250 に答える
0

具体的またはモックを介して を返す DAO を実装することにより、false現在checkExistsTest行っているように転送名を確認します。他に何がありますか?

これが、依存性注入/制御の反転が非常に役立つ理由の 1 つです。

テスト方法、DAO のインスタンス化方法などについて何も知らずに。具体的な支援を提供することは困難です。最終的に (できれば明らかに) 失敗をテストするには、失敗する必要があります。

于 2012-06-14T16:02:06.853 に答える