英文:
Testing custom validators with injectionpoints, WeldUnit is not injecting the hibernate bean validation factory
问题
我正在尝试创建一个非常简单的测试用例,以测试Hibernate Bean Validation中的自定义验证器。这个自定义验证器有一个注入点。
Junit4,BeanValidation 6.1.5.Final,WeldUnit 2.0.0.Final
```java
public class AssertCrsForOffshoreTest extends TestBase {
//CHECKSTYLE:OFF
@Rule
// public WeldInitiator weld = WeldInitiator.from( ValidatorFactory.class ).inject( this ).build();
public WeldInitiator weld = WeldInitiator.from( ValidatorFactory.class ).build();
//CHECKSTYLE:ON
@Test
public void testValidCrs() {
// prepare test
BroLocation location = createLocation();
location.setCrs( BroConstants.CRS_WGS84 );
BeanWithLocation bean = new BeanWithLocation( location );
// action
Set<ConstraintViolation<BeanWithLocation>> violations = weld.select( ValidatorFactory.class ).get().getValidator().validate( bean );
// verify
assertThat( violations ).isEmpty();
}
}
然而,由于某种原因它无法解析注入点:org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001334: 无法满足类型为 ValidatorFactory 的依赖项,带有限定符
。我猜我需要引用一个实现,而不是 ValidatorFactory.class
。
<details>
<summary>英文:</summary>
I'm trying to make a very simple testcase testing a custom validator in hibernate bean validation. The custom validator has an injection point.
Junit4, BeanValidation 6.1.5.Final, WeldUnit 2.0.0.Final
```java
public class AssertCrsForOffshoreTest extends TestBase {
//CHECKSTYLE:OFF
@Rule
// public WeldInitiator weld = WeldInitiator.from( ValidatorFactory.class ).inject( this ).build();
public WeldInitiator weld = WeldInitiator.from( ValidatorFactory.class ).build();
//CHECKSTYLE:ON
@Test
public void testValidCrs() {
// prepare test
BroLocation location = createLocation();
location.setCrs( BroConstants.CRS_WGS84 );
BeanWithLocation bean = new BeanWithLocation( location );
// action
Set<ConstraintViolation<BeanWithLocation>> violations = weld.select( ValidatorFactory.class ).get().getValidator().validate( bean );
// verify
assertThat( violations ).isEmpty();
}
}
However, for some reason it cannot resolve the injection point: org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001334: Unsatisfied dependencies for type ValidatorFactory with qualifiers
. I guess I need to refer to an implementation rather than the ValidatorFactory.class
.
答案1
得分: 2
如Nikos在上面的评论部分所描述,我需要在fluent方法的from语句中添加ValidationExtension.class
,并且将CDI库添加到测试范围中。
以下是完整的(可工作的解决方案):
public class AssertCrsForOffshoreTest extends TestBase {
//CHECKSTYLE:OFF
// 初始化验证扩展并将测试类“注册”为GeometryServiceHelper模拟的生成器
@Rule
public WeldInitiator weld = WeldInitiator.from(ValidationExtension.class, AssertCrsForOffshoreTest.class).build();
//CHECKSTYLE:ON
@ApplicationScoped
@Produces
GeometryServiceHelper produceGeometryServiceHelper() throws GeometryServiceException {
// 提供给自定义注释的模拟
GeometryServiceHelper geometryService = mock(GeometryServiceHelper.class);
when(geometryService.isOffshore(any(BroLocation.class))).thenReturn(true);
return geometryService;
}
@Test
public void testValidCrs() {
// 准备测试数据
BroLocation location = createLocation();
location.setCrs(BroConstants.CRS_WGS84);
BeanWithLocation bean = new BeanWithLocation(location);
// 执行操作
Set<ConstraintViolation<BeanWithLocation>> violations = weld.select(ValidatorFactory.class).get().getValidator().validate(bean);
// 验证结果
assertThat(violations).isEmpty();
}
}
你还需要将beanvalidation的CDI扩展添加到单元测试的依赖范围中:
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>6.1.5.Final</version>
<scope>test</scope>
</dependency>
注意:这只是你提供的代码片段的翻译,可能会因为上下文的缺失而导致某些表达不够准确。
英文:
As Nikos described in the comment section above I needed to add the ValidationExtension.class
to the from fluent method and add the cdi library to the test scope.
This is the full (working solution)
public class AssertCrsForOffshoreTest extends TestBase {
//CHECKSTYLE:OFF
// intializes the validation extension and 'registers' the test class as producer of the GeometryServiceHelper mock
@Rule
public WeldInitiator weld = WeldInitiator.from( ValidationExtension.class, AssertCrsForOffshoreTest.class ).build();
//CHECKSTYLE:ON
@ApplicationScoped
@Produces
GeometryServiceHelper produceGeometryServiceHelper() throws GeometryServiceException {
// mock provided to the custom annotation.
GeometryServiceHelper geometryService = mock( GeometryServiceHelper.class );
when( geometryService.isOffshore( any( BroLocation.class ) ) ).thenReturn( true );
return geometryService;
}
@Test
public void testValidCrs() {
// prepare test
BroLocation location = createLocation();
location.setCrs( BroConstants.CRS_WGS84 );
BeanWithLocation bean = new BeanWithLocation( location );
// action
Set<ConstraintViolation<BeanWithLocation>> violations = weld.select( ValidatorFactory.class ).get().getValidator().validate( bean );
// verify
assertThat( violations ).isEmpty();
}
}
You'll also need to add the cdi extension of beanvalidation to the unit test scope
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>6.1.5.Final</version>
<scope>test</scope>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论