【问题标题】:Struts2 Unit test gives the error: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletExceptionStruts2 单元测试给出错误:类文件 javax/servlet/ServletException 中非本机或抽象方法中的缺失代码属性
【发布时间】:2013-10-12 04:27:48
【问题描述】:

我正在尝试使用文档中描述的方法对我的 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 以便运行单元测试?

【问题讨论】:

    标签: java unit-testing maven struts2 junit


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2013-04-03
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      相关资源
      最近更新 更多