当前位置: 代码迷 >> Java Web开发 >> strutsTestCase无法测试ssh中的struts
  详细解决方案

strutsTestCase无法测试ssh中的struts

热度:82   发布时间:2016-04-16 21:25:34.0
求助:strutsTestCase无法测试ssh中的struts
最近应导师要求,学习strutsTestCase来测试struts中的action,可是问题多多。
我先是创建单独struts项目,测试可以通过。
再创建struts+hibernate项目,测试也可以通过。
可是在测试SSH时,却有问题。
这个是错误:
junit.framework.AssertionFailedError: received error 404 : Invalid path was requested
at servletunit.HttpServletResponseSimulator.sendError(HttpServletResponseSimulator.java:463)
at org.apache.struts.action.RequestProcessor.processMapping(RequestProcessor.java:677)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:191)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:394)
at com.fang.mytest.TestLoginAction.testSuccessLogin(TestLoginAction.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我的测试类,如下:
package com.fang.mytest;

import java.io.File;

import servletunit.struts.MockStrutsTestCase;

public class TestLoginAction extends MockStrutsTestCase{
public void setUp() throws Exception {
        super.setUp();
        String filePath = "E:/Workspaces/sshunittest/WebRoot";
File web = new File(filePath );
        this.setContextDirectory(web);
        setServletConfigFile(filePath+"/WEB-INF/web.xml");
//        setConfigFile(filePath+"/WEB-INF/web.xml");
        setConfigFile(filePath+"/WEB-INF/struts-config.xml");
        setConfigFile(filePath+"/WEB-INF/applicationContext.xml");
    }

    public void tearDown() throws Exception {
        super.tearDown();
    }

    public TestLoginAction(String testName) {
        super(testName);
    }

    public void testSuccessLogin() {
        setRequestPathInfo("/login");
        addRequestParameter("username","test");
        addRequestParameter("password","123456");
        actionPerform();
        verifyForward("success");
    }

    public void testFailLogin() {
        setRequestPathInfo("/login");
        addRequestParameter("username","smail");
        addRequestParameter("password","123456");
        actionPerform();
        verifyForward("/form/login.jsp");
    }
}
------解决思路----------------------
路径不正确
------解决思路----------------------
path写绝对路径/WEB-INF/开头