首先,我在 Google 上进行了密集搜索,根据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:WELD-001408:在注入点[BackedAnnotatedField] @Inject private at.fhhagenberg.unitTesting.beans.SurveyBean.log ...
这意味着 CdiRunner 尝试构建我的 SurveyBean 并注入记录器,但它找不到要注入的对象,尽管我专门在基类中生成它(EntityManager 也是如此)。
有人知道如何解决这个问题吗?
PS:我不允许添加的标签:cdi-unit,jglue