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

使用TestNG运行测试用例时遭遇NoClassDefFoundError问题求助

解决TestNG运行时NoClassDefFoundError: org/slf4j/LoggerFactory错误

你碰到的这个错误是因为TestNG 7.10.1依赖SLF4J(Simple Logging Facade for Java)日志组件,但你的项目里没有引入对应的依赖包。TestNG从7.x版本开始不再内置日志实现,必须手动添加SLF4J相关依赖才能正常运行。

解决方案分三种情况:

1. 使用Maven管理项目

在项目的pom.xml中添加以下依赖:

<!-- SLF4J核心API -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.9</version>
    <scope>test</scope>
</dependency>
<!-- 轻量级日志实现,也可以换成logback等其他实现 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.9</version>
    <scope>test</scope>
</dependency>

2. 使用Gradle管理项目

在项目的build.gradle中添加以下依赖:

testImplementation 'org.slf4j:slf4j-api:2.0.9'
testImplementation 'org.slf4j:slf4j-simple:2.0.9'

3. 手动管理Jar包

  • 下载SLF4J的核心Jar包slf4j-api-2.0.9.jar和一个日志实现Jar包(比如slf4j-simple-2.0.9.jar)
  • 将这两个Jar包放到项目的lib目录下
  • 在IDE中把它们添加到项目的类路径中(比如IDEA里右键Jar包选择"Add as Library")

添加完依赖后,重新运行你的TestNG测试代码,就能正常输出指定内容了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 01:52:34