【问题标题】:Getting null pointer exception using ExtentReport使用 ExtentReport 获取空指针异常
【发布时间】:2019-03-18 00:57:33
【问题描述】:

我有 3 节课。 Test1、Test2 和基类。 Test1 和 Test2 扩展基类。当我使用 TestNg xml 执行这两个类时,@BeforeClass 失败并出现空指针异常。正在生成 test1 的报告。也没有范围报告它的工作正常。请帮帮我

=====================================基类========= ======================

public class Base
{
public ExtentReports reports;
public ExtentTest logger;
  @BeforeClass

  public void startBrowser() 
  {
      String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
      logger = reports.createTest("Test1"+name);
      System.out.println("Started Test1");

}

  @BeforeSuite
  public void initialSetup()
  {
      String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
      ExtentHtmlReporter extent = new ExtentHtmlReporter(new File("./Reports/"+name+".html"));
      reports = new ExtentReports();
      reports.attachReporter(extent); 
  }

  @AfterClass
  public void end()
  {
      reports.flush();
  }

}

============================Test1 class================== ===

public class Test1 extends Base{
  @Test
  public void TestCase() 
  {

      logger.info("Test1");
  }
}

====================Test2 class======================== ====

public class Test2 extends Base{
  @Test
  public void f() 
  {
      System.out.println("Started Test2");
  }
}

【问题讨论】:

    标签: java testng


    【解决方案1】:

    问题在于您的测试代码。 您已经在 @BeforeSuite 注释方法中初始化了数据成员 reports

    每个<suite> 标签只执行一次。 由于您的两个类都是从同一个基类扩展而来,initialSetup() 方法将只执行一次(例如,Test1.java)。

    这就是为什么当您的 @BeforeClass 注释 startBrowser() 尝试访问 reports.createTest("Test1"+name);Test2 时,您会遇到 NullPointerException。

    您需要通过从 @BeforeClass 方法中移动您的 initialSetup() 调用来更改此设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多