2015-10-06 92 views
1

首先,我密集搜索并根据http://jglue.org/cdi-unit-user-guide/产生的东西注入单元测试应该工作得很好。CDI单元@生产不起作用

我的设置:

@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:

异常0: org.jboss.weld 3个例外例外列表.exceptions.DeploymentException:WELD-001408:具有限定符的Logger类型的不满意依赖关系@默认 处于注入点[BackedAnnotatedField] @Inject private at.fhhagenberg.unitTesting.beans.SurveyBean.log ...

这意味着CdiRunner会尝试构建我的SurveyBean并注入记录器,但无法找到要注入的对象,尽管我专门在基类中生成了它(EntityManager也一样)。

任何人都知道如何解决这个问题?

PS:标签我是不允许添加:CDI单元,jglue

回答

2

你需要把你的生产方法为从DBUnitBaseTest一个单独的类。这个类是抽象的,不能用作CDI生产者。这两种生产者方法适用于em和logger。

这是因为具有生产者方法/字段的类必须是CDI bean本身 - 该类的实例在调用生产者方法之前由CDI创建。而CDI不能从抽象类创建bean。此外,@Producer注释未被继承,因此由SurveyBeanTest继承的方法不被视为生产者。

+0

听起来很对,今晚会试。谢谢。 – Thomas