CDI-Unit @Produces无法正常工作 [英] CDI-Unit @Produces not working
问题描述
首先,我根据 http://jglue.org/cdi-unit进行了深入搜索-user-guide/生成要注入到单元测试中的东西应该可以正常工作.
First off I googled intensively and according to http://jglue.org/cdi-unit-user-guide/ producing stuff to inject in a unit test should work just fine.
我的设置:
@RunWith(CdiRunner.class)
public abstract class CdiUnitBaseTest extends DBUnitBaseTest {
@Produces
public EntityManager em() {
return em; //field from base class filled @BeforeClass
}
@Produces
public Logger logger() {
return LogManager.getLogger();
}
}
public class SurveyBeanTest extends CdiUnitBaseTest {
@Inject
private SurveyBean bean;
@Test
public void surveyWithoutParticipation() {
Survey s = new Survey();
s.setParticipation(new ArrayList<Participation>());
boolean result = this.bean.hasParticipated("12ST", s);
Assert.assertFalse(result);
}
}
@Remote(SurveyRemote.class)
@Stateless
public class SurveyBean implements SurveyRemote {
@Inject
private Logger log;
@Inject
private SurveyDao sDao;
@Inject
private ParticipationDao pDao;
...
}
例外:
org.jboss.weld.exceptions.DeploymentException:具有3个例外的例外列表:
org.jboss.weld.exceptions.DeploymentException: Exception List with 3 exceptions:
异常0: org.jboss.weld.exceptions.DeploymentException:WELD-001408:带限定符@Default的Logger类型的依赖关系未得到满足 在注入点[BackedAnnotatedField] @Inject私有at.fhhagenberg.unitTesting.beans.SurveyBean.log ...
Exception 0 : org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Logger with qualifiers @Default at injection point [BackedAnnotatedField] @Inject private at.fhhagenberg.unitTesting.beans.SurveyBean.log ...
这意味着CdiRunner尝试构建我的SurveyBean并注入记录器,但找不到我要注入的对象,尽管我在基类中专门生产了它(EntityManager也是如此).
This means the CdiRunner tries to build my SurveyBean and inject the logger, but it cannot find an object to inject, although I specifically produce it in the base class (same goes for EntityManager).
有人知道如何解决这个问题吗?
Anybody knows how to fix this?
PS:我不允许添加的标签:cdi-unit,jglue
PS: tags I was not allowed to add: cdi-unit, jglue
推荐答案
您需要将生产者方法放入与DBUnitBaseTest分开的类中.此类是抽象的,不能用作CDI生产者. em和logger的生产者方法.
You need to put your producer methods into a separate class from DBUnitBaseTest. This class is abstract and cannot be used as a CDI producer. Both producer methods for em and logger.
这是因为具有生产者方法/字段的类本身必须是CDI bean-该类的实例是在调用生产者方法之前由CDI创建的. CDI无法从抽象类创建bean.另外,@Producer
注释不会被继承,因此SurveyBeanTest
继承的方法不会被视为生产者.
This is because the class having producer methods/fields must be a CDI bean itself - an instance of that class is created by CDI prior to calling producer methods. And CDI cannot create beans from abstract class. Also, @Producer
annotations are not inherited, therefore methods inherited by SurveyBeanTest
are not treated as producers.
这篇关于CDI-Unit @Produces无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!