通过注入点测试自定义验证器,WeldUnit未注入Hibernate Bean验证工厂。

huangapple go评论106阅读模式
英文:

Testing custom validators with injectionpoints, WeldUnit is not injecting the hibernate bean validation factory

问题

  1. 我正在尝试创建一个非常简单的测试用例以测试Hibernate Bean Validation中的自定义验证器这个自定义验证器有一个注入点
  2. Junit4BeanValidation 6.1.5.FinalWeldUnit 2.0.0.Final
  3. ```java
  4. public class AssertCrsForOffshoreTest extends TestBase {
  5. //CHECKSTYLE:OFF
  6. @Rule
  7. // public WeldInitiator weld = WeldInitiator.from( ValidatorFactory.class ).inject( this ).build();
  8. public WeldInitiator weld = WeldInitiator.from( ValidatorFactory.class ).build();
  9. //CHECKSTYLE:ON
  10. @Test
  11. public void testValidCrs() {
  12. // prepare test
  13. BroLocation location = createLocation();
  14. location.setCrs( BroConstants.CRS_WGS84 );
  15. BeanWithLocation bean = new BeanWithLocation( location );
  16. // action
  17. Set<ConstraintViolation<BeanWithLocation>> violations = weld.select( ValidatorFactory.class ).get().getValidator().validate( bean );
  18. // verify
  19. assertThat( violations ).isEmpty();
  20. }
  21. }

然而,由于某种原因它无法解析注入点:org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001334: 无法满足类型为 ValidatorFactory 的依赖项,带有限定符。我猜我需要引用一个实现,而不是 ValidatorFactory.class

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying to make a very simple testcase testing a custom validator in hibernate bean validation. The custom validator has an injection point.
  4. Junit4, BeanValidation 6.1.5.Final, WeldUnit 2.0.0.Final
  5. ```java
  6. public class AssertCrsForOffshoreTest extends TestBase {
  7. //CHECKSTYLE:OFF
  8. @Rule
  9. // public WeldInitiator weld = WeldInitiator.from( ValidatorFactory.class ).inject( this ).build();
  10. public WeldInitiator weld = WeldInitiator.from( ValidatorFactory.class ).build();
  11. //CHECKSTYLE:ON
  12. @Test
  13. public void testValidCrs() {
  14. // prepare test
  15. BroLocation location = createLocation();
  16. location.setCrs( BroConstants.CRS_WGS84 );
  17. BeanWithLocation bean = new BeanWithLocation( location );
  18. // action
  19. Set&lt;ConstraintViolation&lt;BeanWithLocation&gt;&gt; violations = weld.select( ValidatorFactory.class ).get().getValidator().validate( bean );
  20. // verify
  21. assertThat( violations ).isEmpty();
  22. }
  23. }

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库添加到测试范围中。

以下是完整的(可工作的解决方案):

  1. public class AssertCrsForOffshoreTest extends TestBase {
  2. //CHECKSTYLE:OFF
  3. // 初始化验证扩展并将测试类“注册”为GeometryServiceHelper模拟的生成器
  4. @Rule
  5. public WeldInitiator weld = WeldInitiator.from(ValidationExtension.class, AssertCrsForOffshoreTest.class).build();
  6. //CHECKSTYLE:ON
  7. @ApplicationScoped
  8. @Produces
  9. GeometryServiceHelper produceGeometryServiceHelper() throws GeometryServiceException {
  10. // 提供给自定义注释的模拟
  11. GeometryServiceHelper geometryService = mock(GeometryServiceHelper.class);
  12. when(geometryService.isOffshore(any(BroLocation.class))).thenReturn(true);
  13. return geometryService;
  14. }
  15. @Test
  16. public void testValidCrs() {
  17. // 准备测试数据
  18. BroLocation location = createLocation();
  19. location.setCrs(BroConstants.CRS_WGS84);
  20. BeanWithLocation bean = new BeanWithLocation(location);
  21. // 执行操作
  22. Set<ConstraintViolation<BeanWithLocation>> violations = weld.select(ValidatorFactory.class).get().getValidator().validate(bean);
  23. // 验证结果
  24. assertThat(violations).isEmpty();
  25. }
  26. }

你还需要将beanvalidation的CDI扩展添加到单元测试的依赖范围中:

  1. <dependency>
  2. <groupId>org.hibernate.validator</groupId>
  3. <artifactId>hibernate-validator-cdi</artifactId>
  4. <version>6.1.5.Final</version>
  5. <scope>test</scope>
  6. </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)

  1. public class AssertCrsForOffshoreTest extends TestBase {
  2. //CHECKSTYLE:OFF
  3. // intializes the validation extension and &#39;registers&#39; the test class as producer of the GeometryServiceHelper mock
  4. @Rule
  5. public WeldInitiator weld = WeldInitiator.from( ValidationExtension.class, AssertCrsForOffshoreTest.class ).build();
  6. //CHECKSTYLE:ON
  7. @ApplicationScoped
  8. @Produces
  9. GeometryServiceHelper produceGeometryServiceHelper() throws GeometryServiceException {
  10. // mock provided to the custom annotation.
  11. GeometryServiceHelper geometryService = mock( GeometryServiceHelper.class );
  12. when( geometryService.isOffshore( any( BroLocation.class ) ) ).thenReturn( true );
  13. return geometryService;
  14. }
  15. @Test
  16. public void testValidCrs() {
  17. // prepare test
  18. BroLocation location = createLocation();
  19. location.setCrs( BroConstants.CRS_WGS84 );
  20. BeanWithLocation bean = new BeanWithLocation( location );
  21. // action
  22. Set&lt;ConstraintViolation&lt;BeanWithLocation&gt;&gt; violations = weld.select( ValidatorFactory.class ).get().getValidator().validate( bean );
  23. // verify
  24. assertThat( violations ).isEmpty();
  25. }
  26. }

You'll also need to add the cdi extension of beanvalidation to the unit test scope

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.hibernate.validator&lt;/groupId&gt;
  3. &lt;artifactId&gt;hibernate-validator-cdi&lt;/artifactId&gt;
  4. &lt;version&gt;6.1.5.Final&lt;/version&gt;
  5. &lt;scope&gt;test&lt;/scope&gt;
  6. &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年9月4日 03:38:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63730572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定