2013-10-12 35 views
1

我想使用文檔中描述的方法在我的Struts2操作上運行一個簡單的測試。然而,測試類沒有實例化,而是給了我以下異常:Struts2單元測試給出的錯誤:在類文件中不是本地或抽象的方法中的缺少代碼屬性javax/servlet/ServletException

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,以便單元測試能夠運行?

回答

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); 
    } 
} 
相關問題