[英]Struts2 Unit test gives the error: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
I'm trying to run a simple test on my Struts2 action using the method described in the documentation. 我正在尝试使用文档中描述的方法对Struts2动作进行简单的测试。 However, the test class is not instantiated, instead it gives me the following exception:
但是,测试类未实例化,而是给了我以下异常:
Results :
Tests in error:
initializationError(net.myorg.myProj.actions.HomeTest): Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
Here is the code of my test class: 这是我的测试课的代码:
import com.opensymphony.xwork2.ActionProxy;
import org.apache.struts2.StrutsTestCase;
import org.junit.Test;
import static org.junit.Assert.*;
public class HomeTest extends StrutsTestCase
{
@Test
public void testExecute() throws Exception
{
System.out.println("getting proxy");
ActionProxy proxy = getProxy();
System.out.println("got proxy");
String result = proxy.execute();
System.out.println("got result: " + result);
assertEquals("Landing", result, "landing");
}
private ActionProxy getProxy()
{
return getActionProxy("/");
}
}
What am I doing wrong? 我究竟做错了什么?
Edit: In my pom.xml, I've got the following: 编辑:在我的pom.xml中,我有以下内容:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
I'm guessing that this is causing this problem, ie the jar is not being properly downloaded or included when I just run the unit test from within the IDE rather than via the web browser? 我猜这是导致此问题的原因,即当我只是在IDE中而不是通过Web浏览器运行单元测试时,jar的下载或包含的不正确吗? How can I include this jar myself so the unit tests will run?
我如何自己包含这个jar,以便运行单元测试?
Adding the following to my pom.xml fixed this: 将以下内容添加到我的pom.xml中可解决此问题:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<type>jar</type>
<scope>test</scope>
</dependency>
In addition, for JUnit4, I had to rewrite my test as the following: 另外,对于JUnit4,我必须将测试重写如下:
import com.opensymphony.xwork2.ActionProxy;
import org.apache.struts2.StrutsJUnit4TestCase;
import org.junit.Test;
import static org.junit.Assert.*;
public class HomeTest extends StrutsJUnit4TestCase<Home>
{
@Test
public void testExecute() throws Exception
{
ActionProxy proxy = getActionProxy("/home");
String result = proxy.execute();
assertEquals("Landing", "landing", result);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.