WELD-001408类型[...]与限定符[@Named]的不满意依赖关系

时间:2014-12-11 09:02:55

标签: java-ee dependency-injection cdi jboss-arquillian wildfly-8

但是,当我尝试运行Test方法时,我可以成功部署和运行项目。我有这个常见的错误

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [AutomatedTellerMachine] with qualifiers [@Named] at injection point [[field] @Named @Inject private advocacy.TestExample.automatedTellerMachine]
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:274)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:243)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:106)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:126)
    at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:345)
    at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:330)
..
..

此外,我从这里读过类似的问题没有任何帮助。我正在阅读一些关于依赖注入的helloword示例cdi-tutorial,之后我开始阅读arquillian-tutorial

AutomatedTellerMachine.java

package advocacy;

import java.math.BigDecimal;

public interface AutomatedTellerMachine {

    public void deposit(BigDecimal bd);
    public void withdraw(BigDecimal bd);
}

AutomatedTellerMachineImpl.java

package advocacy;

import javax.inject.Inject;
import javax.inject.Named;
import java.math.BigDecimal;

@Named("atm")
public class AutomatedTellerMachineImpl implements AutomatedTellerMachine {

    @Inject
    @Named("jsonRestAtmTransport")
    private ATMTransport atmTransport;

    ...
}

我的测试方法就像;

TestExample.java

package advocacy;

imports..

@RunWith(Arquillian.class)
public class TestExample {

    @Inject
    @Named("atm")
    private AutomatedTellerMachine automatedTellerMachine;

    @Deployment
    public static WebArchive createTestArchive() {
        return ShrinkWrap.create(WebArchive.class, "dependecy-injection.war")
                .addClasses(AutomatedTellerMachine.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "META-INF/beans.xml");
    }


    @Test
    public void test() {
        automatedTellerMachine.deposit(new BigDecimal(12.99));
    }
}

我的beans.xml位于src/main/resources/META-INF/beans.xml

最后,您可以看到我的pom.xml文件here

1 个答案:

答案 0 :(得分:2)

您应该将AutomatedTellerMachineImpl.class添加到ShrinkWrap,就像那样:

@Deployment
public static WebArchive createTestArchive() {
    return ShrinkWrap.create(WebArchive.class, "dependecy-injection.war")
            .addClasses(AutomatedTellerMachine.class)
            .addClasses(AutomatedTellerMachineImpl.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "META-INF/beans.xml");
}

在实践中,最好直接添加一个包,这样你就不会一直错过任何东西。