Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 715
jamesdeath123 Avatar asked Apr 15 '15 21:04

jamesdeath123


1 Answers

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!!

like image 90
jamesdeath123 Avatar answered Nov 05 '22 17:11

jamesdeath123