简体   繁体   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

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.

相关问题 类文件javax / servlet / ServletException中不是本机或抽象的方法中的Embedded Jetty Absent Code属性 - Embedded Jetty Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException 方法中的Absent Code属性在类文件javax / servlet / ServletException中不是本机的或抽象的 - Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException JPA ClassFormat 错误“类文件 javax/persistence/Persistence 中非本机或抽象的方法中缺少代码属性” - JPA ClassFormat Error "Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence" 在类文件javax / faces / webapp / FacesServlet中的非本机或抽象方法中的Absent Code属性 - Absent Code attribute in method that is not native or abstract in class file javax/faces/webapp/FacesServlet Java EE 6中类文件javax / mail / MessagingException中不是本机或抽象的方法中的缺少Code属性 - Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException in Java EE 6 ClassFormatError:在类文件 javax/mail/MessagingException 中非本机或抽象的方法中缺少代码属性 - ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException class 文件 javax/validation/ConstraintViolationException 中非本机或抽象方法中的缺失代码属性 - Absent Code attribute in method that is not native or abstract in class file javax/validation/ConstraintViolationException java.lang.ClassFormatError:类文件javax / faces / FacesException中不是本机或抽象的方法中的缺少Code属性 - java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/FacesException java.lang.ClassFormatError:在类文件 javax/mail/MessagingException 中非本机或抽象的方法中缺少代码属性 - java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException 类文件javax / transaction / SystemException中不是本机或抽象的方法中的缺少Code属性 - Absent Code attribute in method that is not native or abstract in class file javax/transaction/SystemException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM