Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单元测试支柱_Java_Unit Testing_Struts - Fatal编程技术网

Java 单元测试支柱

Java 单元测试支柱,java,unit-testing,struts,Java,Unit Testing,Struts,我有以下行动: public ActionForward addSomething(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { SomeForm sForm = (SomeForm) form; Test t = testForm.toTest(); if

我有以下行动:

    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方法:

如何实现testAddClientFailed()?:


您需要模拟
tDao
并使
checkexistest
返回false

在你们班上哪一个有这个方法

   public ActionForward addSomething(...)
您应该(通过构造函数)注入或通过setter方法设置
TDao
的实现,该方法返回false,例如

FakeDao implements TDao {
 public boolean checkExistsTest(...) {return false;}
}
或者将具体的DAO子类化并重写此方法


您还可以使用模拟框架来提供类似或的实现。

通过实现DAO(具体的或通过模拟),该DAO为
checkExistTest
返回
false
,并像现在一样检查转发名称——还有什么

这就是为什么依赖注入/控制反转如此有用的原因之一


如果不知道您是如何测试的,DAO是如何实例化的等等,就很难提供具体的帮助。最终(很明显,希望如此)要测试失败,你必须失败。

好吧,我照你说的做了,但我仍然不能参加测试。我不知道为什么:s
   public ActionForward addSomething(...)
FakeDao implements TDao {
 public boolean checkExistsTest(...) {return false;}
}