Junit 在验证器中注入CDIBean可以工作,但会破坏arquillian测试

Junit 在验证器中注入CDIBean可以工作,但会破坏arquillian测试,junit,dependency-injection,cdi,jboss-arquillian,Junit,Dependency Injection,Cdi,Jboss Arquillian,我试着在我的验证器中注入一个bean,但没有成功。然后我在谷歌上搜索了一下,发现: 我应用了这个方法,它确实有效。它编译并完成任务。但是当我运行我的测试(与arquillian合作)时,我会得到以下stacktrace(我的代码可以在下面找到): 我的测试: public class LocaleBeanTest extends AbstractArquillianTest { @Inject private LocaleBean localeBean; @Test public void

我试着在我的验证器中注入一个bean,但没有成功。然后我在谷歌上搜索了一下,发现:

我应用了这个方法,它确实有效。它编译并完成任务。但是当我运行我的测试(与arquillian合作)时,我会得到以下stacktrace(我的代码可以在下面找到):


我的测试:

public class LocaleBeanTest extends AbstractArquillianTest {
@Inject
private LocaleBean localeBean;

@Test
public void testChangeLocaleEN() {
    localeBean.changeLocale("en");
    assertEquals(localeBean.getLocale().getLanguage(), "en");
    }
}


@RunWith(Arquillian.class)
public abstract class AbstractArquillianTest {

    @PersistenceContext
    public EntityManager entityManager;

    public static EntityManagerFactory emf;

    static Context context;

    @Resource
    public static UserTransaction tx;

    @Deployment
    public static JavaArchive initializeEjb() {
        JavaArchive archive = ShrinkWrap.create(JavaArchive.class)
                .addPackages(true, "com.rd.rdtravel")
                .addAsResource("META-INF/persistence.xml")
                .addAsResource("META-INF/beans.xml")
                .addAsResource("import.sql");

        System.out.println(archive.toString(true));

        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        properties.put("jboss.naming.client.ejb.context", true);
        properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        properties.put(Context.PROVIDER_URL, "remote://localhost:4447");
        properties.put(Context.SECURITY_PRINCIPAL, "root");
        properties.put(Context.SECURITY_CREDENTIALS, "rootroot");
        try {
            context = new InitialContext(properties);
        } catch (NamingException e) {
            e.printStackTrace();
        }

        return archive;
    }
}

看起来您的部署归档中缺少了BookBean所依赖的东西,可能是一些外部库

如果将arquillian.xml中的deploymentExportPath设置为适当的路径,则可以检查归档文件的内容

比如:


目标/阿奎利安
package com.rd.rdtravel.Validator;

//This annotations are needed to support injection in validators. This isn't supported by default.
@Named
@RequestScoped
public class NumberOfPersonsValidator implements Validator {
@Inject
BookBean bookBean;

@Override
public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
    int numberOfPersons = (Integer) value;
    if (bookBean.getSelectedTrip().getAvailablePlaces() < numberOfPersons) {
        FacesMessage message = new FacesMessage();
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        message.setSummary("Trip can't have that many passengers.");
        message.setDetail("Trip can't have that many passengers.");
        facesContext.addMessage(null, message);
        throw new ValidatorException(message);
        }
    }
}
@SessionScoped
@Named
public class BookBean implements Serializable {

//Some methodes and properties. Don't think they are important for this question.
}
public class LocaleBeanTest extends AbstractArquillianTest {
@Inject
private LocaleBean localeBean;

@Test
public void testChangeLocaleEN() {
    localeBean.changeLocale("en");
    assertEquals(localeBean.getLocale().getLanguage(), "en");
    }
}


@RunWith(Arquillian.class)
public abstract class AbstractArquillianTest {

    @PersistenceContext
    public EntityManager entityManager;

    public static EntityManagerFactory emf;

    static Context context;

    @Resource
    public static UserTransaction tx;

    @Deployment
    public static JavaArchive initializeEjb() {
        JavaArchive archive = ShrinkWrap.create(JavaArchive.class)
                .addPackages(true, "com.rd.rdtravel")
                .addAsResource("META-INF/persistence.xml")
                .addAsResource("META-INF/beans.xml")
                .addAsResource("import.sql");

        System.out.println(archive.toString(true));

        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        properties.put("jboss.naming.client.ejb.context", true);
        properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        properties.put(Context.PROVIDER_URL, "remote://localhost:4447");
        properties.put(Context.SECURITY_PRINCIPAL, "root");
        properties.put(Context.SECURITY_CREDENTIALS, "rootroot");
        try {
            context = new InitialContext(properties);
        } catch (NamingException e) {
            e.printStackTrace();
        }

        return archive;
    }
}
<engine>
   <property name="deploymentExportPath">target/arquillian</property>
</engine>