初始化ExtentReports 5.1.2变量时程序终止问题求助
ExtentReports 5.1.2初始化NoClassDefFoundError异常排查
在使用Selenium和ExtentReports 5.1.2时,出现以下异常问题:AvantewebsuiteTest类的静态代码块中,执行完System.out.println("Init Spark");后,初始化ExtentSparkReporter时程序直接终止,抛出NoClassDefFoundError异常。程序由另一个类的GUI菜单启动,无main方法。
相关代码
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class AvantewebsuiteTest { . . . private static ExtentReports extent; private static ExtentTest test; static{ try{ System.out.println("Init Spark"); ExtentSparkReporter spark = new ExtentSparkReporter("reports/AvanteWebSuiteReport.html"); System.out.println("Inited Spark"); spark.config().setReportName("Avante Web Suite - Test Report"); spark.config().setDocumentTitle("Relatório de Testes"); System.out.println("Init Extent"); extent = new ExtentReports(); System.out.println("Inited Extent"); extent.attachReporter(spark); extent.setSystemInfo("Projeto", "AvanteWebSuite"); extent.setSystemInfo("Ambiente", "QA"); extent.setSystemInfo("Usuário", System.getProperty("user.name")); } catch (Exception e) { e.printStackTrace(); throw new ExceptionInInitializerError("Failed to initialize ExtentReports: " + e.getMessage()); } } public static ExtentReports getExtentReports() { return extent; } . . .
错误日志
Init Spark Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Could not initialize class testes.avante.testes.AvantewebsuiteTest at testes.avante.ui.AvanteGUI.updateBottomPanel(AvanteGUI.java:390) at testes.avante.ui.AvanteGUI.access$1200(AvanteGUI.java:22) at testes.avante.ui.AvanteGUI$8$1.done(AvanteGUI.java:327) at java.desktop/javax.swing.SwingWorker$5.run(SwingWorker.java:750) at java.desktop/javax.swing.SwingWorker$DoSubmitAccumulativeRunnable.run(SwingWorker.java:848) at java.desktop/sun.swing.AccumulativeRunnable.run(AccumulativeRunnable.java:112) at java.desktop/javax.swing.SwingWorker$DoSubmitAccumulativeRunnable.actionPerformed(SwingWorker.java:858) at java.desktop/javax.swing.Timer.fireActionPerformed(Timer.java:311) at java.desktop/javax.swing.Timer$DoPostEvent.run(Timer.java:243) at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90) Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.NoClassDefFoundError: com/aventstack/extentreports/reporter/ExtentSparkReporter [in thread "SwingWorker-pool-1-thread-1"] at testes.avante.testes.AvantewebsuiteTest.<clinit>(AvantewebsuiteTest.java:50) at testes.avante.ui.AvanteGUI$8$1.doInBackground(AvanteGUI.java:319) at testes.avante.ui.AvanteGUI$8$1.doInBackground(AvanteGUI.java:307) at java.desktop/javax.swing.SwingWorker$1.call(SwingWorker.java:304) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.desktop/javax.swing.SwingWorker.run(SwingWorker.java:343) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:842)
环境验证
同一环境下运行以下极简测试代码时一切正常:
package testes.avante.testes; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.reporter.ExtentSparkReporter; public class ExtentSpark { public static void main(String[] args) { try { System.out.println("Initializing ExtentSparkReporter..."); ExtentSparkReporter spark = new ExtentSparkReporter("test-report.html"); System.out.println("ExtentSparkReporter initialized successfully!"); ExtentReports extent = new ExtentReports(); extent.attachReporter(spark); extent.createTest("Sample Test").pass("This test passed!"); extent.flush(); System.out.println("Report generated successfully."); } catch (Exception e) { e.printStackTrace(); } } }
已尝试方案
- 更新ExtentReports相关依赖
- 更换旧版本的ExtentReports
- 将ExtentReports的初始化逻辑移至独立类
以上方案均未解决问题,恳请协助排查该异常。
内容的提问来源于stack exchange,提问作者João Gualberto
相关产品推荐
相关产品推荐

