deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alebu <ale...@gmail.com>
Subject Unit tests with deltaspike, weld and hibernate bean-validation
Date Thu, 04 Feb 2016 09:30:16 GMT
Hi all,
I want to configure unit tests so I could use bean-validation but for some
reason my validation constraints are ignored. The idea behind is to
simulate JEE application server (AS) as max as possible and test business
logic this way. I defined a very simple case which consist of service class
which has a method with some constraints on parameters. And I expect to get
a ConstraintViolationException if I call this method with "wrong"
parameters. However method is executed without any exceptions and it looks
like no validation happens.
Here is my setup in maven but briefly:
1) I added hivernate validator because I was getting
javax.validation.ValidationException with message below otherwise:
Unable to create a Configuration, because no Bean Validation provider could
be found. Add a provider like Hibernate Validator (RI) to your classpath.

2) Based on docs https://deltaspike.apache.org/documentation/configure.html
I added:
deltaspike-core-api and deltaspike-core-impl artifacts.

3) Based on docs
https://deltaspike.apache.org/documentation/bean-validation.html I added:
deltaspike-bean-validation-module-impl

4) Based on https://deltaspike.apache.org/documentation/test-control.html I
added
deltaspike-test-control-module-api
deltaspike-test-control-module-impl
and weld realted artifacts:
deltaspike-cdictrl-weld
weld-se-core

DETAILS:

    <properties>
        ...
        <deltaspike.version>1.5.2</deltaspike.version>
        <weld.version>2.3.2.Final</weld.version>
    </properties>
...
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.3.Final</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.core</groupId>
            <artifactId>deltaspike-core-api</artifactId>
            <version>${deltaspike.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.core</groupId>
            <artifactId>deltaspike-core-impl</artifactId>
            <version>${deltaspike.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-bean-validation-module-impl</artifactId>
            <version>${deltaspike.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-test-control-module-api</artifactId>
            <version>${deltaspike.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-test-control-module-impl</artifactId>
            <version>${deltaspike.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.cdictrl</groupId>
            <artifactId>deltaspike-cdictrl-weld</artifactId>
            <version>${deltaspike.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se-core</artifactId>
            <version>${weld.version}</version>
            <scope>test</scope>
        </dependency>

-----

JUNIT looks like:
@RunWith(CdiTestRunner.class)
public class TestDeltaspike {

    @Inject
    DummyService dummyService;

    @Test
    public void testValidAnnotationScenario(){
        assertNotNull(dummyService);


//Validation.byDefaultProvider().configure().constraintValidatorFactory(new
CDIAwareConstraintValidatorFactory()).buildValidatorFactory();

        dummyService.validateValue(7);
        fail("Expected validation exception");
    }
}

where DummyService looks like:
public class DummyService {

    public void validateValue( @Min(10) int value ){
        System.out.println( "XXX: value "+value+" was validated at this
point and must be at least 10" );
    }
}

-----
I also have beans.xml in src/test/resources/META-INF/beans.xml like:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.2" bean-discovery-mode="all">
</beans>

and validation.xml in src/test/resources/META-INF/validation.xml
but from logs it looks like it does nothing. I am making this assumption
because when I uncommenting the line in my test
//Validation.byDefaultProvider().configure() ...
then I see something in logs related to validator configuration, otherwise
no validation is mentioned in logs.
But non of the both helps anyway.
Test fails on:
fail("Expected validation exception");

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message