我有一个对Struts 2操作的Junit 3.8测试,它运行在我的工作区中没有任何问题(来自eclipse:右键单击> runs > junit )。
为此,我使用两个插件:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>2.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.8</version>
</dependency>下面是测试类:
package com.myapp.user.my;
import org.apache.struts2.StrutsSpringTestCase;
import com.myapp.user.action.UserAction;
import com.opensymphony.xwork2.ActionProxy;
public class TestAccountActionUsingStrutsTestCase extends StrutsSpringTestCase {
public void testUserNameErrorMessage() throws Exception {
request.setParameter("userBean.userName", "Bruc");
request.setParameter("userBean.password", "test");
ActionProxy proxy = getActionProxy("/userAction");
UserAction userAction = (UserAction) proxy.getAction();
proxy.execute();
assertTrue("Problem There were no errors present in fieldErrors but there should have been one error present", userAction.getFieldErrors().size() == 1);
assertTrue("Problem field user.userName not present in fieldErrors but it should have been",
userAction.getFieldErrors().containsKey("userBean.userName") );
System.out.println("Finish 1 test.");
}
}接下来,我尝试调用这个测试,这次是从一个web应用程序( JSF )中调用的。
下面是我尝试这样做的代码(我从托管bean调用以下runTest()方法):
import java.util.List;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import com.myapp.user.my.TestAccountActionUsingStrutsTestCase;
public class CallStrutsActionExecuteThruTest {
public void runTest(){
System.out.println("CallStrutsActionExecuteThruTest.runTest() is executed.");
TestAccountActionUsingStrutsTestCase test = new TestAccountActionUsingStrutsTestCase();
JUnitCore jUnitCore = new JUnitCore();
Result result = jUnitCore.run(test);
List<Failure> list = result.getFailures();
for (Failure failure : list) {
System.out.println(failure.getMessage());
}
System.out.println("Test done!");
}
}当我访问托管bean时,我可以看到调用了runTest()。第一个输出CallStrutsActionExecuteThruTest.runTest() is executed.被打印到控制台。奇怪的是,下一个输出没有打印到控制台,尽管调试器显示它们被执行。
另外,result.getFailures()返回一个带有一个元素的列表。正如我说过的,由于某种原因,它的failure.getMessage()没有打印到控制台,但是当我在调试器中看到它时,它的值是failure.getMessage()。
*即使在我的测试类中只有一种方法时:
public void testTrue() throws Exception {
System.out.println("inside testTrue().");
assertTrue(true);
}我还是得到了同样的结果。
我的问题是,
TestCase.fname?首先,在我的测试类中看不到设置此值的方法。其次,据我理解,fanme是我想要调用的测试类中的测试方法的名称;jUnitCore.run(test)应该调用测试类test中的所有测试方法,那么我如何使用一个fname参数来指定所有这些方法呢?下载-您可以下载我的项目这里。我使用Maven、Eclipse并在JBoss7上部署。
通过:http://localhost:8080/Struts2WithSpringDIIntegrationExampleJunitFromUI-1.0-SNAPSHOT/xhtml/hello.jsf访问JSF
发布于 2013-04-14 14:35:27
由于某些原因,Struts2测试不能使用原始参数。使用parameterMap代替。
Map<String, String[]> parameterMap = new HashMap<String, String[]>();
parameterMap.put("userBean.userName", new String[]{"Bruc"});
parameterMap.put("userBean.password", new String[]{"test"});
StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
request.setupGetServletPath("/userAction");
request.setParameterMap(parameterMap);https://stackoverflow.com/questions/15999929
复制相似问题