运行JUnit测试时出现javax.validation.ConstraintViolationException代码属性缺失错误
Hey there, I’ve run into this exact headache before—let’s break down what’s going on and fix it step by step.
The Root Cause
Your issue boils down to conflicting dependencies and how the provided scope works:
- The
javaee-apidependency withscope=providedholds empty skeleton classes forjavax.validation(includingConstraintViolationException). These classes only have method signatures, no actual code inside. - The
providedscope tells Maven "the runtime server (like Tomcat/JBoss) will supply these real classes"—but when running JUnit tests, there’s no application server to do that. - Even though you have
validation-apiandhibernate-validatorin your project, the empty classes fromjavaee-apiget loaded first by the classloader, leading to that runtime error.
Solutions to Try
1. Replace Full javaee-api with Specific Modules
Most Spring Boot projects don’t need the entire Java EE API. Instead, only add the specific modules you actually use. For example, if you just need Servlet API:
<!-- Remove the full javaee-api dependency --> <!-- Replace with targeted modules like Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- Keep your required validation dependencies --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.13.Final</version> </dependency>
This completely avoids the empty validation classes from javaee-api.
2. Exclude Validation from javaee-api
If you absolutely need to keep the full javaee-api dependency, exclude its validation-api component so your real validation libraries take priority:
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>8.0</version> <scope>provided</scope> <exclusions> <exclusion> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> </exclusion> </exclusions> </dependency> <!-- Your existing validation dependencies stay as-is --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.13.Final</version> </dependency>
3. Double-Check Spring Boot Test Classpath
Since you’re using Spring Boot, make sure hibernate-validator isn’t marked as provided—it needs to be available in the test classpath. Even though you excluded it from spring-boot-starter-web, adding it as a direct dependency (without scope=provided) ensures JUnit tests can access the actual implementation.
Final Validation Steps
- After updating your
pom.xml, runmvn clean testfrom the command line, or right-click your project in Eclipse → Maven → Update Project to refresh dependencies. - Re-run your JUnit test—this should resolve the "Absent Code attribute" error.
内容的提问来源于stack exchange,提问作者Kim

