lundi 25 mai 2020

Getting NullPointerException error while running the test cas

I want to implement the logs in my extent reports.

This is my test case file ReadDataFromJson.java where I am passing the argument in the method -

package practice.AppiumFramework;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.testng.annotations.Test;
import base.BaseClass;
import resources.TestListener1;
public class ReadDataFromJson extends BaseClass{
    @Test
    public void show() throws Exception {

        // for passing the logs
        TestListener1 obj = new TestListener1();
        obj.extentLogs("info 1");       // passing the argument 
        obj.extentLogs("info 2");
        obj.extentLogs("info 3");
    }
}

TestListener1.java

package resources;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.openqa.selenium.OutputType;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

import java.io.File;
import java.io.*;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;


public class TestListener1 implements ITestListener {
    //Extent Report Declarations
    AndroidDriver<AndroidElement> driver;
    private static ExtentReports extent = ExtentManager.createInstance();
    ThreadLocal<ExtentTest> test = new ThreadLocal<>();

    @Override
    public synchronized void onStart(ITestContext context) {
        System.out.println("Extent Reports Version 3 Test Suite started!");
    }

    @Override
    public synchronized void onFinish(ITestContext context) {
        System.out.println(("Extent Reports Version 3  Test Suite is ending!"));
        extent.flush();
    }

    @Override
    public synchronized void onTestStart(ITestResult result) {
        System.out.println((result.getMethod().getMethodName() + " started!"));
        ExtentTest extentTest = extent.createTest(result.getMethod().getMethodName(),result.getMethod().getDescription());
        test.set(extentTest);
    }

    @Override
    public synchronized void onTestSuccess(ITestResult result) {
        System.out.println((result.getMethod().getMethodName() + " passed!"));
        test.get().pass("testPassed");
    }

    @Override
    public synchronized void onTestFailure(ITestResult result) {
        System.out.println((result.getMethod().getMethodName() + " failed!"));
        test.get().fail(result.getThrowable());

}

@Override
public synchronized void onTestSkipped(ITestResult result) {
    System.out.println((result.getMethod().getMethodName() + " skipped!"));
    test.get().skip(result.getThrowable());
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
    System.out.println(("onTestFailedButWithinSuccessPercentage for " + result.getMethod().getMethodName()));
}

//mthod to pass logs in extent report
public void extentLogs(String info) {
    test.get().pass(info);
}

}

Error that I am facing

[RemoteTestNG] detected TestNG version 7.0.1
Directory already exists: C:\Users\gupta\eclipse-workspace\AppiumFramework\TestReport
ExtentReport Path for WINDOWS: C:\Users\gupta\eclipse-workspace\AppiumFramework\TestReport

FAILED: show
java.lang.NullPointerException
    at resources.TestListener1.extentLogs(TestListener1.java:72)
    at practice.AppiumFramework.ReadDataFromJson.show(ReadDataFromJson.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.testng.TestRunner.privateRun(TestRunner.java:766)
    at org.testng.TestRunner.run(TestRunner.java:587)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================

I am facing this error when I run a single test if I run this in testNG.xml than I am not facing any error can you please suggest why I am facing this error.

Aucun commentaire:

Enregistrer un commentaire