Sjaak
Sjaak

Reputation: 4160

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

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

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.

Upvotes: 0

Views: 620

Answers (1)

Sjaak
Sjaak

Reputation: 4160

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>

Upvotes: 2

Related Questions