jamesdeath123 jamesdeath123 - 1 year ago 94
Java Question

TestNG @BeforeGroup is not executed before the group is run

Given the following codes:

public class NewTest {

private Object _foreground = null;

public void setUp() {
System.out.println("executed? setUp");
_foreground = new MyObject();

public void testMyObjectToString() throws Exception {
System.out.println("??? ");
System.out.println(_foreground == null);
String value = _foreground.toString();
Assert.assertTrue(value != null);

And the testNG.xml:

<include name="regression" />

<class name="com.automation.test.NewTest"/>

When I tried to run this, the print statements say:


So that means _foreground is null, meaning the setUp method is not executed.
TestNG also shows java.lang.NullPointerException on the line:

String value = _foreground.toString();

However I have no idea what I missed. Looks to me the "regression" group will be run and the setUp method with @beforeGroup will be run before testMyObjectToString with @Test. Apparently this is not what is happening..

Answer Source

It is a very stupid mistake that maybe someone new to testNG may make...


This is a wrong usage.. The correct usage should be:

@BeforeGroups(groups = "regression")

Took me two days!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download