Re: [testng-users] Exception while running testng programatically

169 views
Skip to first unread message

Krishnan Mahadevan

unread,
Mar 13, 2013, 12:56:45 AM3/13/13
to testng...@googlegroups.com
Denzil,
What exactly does your input look like ?
Have you tried printing you suite as an xml and see what it looks like?



On Wednesday, March 13, 2013, DenZie wrote:
My requirement is to run the testng programatically with a provided suite provided as string parameter.
I was able to create the the virtual suite and tests adding the class and methods to it .
But when I try to run it, I am getting this exception?
Is it a known issue?
I am using testng-6.8

Exception in thread "main" java.lang.NullPointerException
at org.testng.xml.XmlTest.getIncludedGroups(XmlTest.java:135)

Please refer the code that I have.
Anyone please suggest a workaround?

public class Runner {

public TestNG tng = new TestNG();
public XmlSuite suite = new XmlSuite();
public void runSuite(XmlSuite suite) {
ArrayList<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
tng.setXmlSuites(suites);
tng.run();
}
public void runSuite() {
ArrayList<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
System.out.println(suite.toXml());
tng.setXmlSuites(suites);
tng.run();
}

  public static void main(String[] args) {
  Runner runner = new Runner();
  HashMap<String, String> parameters = null;
  runner.createVirtualSuite(parameters);
  runner.runSuite();
  }
  
  public void createVirtualSuite(HashMap<String,String> parameters) {
String suiteName = "Bluefin runner";
// suite.setParameters(parameters);
suite.setName(suiteName);
XmlTest test = setTestXml();
suite.addTest(test);
}

public XmlTest setTestXml() {
XmlTest xmlTest = new XmlTest();
xmlTest.setName("Bluefin Test");
HashMap<String, ArrayList<String>> testMethodMap = new HashMap<String, ArrayList<String>>();
ArrayList<String> testmethods = new ArrayList<String>();
testmethods.add("mytest");
testMethodMap.put("com.myclass", testmethods);
Set<String> stClass = testMethodMap.keySet();
Iterator<String> it = stClass.iterator();
List<XmlClass> classes = new ArrayList<XmlClass>();
while(it.hasNext()) {
String testClassName = it.next();
XmlClass xmlcls = new XmlClass(testClassName);
ArrayList<String> methodslist = testMethodMap.get(testClassName);
ArrayList<XmlInclude> methodsToRun = new ArrayList<XmlInclude>();
for(String m: methodslist) {
methodsToRun.add(new XmlInclude(m));
}
xmlcls.setIncludedMethods(methodsToRun);
classes.add(xmlcls);
xmlTest.setXmlClasses(classes) ;
}
xmlTest.setVerbose(2);
return xmlTest;
}
}
 
 
 

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

DenZie

unread,
Mar 14, 2013, 12:02:22 PM3/14/13
to testng...@googlegroups.com
Krishna,
Yes I tried to print the suite object which is being added to the testng object and did a console print.

it looks something like this and looks fair to me.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
  <test verbose="2" name="Sample Test" preserve-order="false">
    <classes>
      <class name="package.class">
        <methods>
          <include name="method"/>
        </methods>
      </class> <!-- package.class -->
    </classes>
  </test> <!-- Sample Test -->
</suite> <!-- Default Suite -->
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

DenZie

unread,
Mar 14, 2013, 12:04:38 PM3/14/13
to testng...@googlegroups.com
and the null point exception is happening at this code fragment which is commented as dprecated

  public List<String> getIncludedGroups() {
    List<String> result;
    if (m_xmlGroups != null) {
      result = m_xmlGroups.getRun().getIncludes();
      result.addAll(m_suite.getIncludedGroups());
    } else {
      // deprecated
      result = Lists.newArrayList(m_includedGroups);
      result.addAll(m_suite.getIncludedGroups());
    }
    return result;
  }

On Wednesday, 13 March 2013 10:26:45 UTC+5:30, Krishnan wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Krishnan Mahadevan

unread,
Mar 15, 2013, 2:50:48 AM3/15/13
to testng...@googlegroups.com
Denzil,
The problem seems to be tied down to your code.

When you create an XmlTest object you also need to ensure that you associate the XmlTest object to a particular suite using setSuite() method.
You dont seem to have done that.

Here's the code that worked for me:

package rationale.emotions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.testng.TestNG;
import org.testng.xml.*;

public class Runner {

public TestNG tng = new TestNG();
public XmlSuite suite = new XmlSuite();
public void runSuite(XmlSuite suite) {
ArrayList<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
tng.setXmlSuites(suites);
tng.run();
}
public void runSuite() {
ArrayList<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
System.out.println(suite.toXml());
tng.setXmlSuites(suites);
tng.run();
}

  public static void main(String[] args) {
  Runner runner = new Runner();
  HashMap<String, String> parameters = null;
  runner.createVirtualSuite(parameters);
  runner.runSuite();
  }
  
  public void createVirtualSuite(HashMap<String,String> parameters) {
String suiteName = "Bluefin runner";
suite.setName(suiteName);
XmlTest test = setTestXml();
test.setSuite(suite); // This line was missing
suite.addTest(test);
}

public XmlTest setTestXml() {
XmlTest xmlTest = new XmlTest();
xmlTest.setName("Bluefin Test");
HashMap<String, ArrayList<String>> testMethodMap = new HashMap<String, ArrayList<String>>();
ArrayList<String> testmethods = new ArrayList<String>();
testmethods.add("foo");
testMethodMap.put("rationale.emotions.MyTestClass", testmethods);
Set<String> stClass = testMethodMap.keySet();
Iterator<String> it = stClass.iterator();
List<XmlClass> classes = new ArrayList<XmlClass>();
while(it.hasNext()) {
String testClassName = it.next();
XmlClass xmlcls = new XmlClass(testClassName);
ArrayList<String> methodslist = testMethodMap.get(testClassName);
ArrayList<XmlInclude> methodsToRun = new ArrayList<XmlInclude>();
for(String m: methodslist) {
methodsToRun.add(new XmlInclude(m));
}
xmlcls.setIncludedMethods(methodsToRun);
classes.add(xmlcls);
xmlTest.setXmlClasses(classes) ;
}
xmlTest.setVerbose(2);
return xmlTest;
}
}


Here's the output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Bluefin runner">
  <test verbose="2" name="Bluefin Test">
    <classes>
      <class name="rationale.emotions.MyTestClass">
        <methods>
          <include name="foo"/>
        </methods>
      </class> <!-- rationale.emotions.MyTestClass -->
    </classes>
  </test> <!-- Bluefin Test -->
</suite> <!-- Bluefin runner -->

[TestNG] Running:
  Command line suite

foo says hello
PASSED: foo

===============================================
    Bluefin Test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Bluefin runner
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@59c9b9ca: 16 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@33bfc93a: 5 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3daa57fb: 45 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@7ca3d4cf: 3 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@2d20cc56: 28 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages