NullPointerException in ITestResult when failed @BeforeClass method

913 views
Skip to first unread message

Алексей Панащенко

unread,
Aug 14, 2013, 3:44:04 AM8/14/13
to testng...@googlegroups.com
Hi!
I use TesNG and I created my Listener. In this Listener I implemented IInvokedMethodListener2 and @Override beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult), afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult).
In afterInvocation() I catch exeption and i see problem, if my test failed in @Test method -  iTestResult.getThrowable() returned exeption, but if my test failed in @BeforeClass method - iTestResult = null and system freezes. Maybe, need change returned result when test filed in @BeforeClass method.

Krishnan Mahadevan

unread,
Aug 21, 2013, 12:27:42 AM8/21/13
to testng...@googlegroups.com
The afterInvocation() gets invoked both for @Test methods and also for configuration methods as well.

I don’t think the ITestResult object is NULL in this case. I believe that getThrowable() is returning a null and your code seems to be doing something with it.
Can you please try creating a simple example that can show this problem ?

Here's a simple example I tried :

package organized.chaos;


import org.testng.annotations.Listeners;

import org.testng.annotations.Test;

import org.testng.annotations.BeforeClass;


@Listeners({ MyMethodListener.class, MyConfigMethodListener.class })

public class MySampleTest {

    @Test

    public void f() {

        System.out.println("Hello world");

    }


    @BeforeClass

    public void beforeClass() {

        throw new RuntimeException("Hi am an exception");

    }


}


package organized.chaos;


import org.testng.IInvokedMethod;

import org.testng.IInvokedMethodListener;

import org.testng.ITestResult;


public class MyMethodListener implements IInvokedMethodListener {


    @Override

    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {


    }


    @Override

    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {

        if (testResult.getThrowable() != null) {

            System.out.println(":::: " + testResult.getThrowable().getLocalizedMessage());

        }

    }

}


package organized.chaos;


import org.testng.IConfigurationListener;

import org.testng.ITestResult;


public class MyConfigMethodListener implements IConfigurationListener {


    @Override

    public void onConfigurationSuccess(ITestResult itr) {


    }


    @Override

    public void onConfigurationFailure(ITestResult itr) {

        System.out.println(">>>> " + itr.getThrowable().getLocalizedMessage());


    }


    @Override

    public void onConfigurationSkip(ITestResult itr) {


    }


}



Output :

[TestNG] Running:

  /private/var/folders/47/hs5x_y397rsf8vbfqmqrmngm0000gn/T/testng-eclipse-285803179/testng-customsuite.xml


:::: Hi am an exception

>>>> Hi am an exception

FAILED CONFIGURATION: @BeforeClass beforeClass

java.lang.RuntimeException: Hi am an exception

at organized.chaos.MySampleTest.beforeClass(MySampleTest.java:16)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)

at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)

at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)

at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)

at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)

at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)

at org.testng.TestRunner.privateRun(TestRunner.java:767)

at org.testng.TestRunner.run(TestRunner.java:617)

at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)

at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)

at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)

at org.testng.SuiteRunner.run(SuiteRunner.java:240)

at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)

at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)

at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)

at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)

at org.testng.TestNG.run(TestNG.java:1057)

at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)

at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)

at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


SKIPPED: f










Thanks and Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages