WELD-SE + JUnit5 WELD-001409:带有限定符@Default 的类型验证器的依赖性不明确
WELD-SE + JUnit5 WELD-001409: Ambiguous dependencies for type Validator with qualifiers @Default
我想使用带有 JEE8 (CDI 2.0) 的 WELD SE 创建 REST 资源的单元测试。
这是REST资源的代码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();
}
我创建了其余测试 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());
}
在 pom.xml 中,我在测试范围内添加了 hibernate-validator 引用,因此在第一个实例中它可以创建验证器。
当我尝试 运行 测试时,出现此错误:
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
我该如何解决这个问题?
您的方法 - createEntityManager()
和 createValidator()
- return 具有 .types(Validator.class)
的 bean。因此,您有两个符合注入类型 Validator
的 bean。我认为您将实体管理器键入为验证器有误吗?
作为旁注,在同一个测试 class 中有 @ExtendWith(WeldJunit5Extension.class)
和 @EnableWeld
是多余的。 EnableWeld
只是Junit原始语法的缩写。
我想使用带有 JEE8 (CDI 2.0) 的 WELD SE 创建 REST 资源的单元测试。
这是REST资源的代码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();
}
我创建了其余测试 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());
}
在 pom.xml 中,我在测试范围内添加了 hibernate-validator 引用,因此在第一个实例中它可以创建验证器。
当我尝试 运行 测试时,出现此错误:
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
我该如何解决这个问题?
您的方法 - createEntityManager()
和 createValidator()
- return 具有 .types(Validator.class)
的 bean。因此,您有两个符合注入类型 Validator
的 bean。我认为您将实体管理器键入为验证器有误吗?
作为旁注,在同一个测试 class 中有 @ExtendWith(WeldJunit5Extension.class)
和 @EnableWeld
是多余的。 EnableWeld
只是Junit原始语法的缩写。