Getting null pointer exception using ExtentReport - java

I have 3 class . Test1,Test2 and Base class. Test1 and Test2 extends Base class. When i am executing both classes using TestNg xml it fails #BeforeClass with null pointer exception. Report of test1 is getting generated. Also without extent report its working fine. Please help me
====================================Base Class==============================
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");
}
}

The problem lies in your test code.
You have initialised the data member reports in a #BeforeSuite annotated method.
This gets executed only once per <suite> tag.
Since both your classes are extending from the same base class, the initialSetup() method will get executed only once (for e.g., Test1.java).
That is why when your #BeforeClass annotated startBrowser() attempts at accessing reports.createTest("Test1"+name); for Test2 you hit a NullPointerException.
You need to change this by moving your initialSetup() invocation from within a #BeforeClass method.

Related

How to instantiate objects before testNG class?

I have multiple testNG test classes that extend a Base Test class and all use the same common objects. I want to have the object creation done automatically in BaseTest so I don't have to include it in each test class. As of now, the code only works if I add createPages() to the start of the test. I tried putting them in the BaseTest class using #BeforeClass and #BeforeSuite but both gave a null pointer exception meaning they weren't instantiated before the #Test test123 was run I beleive.
public someTest extends BaseTest {
#Test
public void test123(){
createPages(); //i want to be able to remove this and have it done in BaseTest
menuPage.scroll();
}
}
public BaseTest {
MenuPage menuPage;
public void createPages() {
menuPage = new MenuPage(getDriver());
}
/*
#BeforeSuite
public void beforeSuite() {
createPages();
}
#BeforeClass
public void beforeClass() {
createPages();
}
*/
}
#beforeTest is one such annotation. A method with #beforeTest annotation will run, before any test method belonging to the classes inside the test tag
inside ur BaseTest
public class Basetest{
#BeforeTest
public void doBeforeTest() {
createPages();
}
}

How to execute a piece of code once before all test classes start?

How can I execute a method once before all tests in all classes start?
I have a program that needs a system property to be set before any test start. Is there any way to do that?
Note: #BeforeClass or #Before are used just for the same test class. In my case, I'm looking for a way to execute a method before all test classes start.
To setup precondition to your test cases you can use something like this -
#Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}
if you need to run that method before the start of all test you should use the annotation #BeforeClass or if you need to execute the same method every time you will execute a test method of that class you must use #Before
f.e
#Before
public void executedBeforeEach() {
//this method will execute before every single test
}
#Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}
You can make use of a Test Suite.
The test suite
#RunWith(Suite.class)
#Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {
#BeforeClass
public static void setup() {
// the setup
}
}
and, the test classes
public class Test2Class {
#Test
public void test2() {
// some test
}
}
public class TestClass {
#Test
public void test() {
// some test
}
}
Or, you can have a base class which handles the setup
public class TestBase {
#BeforeClass
public static void setup() {
// setup
}
}
and, then the test classes can extend the base class
public class TestClass extends TestBase {
#Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
#Test
public void test() {
// some test
}
}
However, this will call the setup method in TestBase for all its subclasses everytime each of them executes.

JUnit - Use method in test from another class

The scenario is the following:
I have a class that executes tests for a section of a project.
public class section1 extends BaseTest {
#Test
public void test1() {
//somecode
}
}
The thing is that in test1, I want to use a Method that's defined in another test class (we're not using Page Objects in the automation project).
public class section2 extends BaseTest {
#Test
public void test2() {
//somecode
methodOne();
}
void methodOne(){
//somecode
}
}
So, I've tried creating an instance and access to the method through it
section2 sec2 = new section2();
.
.
.
#Test
public void test1() {
//somecode
section2.methodOne();
}
But it throws a NullPointerException as soon as it starts executing the method.
Then, I tried just extending the class section1 to section2 and use the method, so here it executes the method but, as soon as test1 finishes, it starts executing the tests from the extended class, in this case, test2.
How can I do to prevent that, or is there another way to use the method without extending and not making the method public?
Section 1 extends BaseTest
Section 2 extends BaseTest
Why don't you try this :
Section 2 extends Section 1
Add the below code in the section1 class:
section2 sec2;
#Before
public void initialize() {
sec2 = new section2();
}
As we are using the #Before JUnit annotation above, this 'initialize()' method will get executed first there by initializing the 'section2' class.
#Test
public void test1() {
sec2.methodOne();
}
Then in the #Test JUnit method will get executed so you won't get NPE.

How to use #BeforeTest in separate java file in testng

I am writing automation testing script for a project . Here , I will have many test classes for which I will be using same #BeforeTest Method . I tried by creating a base class (in which I declared before test method ) and extending them in my test classes . But its not working . Is there any other way to have a common beforeTest method in a seperate java file and use it for all my test classes .
Using a base class for my other classes works for me. You should use #BeforeMethod for your needs since #BeforeTest is used to run any #Test included in the <test> tag in a testNG.xml file.
public class BaseClass {
#BeforeMethod
public void before() {
System.out.println("Before method");
}
}
and then
public class ATestClass extends BaseClass {
#Test
public void testOne() {
System.out.println("testOne run");
}
#Test
public void testTwo() {
System.out.println("testTwo run");
}
}
gave me the result
Try it out!
If you are using PowerMockito or Mockito you can achieve by following.
public abstract class ParentClass{
#Before
public void init() {
System.out.println("inniinii");
}
}
#RunWith(PowerMockRunner.class)
public class ChildClass extends ParentClass{
#Test
public void test() {
System.out.println("hello test");
}
}

TestNG how to skip test when beforetest fails in inheritance

I have a little problem with testNG. Below my example code:
abstract class parent {
abstract void beforeTest();
#Test
void test() {
// some testing
}
}
class child extends parent {
#BeforeTest
void beforeTest() {
\\some before things
}
}
And the question is how to do this code work properly? So I want to perform beforeTest() method and if it fails the test method shoud skip. How can I achieve this thing?
Usually, the configuration methods go into the parrent and test classes should extend the parrent.
So, try using this example for your tests:
abstract class TestBase {
#BeforeTest
public void beforeTest() {
// do config here
// this will run for each of you <test> tag in your testng.xml suite
}
#BeforeMethod
public void beforeMethod() {
// do some config here
// this will run for each method annotated with #Test
}
}
class SomeTestClass extends TestBase {
#Test
public void some_test() {
// some testing
}
}

Categories

Resources