You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@deltaspike.apache.org by alebu <al...@gmail.com> on 2016/02/04 10:30:16 UTC

Unit tests with deltaspike, weld and hibernate bean-validation

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");

Re: Unit tests with deltaspike, weld and hibernate bean-validation

Posted by Gerhard Petracek <ge...@gmail.com>.
hi,

great to hear that!
yes - the bv-module is just needed with bv v1.0.

regards,
gerhard

http://www.irian.at

Your JavaEE powerhouse -
JavaEE Consulting, Development and
Courses in English and German

Professional Support for Apache
MyFaces, DeltaSpike and OpenWebBeans

2016-02-04 11:57 GMT+01:00 alebu <al...@gmail.com>:

> Thank you Gerhard,
> Forwarding to hibernate validator AND CDI was a good hint. By replacing
> 'hibernate-validator' artifact with 'hibernate-validator-cdi' I got
> validation right where I wanted it. Additionally I removed deltaspike
> bean-validation and it still works fine.
>
>
> On Thu, Feb 4, 2016 at 12:40 PM, Gerhard Petracek <gp...@apache.org>
> wrote:
>
> > hi,
> >
> > parameter-validation just works if it's integrated (like with jax-rs).
> > such an integration is done e.g. via an interceptor.
> > -> if you can't use an out-of-the-box integration (provided by the
> > validation-provider you are using),
> > you have to do it on your own (it isn't the task of the
> test-control-module
> > to support it).
> > e.g. via a cdi-extension you can add an interceptor(-binding-annotation)
> to
> > your beans dynamically (and you can even limit that to project-stage
> > development).
> > with that you would introduce a significant difference in your test-code.
> > -> instead of that i would ask the hibernate-validator-team, if they
> > support parameter-validation in a java-se + cdi based application.
> > (in that case it would also work with the test-control-module without
> > changes.)
> >
> > regards,
> > gerhard
> >
> >
> >
> > 2016-02-04 10:30 GMT+01:00 alebu <al...@gmail.com>:
> >
> > > 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");
> > >
> >
>

Re: Unit tests with deltaspike, weld and hibernate bean-validation

Posted by alebu <al...@gmail.com>.
Thank you Gerhard,
Forwarding to hibernate validator AND CDI was a good hint. By replacing
'hibernate-validator' artifact with 'hibernate-validator-cdi' I got
validation right where I wanted it. Additionally I removed deltaspike
bean-validation and it still works fine.


On Thu, Feb 4, 2016 at 12:40 PM, Gerhard Petracek <gp...@apache.org>
wrote:

> hi,
>
> parameter-validation just works if it's integrated (like with jax-rs).
> such an integration is done e.g. via an interceptor.
> -> if you can't use an out-of-the-box integration (provided by the
> validation-provider you are using),
> you have to do it on your own (it isn't the task of the test-control-module
> to support it).
> e.g. via a cdi-extension you can add an interceptor(-binding-annotation) to
> your beans dynamically (and you can even limit that to project-stage
> development).
> with that you would introduce a significant difference in your test-code.
> -> instead of that i would ask the hibernate-validator-team, if they
> support parameter-validation in a java-se + cdi based application.
> (in that case it would also work with the test-control-module without
> changes.)
>
> regards,
> gerhard
>
>
>
> 2016-02-04 10:30 GMT+01:00 alebu <al...@gmail.com>:
>
> > 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");
> >
>

Re: Unit tests with deltaspike, weld and hibernate bean-validation

Posted by Gerhard Petracek <gp...@apache.org>.
hi,

parameter-validation just works if it's integrated (like with jax-rs).
such an integration is done e.g. via an interceptor.
-> if you can't use an out-of-the-box integration (provided by the
validation-provider you are using),
you have to do it on your own (it isn't the task of the test-control-module
to support it).
e.g. via a cdi-extension you can add an interceptor(-binding-annotation) to
your beans dynamically (and you can even limit that to project-stage
development).
with that you would introduce a significant difference in your test-code.
-> instead of that i would ask the hibernate-validator-team, if they
support parameter-validation in a java-se + cdi based application.
(in that case it would also work with the test-control-module without
changes.)

regards,
gerhard



2016-02-04 10:30 GMT+01:00 alebu <al...@gmail.com>:

> 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");
>