Spring mockMvc doesn't consider validation in my test

后端 未结 3 1518
傲寒
傲寒 2020-12-09 10:03

I am trying to set up an integration testing with mockMvc, and i have a problem with it. Indeed, spring doesn\'t integrate any validation annotation.
For more precisio

相关标签:
3条回答
  • 2020-12-09 10:07

    I got same problem, after updating validator to 5.1.0.Final. Application is working perfectly but REST tests not (@Valid annotation is not considered at all). I resolved the problem with adding one additional dependency only for tests:

       <dependency>
         <groupId>javax.el</groupId>
         <artifactId>javax.el-api</artifactId>
         <version>2.2.4</version>
         <scope>test</scope>
      </dependency>
    
    0 讨论(0)
  • 2020-12-09 10:14

    Ok, I have just found what is responsible for the fact that validation was not consider by mockmvc. It's just a bad dependency in my pom:

    I used

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    

    and when i replaced it by

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.1.0.Final</version>
    </dependency>
    

    the test work just fine!

    the problem is solved, but I am wondering why the problem comes from this part. hibernate-validator 5.1.0.Final include javax validation in transitive dependency, so normally, this problem would never have appeared.

    0 讨论(0)
  • 2020-12-09 10:27

    If you are using hibernate version 5.4.1.Final just add below dependency for your test

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.0</version>
    </dependency>
    

    This dependency is "provided" dependency. You can find matching dependency for you version of hibernate at maven site. https://mvnrepository.com/artifact/org.hibernate/hibernate-validator/5.4.1.Final

    For example if you are using 5.4.1 version go to above link and check for provided dependencies and use it.

    0 讨论(0)
提交回复
热议问题