JUnit 规则和 cdi

JUnit Rule and cdi

我不太熟悉 cdi 或 JUnit 规则(事实上我刚刚发现它们)但我尝试使用 embedded-ldap-junit library (based on UnboundID LDAP SDK) following this post 测试我的 ldap api。我明确指出我的 api 不是基于 UnboundId LDAP SDK !

这是我的代码:

@RunWith(CdiRunner.class)
public class LdapTest extends TestCase {
    public static final Logger LOGGER = LoggerFactory.getLogger(LdapTest.class);
    public static final String DOMAIN_DSN = "dc=my.domain";
    public static final String LDIF_FILE = "agents.ldif";
    public static final String BPM_SCHEMA = "schema.ldif";

    @Inject
    private LDAPManager ldapManager;

    @Rule
    public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance().usingDomainDsn(DOMAIN_DSN)
        .importingLdifs(LDIF_FILE).withSchema(BPM_SCHEMA).build();

    @Test
    public void getAgent() throws Exception {
        embeddedLdapRule.ldapConnection();
        DirContext dirContext = embeddedLdapRule.dirContext();
        ldapManager.setContext(dirContext);
        assertNotNull(ldapManager.getAgent("123"));
    }
}

当我尝试 运行 它时,出现以下异常:

org.jboss.weld.exceptions.DefinitionException: WELD-000075 Normal scoped managed bean implementation class has a public field:  public@RunWith @ApplicationScoped class org.mycompany.LdapTest
at org.jboss.weld.bean.ManagedBean.checkBeanImplementation(ManagedBean.java:459)
at org.jboss.weld.bean.AbstractClassBean.initialize(AbstractClassBean.java:176)
at org.jboss.weld.bean.ManagedBean.initialize(ManagedBean.java:329)
at org.jboss.weld.bootstrap.AbstractBeanDeployer.deploy(AbstractBeanDeployer.java:117)
at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:217)
at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:357)
at org.jboss.weld.bootstrap.api.helpers.ForwardingBootstrap.deployBeans(ForwardingBootstrap.java:70)
at org.jboss.weld.environment.se.Weld.initialize(Weld.java:135)
at org.jglue.cdiunit.CdiRunner.createTest(CdiRunner.java:94)
at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.jglue.cdiunit.CdiRunner.methodBlock(CdiRunner.java:122)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我尝试使用 embeddedLdapRule 访问级别修饰符(私有、/受保护/public)但没有成功。你能向我解释一下我做错了什么吗?非常感谢!

我终于找到了答案:

所以解决方案是:

@RunWith(CdiRunner.class)
public class LdapTest extends TestCase {
    public static final Logger LOGGER =     LoggerFactory.getLogger(LdapTest.class);
    public static final String DOMAIN_DSN = "dc=my.domain";
    public static final String LDIF_FILE = "agents.ldif";
    public static final String BPM_SCHEMA = "schema.ldif";

    @Inject
    private LDAPManager ldapManager;

    private EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance()
        .usingDomainDsn(DOMAIN_DSN)
        .importingLdifs(LDIF_FILE)
        .withSchema(BPM_SCHEMA)
        .build();

    @Rule
    public EmbeddedLdapRule getLdapRule() {
        return embeddedLdapRule;
    }

    @Test
    public void getAgent() throws Exception {
        embeddedLdapRule.ldapConnection();
        DirContext dirContext = embeddedLdapRule.dirContext();
        ldapManager.setContext(dirContext);
        assertNotNull(ldapManager.getAgent("123"));
    }
}