繁体   English   中英

Struts2单元测试给出错误:类文件javax / servlet / ServletException中不是本机或抽象的方法中的缺少Code属性

[英]Struts2 Unit test gives the error: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException

我正在尝试使用文档中描述的方法对Struts2动作进行简单的测试。 但是,测试类未实例化,而是给了我以下异常:

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

这是我的测试课的代码:

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("/");
    }

}

我究竟做错了什么?

编辑:在我的pom.xml中,我有以下内容:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

我猜这是导致此问题的原因,即当我只是在IDE中而不是通过Web浏览器运行单元测试时,jar的下载或包含的不正确吗? 我如何自己包含这个jar,以便运行单元测试?

将以下内容添加到我的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>

另外,对于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM