首先,我搜索了大量的内容,根据http://jglue.org/cdi-unit-user-guide/的说法,在单元测试中生成要注入的东西应该可以正常工作。
我的设置:
@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个例外:
异常0: org.jboss.weld.exceptions.DeploymentException:焊缝-001408:类型Logger的不满意的依赖项,有限定符@Default at injection point BackedAnnotatedField @Inject私有at.fhhagenberg.unitTesting.beans.SurveyBean.log .
这意味着CdiRunner试图构建我的SurveyBean并注入记录器,但是它找不到要注入的对象,尽管我在基类中专门生成它(EntityManager也是如此)。
有人知道怎么解决这个问题吗?
PS:标签我不被允许添加: cdi-unit,jglue
发布于 2015-10-07 07:02:39
您需要将生成器方法放到与DBUnitBaseTest不同的类中。该类是抽象的,不能用作CDI生成器。这两种制作方法都是针对em和记录器的。
这是因为具有生产者方法/字段的类必须是CDI本身--这个类的实例是在调用producer方法之前由CDI创建的。CDI不能从抽象类创建bean。而且,@Producer注释不会被继承,因此SurveyBeanTest继承的方法不会被视为生产者。
https://stackoverflow.com/questions/32977594
复制相似问题