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..
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With