生产者是由Weld在Arquillian过程中发现的

时间:2015-09-20 08:07:08

标签: cdi jboss-arquillian glassfish-embedded weld2

使用Glassfish嵌入式插件使用Arquillian运行单元测试时,我收到以下CDI错误:

2015-09-18 06:25:24,376 DEBUG | main | org.jboss.weld.Reflection                               | WELD-000620: interface com.SupportedLocales is not declared @Target(METHOD, FIELD, PARAMETER, TYPE). Weld will use this annotation, however this may make the application unportable.  
sept. 18, 2015 6:25:24 AM com.sun.enterprise.v3.server.ApplicationLifecycle deploy  
GRAVE: Exception during lifecycle processing  
org.glassfish.deployment.common.DeploymentException: CDI deployment failure:WELD-001408: Unsatisfied dependencies for type Set<Locale> with qualifiers @SupportedLocales  
  at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject protected com.MyClass(@SupportedLocales Set<Locale>)  
  at com.MyClass.<init>(MyClass.java:0)  

使用限定符@SupportedLocales的Set(Locale)在已测试的WebArchive中部署的模块中定义。档案内容是:

/WEB-INF/
/WEB-INF/lib/
/WEB-INF/lib/commons-lang3-3.3.2.jar
/WEB-INF/lib/commons-configuration-1.10.jar
/WEB-INF/lib/reflections-0.9.9-RC2.jar
/WEB-INF/lib/jcl-over-slf4j-1.7.10.jar
/WEB-INF/lib/slf4j-api-1.7.10.jar
/WEB-INF/lib/deltaspike-core-api-1.5.0.jar
/WEB-INF/lib/commons-util-1.0.0-SNAPSHOT.jar
/WEB-INF/lib/commons-io-2.4.jar
/WEB-INF/lib/guava-16.0.1.jar
/WEB-INF/lib/log4j-over-slf4j-1.7.10.jar
/WEB-INF/lib/javassist-3.18.2-GA.jar
/WEB-INF/lib/logback-classic-1.1.2.jar
/WEB-INF/lib/logback-core-1.1.2.jar
/WEB-INF/lib/jul-to-slf4j-1.7.10.jar
/WEB-INF/lib/commons-cdi-1.0.0-SNAPSHOT.jar
/WEB-INF/lib/xml-apis-1.0.b2.jar
/WEB-INF/lib/deltaspike-core-impl-1.5.0.jar
/WEB-INF/lib/dom4j-1.6.1.jar
/WEB-INF/lib/commons-codec-1.9.jar
/WEB-INF/lib/commons-lang-2.6.jar
/WEB-INF/lib/annotations-2.0.1.jar
/WEB-INF/lib/libphonenumber-7.0.3.jar
/WEB-INF/classes/
/WEB-INF/classes/com/
/WEB-INF/classes/com/timm/
/WEB-INF/classes/com/timm/common/
/WEB-INF/classes/com/timm/common/cdi/
/WEB-INF/classes/com/timm/common/cdi/web/
/WEB-INF/classes/com/timm/common/cdi/web/i18n/
/WEB-INF/classes/com/timm/common/cdi/web/i18n/ShiroCurrentLocale.class
/WEB-INF/beans.xml

该对象由位于“common-cdi”模块中的生产者方法提供。同一模块提供类似ThreadScoped的CDI扩展功能。 Weld在测试启动期间未发现此生成器,并且Weld未从“commons-cdi”模块中发现bean。怎么可能?我们可以在同一个模块中提供CDI扩展功能和CDI bean吗?

@SupportedLocales在“commons-cdi”中声明:

@Qualifier  
@Target({  
        ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD  
})  
@Retention(RetentionPolicy.RUNTIME)  
public @interface SupportedLocales {  
}

生产者在“commons-cdi”中声明:

@Dependent  
public class I18NProducer {  
    @Produces  
    @ApplicationScoped  
    @Default  
    @SupportedLocales  
    public Set<Locale> getSupportedLocales() {  
        Set<Locale> supportedLocales;  
        supportedLocales = new HashSet<Locale>();  
        supportedLocales.add(Locale.US);  
        return supportedLocales;  
    }  
}

JUnit测试用例定义:

@RunWith(Arquillian.class)
public class LocaleInjectionTest {

    @Deployment
    public static Archive<?> deploy() {
        final String moduleName = LocaleInjectionTest.class.getSimpleName();

        PomEquippedResolveStage resolver = Maven.resolver().loadPomFromFile("pom.xml");

        File[] libs = resolver.resolve("com.myname:commons-cdi").withTransitivity().asFile();

        WebArchive testWar = ShrinkWrap
                .create(WebArchive.class, moduleName + ".war")
                .addClass(MyCurrentLocale.class)
                .addAsLibraries(libs)
                .addAsWebInfResource(ArchiveUtils.getResourceBeanXMLWithAlternativeAndDiscoveryModeAnnotated(MyCurrentLocale.class),
                        "beans.xml");

        return testWar;
    }

    @Inject
    private CurrentLocale bean;

    @Test
    public void testInjection() throws Exception {
        ...
    }
}

MyCurrentLocale类定义:

    @SessionScoped
    @Alternative
    public class MyCurrentLocale extends DefaultCurrentLocale implements Serializable {

        @Inject
        protected MyCurrentLocale(@SupportedLocales Set<Locale> supportedLocales) {
            super(supportedLocales);
        }
       ...
   }

声明中有什么问题吗?

1 个答案:

答案 0 :(得分:0)

看起来你正在使用GlassFish v3,因此你需要jar中的beans.xml文件以及bean存档。