Arquillian在EJB中注入Weld Bean为空

时间:2016-10-31 10:16:08

标签: java jboss-arquillian weld

我成功地使用Arquillian针对(较旧的)Glassfish 3.1嵌入式运行EJB测试。

当尝试@Inject焊接bean时,我想在传递注入的Weld bean中解决方法时得到一个NPE。

所以,我正在测试EJB A. EJB A有一个EJB B的参考.EJB有一个参考Weld bean的参考。

这是Jar init。

@Deployment
public static Archive<?> createDeployment() {
    return ShrinkWrap.create(JavaArchive.class, "itscool.jar")
            .addClasses(DashboardService.class, PropertyService.class, StorageService.class, EPSRepository.class,
                    EmailService.class, NotificationRepository.class, EmailRepository.class, Configuration.class,
                    MailingList.class, BogusBean.class, Bogus.class)
            .addAsManifestResource("META-INF/persistence.xml", "persistence.xml")
            .addAsManifestResource(EmptyAsset.INSTANCE, "META-INF/beans.xml")
            .addAsResource(new File("src/test/resources/csman.properties"), "csman.properties");

}

因此,注入的bean是Configuration.class。首先,我认为Configuration impl类中存在一些初始化错误,因此我创建了@Bogus和BogusBean以仅测试注入。我得到了同样的错误。

@Bogus
public class BogusBean {

    public void setIt(){
    }
}

和资格赛

@Documented
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Bogus {
}

在EmailService中注入(@stateless会话bean)。

@Inject
@Bogus
private BogusBean bogus;

@EJB
private EmailRepository emailRepos;

@PostConstruct
public void init() {
    bogus.setIt();
}

NPE发生在bogus.setIt()行上。 as bogus == null。

这是maven配置

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

    <dependency>
        <groupId>org.jboss.arquillian.core</groupId>
        <artifactId>arquillian-core-api</artifactId>
        <version>1.1.11.Final</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.weld.arquillian.container</groupId>
        <artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
        <version>1.1.2.Final</version>
    </dependency>

我为glassfish嵌入

添加了一个配置文件
<profile>
        <id>arquillian-glassfish-embedded</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian.container</groupId>
                <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
                <version>1.0.0.Final</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish.main.extras</groupId>
                <artifactId>glassfish-embedded-all</artifactId>
                <version>3.1.2.2</version>
                <!--  was 4.0 -->
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-client</artifactId>
                <version>2.23.2</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet</artifactId>
                <version>2.23.2</version>
                <scope>provided</scope>
            </dependency>

        </dependencies>
    </profile>

我也试过从焊接到焊接,但是容器甚至没有启动。 (如此处所述):how-to-test-ejb-method-call-with-arquillian-weld-ee-embedded

0 个答案:

没有答案