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

Maven项目测试时报NoClassDefFoundError: io/micrometer/prometheus/PrometheusMeterRegistry

解决Java.lang.NoClassDefFoundError: io/micrometer/prometheus/PrometheusMeterRegistry问题
  • 检查测试阶段的依赖可用性
    确认micrometer-registry-prometheus的scope是否为compile(默认值),要是不小心设成了runtime,测试代码就拿不到这个依赖。可以显式声明scope,确保测试类能访问:

    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <version>1.13.13</version>
        <scope>compile</scope>
    </dependency>
    
  • 排查测试插件的依赖传递问题
    用Surefire或Failsafe插件时,可能出现依赖隔离。试试在插件配置里显式引入这个依赖,保证测试运行时能加载到:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.2.5</version>
                <configuration>
                    <additionalClasspathElements>
                        <additionalClasspathElement>${maven.dependency.io.micrometer.micrometer-registry-prometheus.jar}</additionalClasspathElement>
                    </additionalClasspathElements>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  • 查看依赖树找冲突或缺失
    执行mvn dependency:tree,搜micrometer-registry-prometheus,看它是不是被正确引入,有没有被其他依赖排除。要是发现有排除,就在对应依赖里取消排除:

    <dependency>
        <!-- 这里是存在排除的依赖 -->
        <groupId>xxx</groupId>
        <artifactId>xxx</artifactId>
        <exclusions>
            <exclusion>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-registry-prometheus</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  • 检查IDE的测试类路径(如果用IDE跑测试)
    要是在IDE里跑测试出问题,可能是IDE类路径和Maven不一致。试试:

    • 刷新Maven项目(IntelliJ右键项目选Maven -> Reload;Eclipse右键选Maven -> Update Project)
    • 清IDE缓存(IntelliJ去File -> Invalidate Caches;Eclipse点Project -> Clean)
  • 直接用命令行跑测试
    别用IDE,直接执行mvn test。如果命令行正常,那就是IDE的问题;要是还报错,接着查Maven配置。

内容的提问来源于stack exchange,提问作者user30933511

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 18:42:17