1

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

4

1 回答 1

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);
    }
}
于 2013-10-13T01:05:53.940 回答