WicketTester和CDI-Unit的单元测试在Eclipse中工作,但在Maven构建期间失败。我错过了什么?
我正在尝试使用CDI-Unit来测试我的Wicket组件,它们使用CDI进行依赖注入。测试似乎在Eclipse中完美工作,但在我的Maven构建过程中失败,我似乎找不到任何提示和错误。WicketTester和CDI-Unit的单元测试在Eclipse中工作,但在Maven构建期间失败。我错过了什么?
我已经创建了一个简单的抽象WicketPanel
public abstract class MyPanel extends Panel{ private static final long serialVersionUID = 4132041261965905788L;
private final RepeatingView rw;
@Inject
transient ReflectiveComponentFactory factory;
public MyPanel(String id) {
super(id);
rw = new RepeatingView(OVERLAY_COMPONENT_GROUP_ID);
add(rw);
}
@Override
public <CT extends Component> CT addComponent(Class<CT> componentType) {
return addComponent(componentType, OVERLAY_COMPONENT_ID);
}
protected <CT extends Component> CT addComponent(Class<CT> componentType, String overlayComponentId) {
WebMarkupContainer collapsableGroup = new WebMarkupContainer(rw.newChildId());
rw.add(collapsableGroup);
CT component = factory.createComponent(componentType, overlayComponentId);
collapsableGroup.add(component);
return component;
}
}
和注入工厂:
@ApplicationScoped public class ReflectiveComponentFactory implements Serializable{
private static final long serialVersionUID = -4587243549845349456L;
public <CT extends Component> CT createComponent(Class<CT> componentType, String componentId){
try {
Constructor<CT> constructor = componentType.getConstructor(String.class);
return constructor.newInstance(componentId);
} catch (ReflectiveOperationException | SecurityException | IllegalArgumentException e) {
throw new ComponentCreationException(e);
}
}
}
然后创建使用CDI-单位一个单元测试:
@RunWith(CdiRunner.class) @AdditionalClasses(value={ReflectiveComponentFactory.class})
public class MyPanelTest {
private WicketTester tester;
@Inject
private BeanManager beanManager;
@Before
public void setup() {
tester = new WicketTester();
new CdiConfiguration(beanManager).setPropagation(ConversationPropagation.NONE).configure(tester.getApplication());
}
@Test
public void testAddComponentWithClass() {
MyPanelTested myPanel = new MyPanelTested("someId");
TestPanel panel1 = myPanel.addComponent(TestPanel.class);
TestPanel panel2 = myPanel.addComponent(TestPanel.class);
tester.startComponentInPage(myPanel);
tester.assertComponent(panel1.getPageRelativePath(), TestPanel.class);
tester.assertComponent(panel2.getPageRelativePath(), TestPanel.class);
tester.assertComponent(panel1.getPageRelativePath() + ":text", Label.class);
tester.assertComponent(panel2.getPageRelativePath() + ":text", Label.class);
}
}
@SuppressWarnings("serial")
class MyPanelTested extends MyPanel {
public MyPanelTested(String id) {
super(id);
}
}
我有不包括TestPanel,但它非常简单(或多或少为空)。
当我在Eclipse中执行此操作时,测试通过绿色!
当我执行这与Maven我得到以下几点:
org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.Dependent at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:578)
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:608)
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:674)
at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:136)
at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:763)
at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:772)
at org.jboss.weld.manager.SimpleInjectionTarget$1.proceed(SimpleInjectionTarget.java:106)
at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
at org.jboss.weld.manager.SimpleInjectionTarget.inject(SimpleInjectionTarget.java:102)
at org.apache.wicket.cdi.NonContextual.postConstruct(NonContextual.java:129)
at org.apache.wicket.cdi.NonContextualManager.postConstruct(NonContextualManager.java:65)
at org.apache.wicket.cdi.DetachEventEmitter.<init>(DetachEventEmitter.java:55)
at org.apache.wicket.cdi.CdiConfiguration.configure(CdiConfiguration.java:196)
.....
任何线索,我在做什么错?
回答:
该问题是由cdi-unit版本2.0.8中的一个错误引起的。 Bryn解决了这个问题(见https://github.com/BrynCooke/cdi-unit/issues/21),因此如果使用cdi-unit 2.0.9或更高版本,它应该不会出现。
以上是 WicketTester和CDI-Unit的单元测试在Eclipse中工作,但在Maven构建期间失败。我错过了什么? 的全部内容, 来源链接: utcz.com/qa/259575.html