You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行JUnit测试时出现javax.validation.ConstraintViolationException代码属性缺失错误

Fixing "Absent Code attribute in method that is not native or abstract" in JUnit Tests

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-api dependency with scope=provided holds empty skeleton classes for javax.validation (including ConstraintViolationException). These classes only have method signatures, no actual code inside.
  • The provided scope 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-api and hibernate-validator in your project, the empty classes from javaee-api get 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, run mvn clean test from 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:57:50