使用TestNG运行Selenium代码时遇Log4j NoSuchMethodError问题求助
问题:TestNG运行Selenium自动化测试时抛出NoSuchMethodError
运行测试时触发以下错误:
Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.ClassLoader[] org.apache.logging.log4j.util.LoaderUtil.getClassLoaders()'
完整控制台日志:
[RemoteTestNG] detected TestNG version 7.9.0 Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.ClassLoader[] org.apache.logging.log4j.util.LoaderUtil.getClassLoaders()' at org.apache.logging.log4j.core.impl.ThreadContextDataInjector.getProviders(ThreadContextDataInjector.java:252) at org.apache.logging.log4j.core.impl.ThreadContextDataInjector.access$000(ThreadContextDataInjector.java:53) at org.apache.logging.log4j.core.impl.ThreadContextDataInjector$ForCopyOnWriteThreadContextMap.(ThreadContextDataInjector.java:192) at org.apache.logging.log4j.core.impl.ContextDataInjectorFactory.createDefaultInjector(ContextDataInjectorFactory.java:94) at org.apache.logging.log4j.core.impl.ContextDataInjectorFactory.createInjector(ContextDataInjectorFactory.java:71) at org.apache.logging.log4j.core.lookup.ContextMapLookup.(ContextMapLookup.java:34) at org.apache.logging.log4j.core.lookup.Interpolator.(Interpolator.java:126) at org.apache.logging.log4j.core.config.AbstractConfiguration.(AbstractConfiguration.java:129) at org.apache.logging.log4j.core.config.NullConfiguration.(NullConfiguration.java:32) at org.apache.logging.log4j.core.LoggerContext.(LoggerContext.java:86) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.createContext(ClassLoaderContextSelector.java:233) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:206) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:131) at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:118) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:148) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:196) at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:137) at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:45) at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47) at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:30) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:388) at org.testng.log4testng.Logger.lambda$getLogger$0(Logger.java:30) at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) at org.testng.log4testng.Logger.getLogger(Logger.java:30) at org.testng.TestNG.(TestNG.java:111) at org.testng.remote.support.RemoteTestNGFactory7_8.createRemoteTestNG(RemoteTestNGFactory7_8.java:16) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:67)
项目pom.xml内容:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>FreeCRMTestAutomation</groupId> <artifactId>FreeCRMTest2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>FreeCRMTest2</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.18.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.9.0</version> <scope>compile</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.guicedee.services/sl4j --> <dependency> <groupId>com.guicedee.services</groupId> <artifactId>sl4j</artifactId> <version>1.0.13.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>3.0.0-beta1</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build> </project>
问题分析与解决方案
原因
这个错误是Log4j版本冲突导致的:
- TestNG 7.9.0依赖Log4j 2.x稳定版本,该版本中
LoaderUtil.getClassLoaders()方法返回ClassLoader[] - 引入的Log4j 3.0.0-beta1是预览版,重构了
LoaderUtil类,修改了getClassLoaders()的方法签名,导致TestNG调用时找不到对应方法 com.guicedee.services:sl4j依赖可能引入冲突的SLF4J/Log4j依赖,进一步加剧版本不一致
解决步骤
- 移除Log4j 3.0.0-beta1依赖,改用TestNG兼容的Log4j 2.x稳定版本(如2.20.0或更高)
- 替换SLF4J依赖,使用标准的
org.slf4j:slf4j-api和对应的Log4j绑定log4j-slf4j-impl,避免第三方包带来的冲突 - 清理Maven依赖,执行
mvn clean install -U刷新依赖缓存
修改后的pom.xml核心依赖部分
<dependencies> <!-- Selenium --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.18.0</version> </dependency> <!-- Apache POI --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.5</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</version> </dependency> <!-- TestNG --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.9.0</version> <scope>compile</scope> </dependency> <!-- SLF4J API --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> </dependency> <!-- Log4j 2.x 核心与SLF4J绑定 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.20.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.20.0</version> </dependency> </dependencies>
验证
修改后执行mvn dependency:tree检查依赖树,确保没有Log4j 3.x或冲突的SLF4J依赖,然后重新运行测试即可解决该错误。
内容的提问来源于stack exchange,提问作者Abhay
相关产品推荐
相关产品推荐

