注射在Arquillian测试中不起作用

时间:2018-04-28 19:27:40

标签: java integration-testing jboss-arquillian

我正在尝试使用JavaEE做简单的应用程序,目前我仍然坚持使用arquillian测试。我有一个像这样的BookRepository类:

package com.pluralsight.bookstore.repository;

import javax.transaction.Transactional;
import static javax.transaction.Transactional.TxType.SUPPORTS;

@Transactional(SUPPORTS)
public class BookRepository {

}

测试类BookRepositoryTest:

package com.pluralsight.bookstore.repository;

@RunWith(Arquillian.class)
public class BookRepositoryTest {

    @Inject
    private BookRepository bookRepository;

    @Deployment
    public static JavaArchive createDeployment() {
        return ShrinkWrap.create(JavaArchive.class)
                .addClass(BookRepository.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
                .addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml");
    }

    @Test
    public void create() {
        assertNotNull(bookRepository);
    }
}

运行测试结果:

    Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 9.864 sec <<< FAILURE!
    create(com.pluralsight.bookstore.repository.BookRepositoryTest)  Time elapsed: 0.287 sec  <<< ERROR!
    java.lang.NullPointerException
            at com.pluralsight.bookstore.repository.BookRepositoryTest.create(BookRepositoryTest.java:38)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            ...

没有任何解释。当我用assertNotNull检查时,bookRepository总是为null所以我想这里注入不起作用。项目结构如下所示: enter image description here

我试着寻找答案已经有一段时间了,我还在同一个地方。对于容器,我使用wildfly远程运行测试,并让WildFly在后台运行。 在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

好的,所以我终于成功了。显然我使用了错误的'@Inject'注释。我使用的是arquillian,而不是javax。我改变了

import org.jboss.arquillian.core.api.annotation.Inject;

在我的测试用例类中

import javax.inject.Inject;

它就像一个魅力。真的很烦人追踪,实际上我发现它是运气不好所以我希望这将有助于将来的人。

答案 1 :(得分:0)

Wildfly是符合Java EE 7标准的AS,在JavaEE 7中,默认情况下会发现CDI bean,如果注释,请查看Default CDI Enablement in Java EE 7,这可能是您的bean.xml空。 因此要注入BookRepository,您必须添加范围注释(请参阅CDI scopes),以获得等级@ApplicationScoped

@ApplicationScoped
@Transactional(SUPPORTS)
public class BookRepository {

}

或者您必须使用发现模式all包含beans.xml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
       http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

将它放在src / test / resources下,将其包含在您的测试修改中,如下所示:

package com.pluralsight.bookstore.repository; 

@RunWith(Arquillian.class) 
public class BookRepositoryTest { 

    @Inject 
    private BookRepository bookRepository; 

    @Deployment 
    public static JavaArchive createDeployment() { 
         return ShrinkWrap.create(JavaArchive.class) 
                                         .addClass(BookRepository.class) 
                                         .addAsManifestResource("META-INF/beans.xml", "beans.xml")
                                         .addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml"); 
     } 

    //...

}