2010-09-30 34 views
6

當我試圖單元測試處理JSF組件的Java類(javax.faces.model.SelectItem)時,出現一個奇怪的錯誤。我得到的錯誤是這樣的:單元測試錯誤(JSF):非本地或抽象方法中的缺少代碼屬性

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/model/SelectItem 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:616) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) 
     at java.net.URLClassLoader.access$000(URLClassLoader.java:58) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:197) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:307) 
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:248) 
     ... 

然後,它達到我的代碼(在ItemOptionsHandler.java):
的SelectItem []項目=新的SelectItem [itemList.size()];

這是什麼錯誤?

感謝您的幫助!

這是我想測試類:

package foo.web.converters; 

import java.io.Serializable; 
import java.util.List; 
import javax.ejb.EJB; 
import javax.faces.model.SelectItem; 

import foo.business.facade.ItemFacade; 
import foo.model.MyType; 


public class ItemOptionsHandler implements Serializable { 

    @EJB 
    private ItemFacade facade; 

    public void setFacade(ItemFacade facade) { 
     this.facade = facade; 
    } 

    public SelectItem[] getSupplierItems() { 

     List<MyType> itemList = facade.getSupplierItems(); 
     SelectItem[] items = new SelectItem[itemList.size()]; 
     int i = 0; 

     // more stuff 

     return items; 
    } 
} 

這是測試:

package foo.web.converters; 

import java.util.ArrayList; 
import java.util.List; 
import javax.faces.model.SelectItem; 
import org.junit.After; 
import org.junit.AfterClass; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.Test; 
import static org.junit.Assert.*; 
import foo.facade.ItemFacade; 
import foo.model.MyModel; 

public class ItemOptionsHandlerTest { 

    private ItemOptionsHandler instance = null; 

    public ItemOptionsHandlerTest() { 
    } 

    @BeforeClass 
    public static void setUpClass() throws Exception { 
    } 

    @AfterClass 
    public static void tearDownClass() throws Exception { 
    } 

    @Before 
    public void setUp() { 
     instance = new ItemOptionsHandler(); 
     instance.setFacade(new BusinessFacadeTemp()); 
    } 

    @After 
    public void tearDown() { 
    } 

    @Test 
    public void testGetSupplierItems() { 
     System.out.println("getSupplierGroups"); 
     SelectItem[] expResult = null; 
     SelectItem[] result = instance.getSupplierItems(); 
     assertEquals(expResult, result); 
    } 

    private class BusinessFacadeTemp implements ItemFacade { 
     @Override 
     public List<MyTest> getSupplierItems() { 
     return null; 
     } 


    } 
} 

下面是一些依賴關係:

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


     <dependency> 
     <groupId>javax.annotation</groupId> 
     <artifactId>jsr250-api</artifactId> 
     <version>1.0</version> 
     </dependency> 

     <dependency> 
     <groupId>javax.faces</groupId> 
     <artifactId>jsf-api</artifactId> 
     <version>1.2</version> 
     </dependency> 

     <dependency> 
     <groupId>javax.faces</groupId> 
     <artifactId>jsf-impl</artifactId> 
     <version>1.2-b19</version> 
     </dependency> 

回答

3

你使用maven解決依賴關係?如果是這樣,這裏似乎是一個答案:http://weblogs.java.net/blog/ludo/archive/2007/01/java_ee_5_apis.html

您可以下載.jar和使用它從你的硬盤:

<dependency> 
    <groupId>javax</groupId> 
    <artifactId>javaee-web-api</artifactId> 
    <version>6.0</version> 
    <scope>provided</scope> 
    <systemPath>/path/to/your/jar</systemPath> 
</dependency> 
+0

是的我使用組織內中央檔案庫中的Maven。我在上面添加了一些我的依賴關係。 – Jojje 2010-10-01 07:44:04

+0

你推薦做什麼? – Jojje 2010-10-01 07:45:15

+0

我認爲你應該下載缺少的jar並使用它,而不是從maven倉庫。 – amorfis 2010-10-01 08:05:03

1

如果你只需要Servlet規範2.5,你可以使用這個:

<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>servlet-api</artifactId> 
    <version>2.5</version> 
</dependency> 

See Megadix

1

與JBoss 6.3的工作我已經與這個詞條添加到我的pom.xml

<dependency> 
     <groupId>org.jboss.spec.javax.faces</groupId> 
     <artifactId>jboss-jsf-api_2.1_spec</artifactId> 
     <version>2.1.29.Final</version> 
     <scope>test</scope> 
    </dependency> 
相關問題