如何为JSF类编写Arquillian测试

时间:2012-08-07 08:24:27

标签: java jsf java-ee jboss jboss-arquillian

我正在尝试对源自JSF API的数据模型执行arquillian测试。我收到了这个错误:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.046 sec <<< FAILURE!
initializationError(mypackage.GenericLazyModelTest)  Time elapsed: 0.003 sec  <<< ERROR!
java.lang.ClassFormatError: Absent Code attribute in method that is not native 
or abstract in class file javax/faces/model/DataModel

简单的arquillian测试,不涉及JSF,但JPA和EJB API运行良好 对网络的研究表明,一个常见的原因是使用sun {s}}和here所描述的存根EE API。

我绝对不会使用它们。这是我pom的依赖部分:

  <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.jboss.bom</groupId>
          <artifactId>jboss-javaee-6.0-with-tools</artifactId>
          <version>${javaee6.with.tools.version}</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
   </dependencyManagement>

<dependency>
  <groupId>javax.enterprise</groupId>
  <artifactId>cdi-api</artifactId>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>javax.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.jboss.spec.javax.annotation</groupId>
  <artifactId>jboss-annotations-api_1.1_spec</artifactId>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.jboss.spec.javax.ws.rs</groupId>
  <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.hibernate.javax.persistence</groupId>
  <artifactId>hibernate-jpa-2.0-api</artifactId>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.jboss.spec.javax.ejb</groupId>
  <artifactId>jboss-ejb-api_3.1_spec</artifactId>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.2.0.Final</version>
  <scope>provided</scope>
  <exclusions>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jpamodelgen</artifactId>
  <version>1.1.1.Final</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.primefaces</groupId>
  <artifactId>primefaces</artifactId>
  <version>3.3.1</version>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>

<dependency>
  <groupId>org.jboss.arquillian.junit</groupId>
  <artifactId>arquillian-junit-container</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.jboss.arquillian.protocol</groupId>
  <artifactId>arquillian-protocol-servlet</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.jboss.shrinkwrap.resolver</groupId>
  <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
  <scope>test</scope>
</dependency>

我还尝试使用内部JBoss JSF API进行测试,但不会对错误产生影响:

 <dependency> 
   <groupId>org.jboss.spec.javax.faces</groupId> 
   <artifactId>jboss-jsf-api_2.1_spec</artifactId> 
   <version>2.0.0.Beta1</version> 
   <scope>test</scope> 
 </dependency> 

我的模型源自主要LazyDataModel

public class GenericLazyModel<T> extends LazyDataModel<T>

LazyDataModel依次来自javax.faces.model.DataModel

public abstract class LazyDataModel<T> extends DataModel<T> implements SelectableDataModel<T>, Serializable

测试大多是空的,我要求注入我的模型并检查null

编辑 ShrinkWrap初始化现在包括faces-config.xml,由stefanglase提出。虽然没有改变错误输出。

   @RunWith(Arquillian.class)
    public class GenericLazyModelTest {

        @Deployment
        public static Archive<?> createTestArchive() {

          return ShrinkWrap
            .create(WebArchive.class, "genericLazyModelTest.war")
                .addClasses(GenericLazyModel.class, Submitter.class, SubmitterPK.class,
                            Ordering.class)
                .addClasses(GenericDao.class, Resources.class)
                .addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(new StringAsset("<faces-config version=\"2.0\"/>"), "faces-config.xml")                            
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
        }

        @Inject
        GenericLazyModel<Submitter> model;

        @Before
        public void before() {
            Assert.assertNotNull(model);
            model.setKlazz(Submitter.class);
        }

我在Jboss 7.1.0.Final上运行它。
关于什么可能是问题以及如何解决它的任何想法?

谢谢

1 个答案:

答案 0 :(得分:1)

您在部署中缺少faces-config.xml。 Java EE 6规范要求faces-config.xml目录中存在WEB-INF描述符以触发JSF。

与您已经包含的beans.xml不同,faces-config.xml描述符不能是空文件。它必须至少包含根节点和version属性,以指定正在使用的JSF版本。

因此,您需要将以下代码添加到ShrinkWrap构建器:

.addAsWebInfResource(new StringAsset("<faces-config version=\"2.0\"/>"), "faces-config.xml");