jamesdeath123 jamesdeath123 - 3 months ago 10
Java Question

TestNG @BeforeGroup is not executed before the group is run

Given the following codes:

public class NewTest {

private Object _foreground = null;

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

@Test(groups="regression")
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:




<groups>
<run>
<include name="regression" />
</run>
</groups>

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


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

???
true


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

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

@BeforeGroups("regression") 

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

@BeforeGroups(groups = "regression")

Took me two days!!