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

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

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

问题

我正在尝试创建一个非常简单的测试用例以测试Hibernate Bean Validation中的自定义验证器这个自定义验证器有一个注入点

Junit4BeanValidation 6.1.5.FinalWeldUnit 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&#39;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&lt;ConstraintViolation&lt;BeanWithLocation&gt;&gt; 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 &#39;registers&#39; 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&lt;ConstraintViolation&lt;BeanWithLocation&gt;&gt; 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

        &lt;dependency&gt;
            &lt;groupId&gt;org.hibernate.validator&lt;/groupId&gt;
            &lt;artifactId&gt;hibernate-validator-cdi&lt;/artifactId&gt;
            &lt;version&gt;6.1.5.Final&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &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:

确定