Search code examples
jakarta-eedependency-injectioncdijboss-arquillianwildfly-8

WELD-001408 Unsatisfied dependencies for type [...] with qualifiers [@Named]


I can deploy and run the project successfully, however, when I try to run the Test method. I had this common error

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)
..
..

Also I have read similar questions from here nothing did help. I was reading some helloword examples about dependency injection from here cdi-tutorial and after that I have started to read 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;

    ...
}

and my test method is like;

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));
    }
}

my beans.xml is in src/main/resources/META-INF/beans.xml

Finally, you can see my pom.xml file here


Solution

  • You should add AutomatedTellerMachineImpl.class to ShrinkWrap, like that:

    @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");
    }
    

    In practice it is better to add directly a package, so you do not miss all the time something.