javajunit4extentreportsdata-driven-testsselenium-extent-report

Is there an equivalent class like ITestResult(using testng) that is available with just JUnit?


I am currently using ExtentReport with TestNG to get the results of my test. I am now restricted to using only JUnit so I was wondering how I would convert my current code to have the same functionality without using TestNG

I have seen examples of others using either TestWatcher or RunListener but I am not really sure how to implement it for what I need. Especially being new to everything that I am using

@BeforeTest
 public void setup() {
     htmlReporter = new ExtentHtmlReporter(new File(fileLocation));
     htmlReporter.loadXMLConfig(new File(FileReaderManager.getInstance().getConfigReader().getReportConfigPath()));
     reports = new ExtentReports();
     reports.attachReporter(htmlReporter);
}

@AfterTest
public void cleanup() {
    reports.flush();
}

@BeforeMethod
public void register(Method method) {
    String testName = method.getName();
    testInfo = reports.createTest(testName);

}

@AfterMethod
public void captureStatus(ITestResult result) {
    if (result.getStatus() == ITestResult.SUCCESS) {
        testInfo.log(Status.PASS, "Test: " + result.getName() + " passed");
    } else if (result.getStatus() == ITestResult.FAILURE) {
        testInfo.log(Status.PASS, "Test: " + result.getName() + " failed");
        testInfo.log(Status.FAIL, "Test FAILURE: " + result.getThrowable());
    } else if (result.getStatus() == ITestResult.SKIP) {
        testInfo.log(Status.PASS, "Test: " + result.getName() + " skipped");
    }
}

`

I would like to repoduce the same results from these functions without the use of ITestResult and the rest of the testNG annotations


Solution

  • What you will want to do is create a rule for handling test results. This rule will be created as a TestWatcher, then you will override its failed and succeeded methods. Example:

    @Rule
    public TestWatcher watchman = new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
            // Handle logging of failing tests
            System.out.println("Fail!");
        }
    
        @Override
        protected void succeeded(Description description) {
            // Handle logging of succeeding tests
           System.out.println("Success!");
        }
    };
    

    The description parameter contains information about the failing class http://junit.sourceforge.net/javadoc/org/junit/runner/Description.html

    As for the method annotations, the following annotations are the TestNG - JUnit 4 equivalents:

    @BeforeTest - @BeforeClass

    @AfterTest - @AfterClass

    @BeforeMethod - @Before

    @AfterMethod - @After

    The following code demonstrates how you could structure your test class:

    public class MyTestClass {
        @Rule
        public TestWatcher watchman = new TestWatcher() {
            @Override
            protected void failed(Throwable e, Description description) {
                // Handle logging of failing tests
                System.out.println("Fail!");
            }
    
            @Override
            protected void succeeded(Description description) {
                // Handle logging of succeeding tests
                System.out.println("Success!");
            }
        };
    
        @BeforeClass
        public static void setup() {
            //Some setup before all test methods
        }
    
        @AfterClass
        public static void cleanup() {
            // Some cleanup after all test methods
        }
    
        @Before
        public void register() {
            // Some setup before each test method
        }
    
        @Test
        public void succeedingTest() {
            Assert.assertTrue(true);
        }
    
        @Test
        public void failingTest() {
            Assert.assertTrue(false);
        }
    }