Unit test error (JSF): Absent Code attribute in method that is not native or abstract
Asked Answered
G

3

6

I get a strange error when Im trying to unit test a Java class dealing with JSF components (javax.faces.model.SelectItem). The error I get is this:

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)
        ...

It then reaches my code (in ItemOptionsHandler.java):
SelectItem[] items = new SelectItem[itemList.size()];

What's this error all about???

Thankful for help!

This is the class I want to test:

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;
   }
}

This is the test:

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;
      }


   }
}

Here are some of the dependencies:

 <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>
Genip answered 30/9, 2010 at 11:59 Comment(0)
E
3

Are you using maven to resolve dependencies? If so, here seems to be an answer: http://weblogs.java.net/blog/ludo/archive/2007/01/java_ee_5_apis.html

You can download .jar and use it from your disk:

<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-web-api</artifactId>
  <version>6.0</version>
  <scope>provided</scope>
  <systemPath>/path/to/your/jar</systemPath>
</dependency>
Elfish answered 30/9, 2010 at 14:34 Comment(6)
Yes I am using Maven from a central archive within the organization. I addded some of my dependecies above.Genip
I think you should download missing jar and use this, not from maven repository.Elfish
Just to put the missing jar in my local .m2 repos ?Genip
POM Location: /Users/.../Documents/projekt/projname/trunk/webproject/pom.xml Validation Messages: [0] For dependency Dependency {groupId=javax, artifactId=javaee-web-api, version=6.0, type=jar}: only dependency with system scope can specify systemPath.Genip
Yes, it complies now.. but the test still fails ... ;) sorry these maven thingies are really not my thing...Genip
If you still have this problem, here is the solution: adam-bien.com/roller/abien/entry/trouble_with_crippled_java_eeElfish
F
1

If you need just servlet spec 2.5 you can use this:

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

See Megadix

Formyl answered 8/4, 2011 at 13:28 Comment(0)
M
1

Working with JBoss 6.3 I've had to add this entry to my 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>
Misericord answered 28/1, 2015 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.