NullPointerException when running a test programatically

594 views
Skip to first unread message

glen

unread,
Nov 12, 2010, 8:41:06 AM11/12/10
to testng-users
I have a custom test framework that runs all the tests in my product.
I'm now extending this framework to add support for TestNG tests. To
do this I need to run TestNG programatically, so following the
instructions in the documentation I wrote the following simple piece
of code as a proof of concept. I'm going to add some custom listeners
that will do some deeper integration with my test runner later on,
once I get a basic test running.


TestMeTestNG is a simple test that doesn't do much.

public class TestMeTestNG {
@Test
public void f() {
assert 1 == 1;
}
}

When I run TestMeTestNG using the eclipse plugin (right-click, Run as -
> TestNG Test) then the test executes and passes as expected.


Then I moved on to trying to get this test to run from some java
code.

import org.testng.TestListenerAdapter;
import org.testng.TestNG;
public class TestRunner {
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { TestMeTestNG.class });
testng.addListener(tla);
testng.run();
}
}

Then I run my code (right-click, Run as, Java Application) and I get a
NPE
Exception in thread "main" java.lang.NullPointerException
at org.testng.TestNG.runSuitesLocally(TestNG.java:884)
at org.testng.TestNG.run(TestNG.java:818)
at com.openet.testng.TestRunner.main(TestRunner.java:12)


I'm using :
Eclipse Helios (Build 20100617-1415)
CentOS 5.2 (64-bit)
I installed the testng eclipse plugin from the eclipse market place.
Looks like that installed 5.14.3beta


I've got a few questions,
1, what am I doing wrong in my TestRunner class
2, where can I get the source code for 5.14.3beta?
3, Is there a "stable" version I should be using instead of a beta and
if so, where can I get that?

I don't see any tags in svn at http://testng-eclipse.googlecode.com/svn
for this version. There's only the trunk version, so when I try to
step into the code in a debugger and see what's causing the NPE
there's a difference between the source and the .class file, so the
line numbers don't match up, which makes debugging this issue
difficult

Cédric Beust ♔

unread,
Nov 12, 2010, 10:11:46 AM11/12/10
to testng...@googlegroups.com
Hi Glen,

The tip of the tree is on github:  http://github.com/cbeust/testng

This NPE is very odd but I'm guessing the plug-in on the Eclipse Market place is a bit old. Can you uninstall it, install the Eclipse plug-in from the update site (http://beust.com/eclipse) and try again?

Thanks.

-- 
Cédric



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric


glen

unread,
Nov 12, 2010, 11:12:07 AM11/12/10
to testng-users
Hi Cédric,
Thanks for the info. I found the problem.

at line 884 of the TestNG class :
> runSuitesSequentially(xmlSuite, suiteRunnerMap, xmlSuite.getVerbose(), getDefaultSuiteName());

xmlSuite.getVerbose() returns an Integer (which is null by default).

The runSuitesSequentially method takes an int for the verbosity level,
not an Integer. auto-unboxing the null to an int results in the NPE.

Changing my TestRunner to explicitly set the verbosity level solves
the problem.

public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { TestMeTestNG.class });
testng.addListener(tla);
testng.setVerbose(-1); //this fixes the NPE
testng.run();
}


If you wish I can either log this as a bug in your JIRA project, or I
can submit a patch (though I'm not sure how to do this) to initialise
the verbosity to the DEFAULT_VERBOSE value (1)

regards,
Glen
> > I don't see any tags in svn athttp://testng-eclipse.googlecode.com/svn
> > for this version.  There's only the trunk version, so when I try to
> > step into the code in a debugger and see what's causing the NPE
> > there's a difference between the source and the .class file, so the
> > line numbers don't match up, which makes debugging this issue
> > difficult
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > To post to this group, send email to testng...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > testng-users...@googlegroups.com<testng-users%2Bunsu...@googlegroups.com>
> > .

Cédric Beust ♔

unread,
Nov 14, 2010, 11:14:21 AM11/14/10
to testng...@googlegroups.com, glen
Hi Glen,

I just fixed this in master, thanks!

-- 
Cédric


To unsubscribe from this group, send email to testng-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric


Paulo Henrique Goularte Vicente

unread,
Oct 29, 2012, 11:15:36 AM10/29/12
to testng...@googlegroups.com, glen
I tried run my tests with group tag in tests suite, but my tests didn't work. when I removed those tags, the tests were run...

somebody knows why?

the error is the same in this thread...

Sabaat Ahmad

unread,
May 21, 2014, 2:50:36 AM5/21/14
to testng...@googlegroups.com
I faced this problem when trying to run a programmatically created XmlSuite. This solution did not work for me. I looked into the XmlTest.java and found that null pointer is actually due to the XmlSuite field being null for that instance of XmlTest. The problem was that:

I was using:

XmlSuite suite=new XmlSuite();
XmlTest test=new XmlTest();
...
...
suite.addTest(test);

I changed it to:
XmlTest test=new XmlTest(suite);

this new code ensures that the suite field of the XmlTest is not null and thus solves the issue.

Rana Dinesh Himachali

unread,
Aug 16, 2018, 3:19:17 AM8/16/18
to testng-users
issue :
=====
 How to solve NULLPOINTEREXCEPTION while using grouping concepts in POM with Extends Reporting ?


Solutions: 
======
       1. Use below parameters to all the configuration annotations which we are using i.e. in base test;
(alwaysRun = true)
2. Use valid try catch block inside the page class and don’t use throws [incorrect exception]
3. Use parameterized values instead of taking hardcode values inside the page or script.
Reply all
Reply to author
Forward
0 new messages