通過焊接發現當使用Glassfish的嵌入插件運行一個單元測試用的Arquillian,我得到以下CDI錯誤:生產者是在過程的Arquillian
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在模塊中定義的部署在測試的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是聲稱,在 「公地CDI」 與:
@Qualifier
@Target({
ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD
})
@Retention(RetentionPolicy.RUNTIME)
public @interface SupportedLocales {
}
,生產商是宣佈 「公地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);
}
...
}
聲明中有什麼問題嗎?
您可以添加您的測試類和類與注入點爲設置? –
我將它添加到最初的消息。 – ruddy32