Search code examples
javacdijava-ee-8weld-seweld-junit5

WELD-SE + JUnit5 WELD-001409: Ambiguous dependencies for type Validator with qualifiers @Default


I want to create a unit test of a REST resource using WELD SE with JEE8 (CDI 2.0).

This is the code of the REST resource class:

@Path("/members")
@RequestScoped
public class MemberResourceRESTService {
    @Inject
    private Logger log;

    @Inject
    private Validator validator;

    @Inject
    private MemberRepository repository;

    @Inject
    MemberRegistration registration;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Member> listAllMembers() {
        log.info("list all members");
        return repository.findAllOrderedByName();
    }

I created the rest test junit

@ExtendWith(WeldJunit5Extension.class)
@EnableWeld
public class MemberTest {

    @WeldSetup
    public WeldInitiator weld = WeldInitiator.from(
            MemberResourceRESTService.class,
            Resources.class,
            WebResources.class,
            MemberRepository.class,
            MemberRegistration.class)
            .addBeans(createValidator(), createEntityManager())
            .activate(RequestScoped.class)
            .build();


    static Bean<?> createValidator() {
        return MockBean.builder()
                .types(Validator.class)
                .scope(ApplicationScoped.class)
                .creating(
//                        Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory().getValidator()
                        Validation.buildDefaultValidatorFactory().getValidator()
                ).build();
    }

    static Bean<?> createEntityManager() {
        return MockBean.builder()
                .types(Validator.class)
                .scope(ApplicationScoped.class)
                .creating(
                        Persistence.createEntityManagerFactory("primary-test").createEntityManager()
                ).build();
    }

    @Test
    @DisplayName("list all mambers test")
    public void list_all_members(MemberResourceRESTService memberResource) {

        final List<Member> members = memberResource.listAllMembers();
        Assertions.assertFalse(members.isEmpty());

    }

In the pom.xml I've added hibernate-validator reference at test scope, so at first instance it can create the validator.

When I try to run the test, I get this error:

org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type Validator with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private it.infocert.shop.rest.MemberResourceRESTService.validator
  at it.infocert.shop.rest.MemberResourceRESTService.validator(MemberResourceRESTService.java:0)
  Possible dependencies: 
  - org.jboss.weld.junit.MockBean@4893b344,
  - org.jboss.weld.junit.MockBean@249e0271

How can I solve this ?


Solution

  • Both your methods - createEntityManager() and createValidator() - return bean that has .types(Validator.class). Therefore you have two beans eligible for injection into type Validator. I think you have a mistake of making the entity manager typed as validator?

    As a side note, having @ExtendWith(WeldJunit5Extension.class) and @EnableWeld on the same test class is superfluous. EnableWeld is just an abbreviation for Junit's original syntax.