Say you have
public class SomeTestNG { @BeforeClass public void setup() { … } @Test public void test1() { … } @Test public void test2() { … } }
You’ll get the following methods invoked:
- setup()
- test1()
- test2()
Not surprising.
What happens, though, with this?
public class SomeTestNG { @BeforeClass public void setup() { … } @Test(groups = {"database"}) public void test1() { … } @Test public void test2() { … } }
And you run the “database” group?
Does setup() get called?
It’d have to, right?
Well…

Not so much.
@BeforeClass has a groups() attribute as well, and it’s respected when you run group test suites. If you want it to run before all methods, you need to use the alwaysRun = true:
public class SomeTestNG { @BeforeClass(alwaysRun = true) public void setup() { … } @Test(groups = {"database"}) public void test1() { … } @Test public void test2() { … } }
What’s a serious humdinger–when you run the test manually in IDEA, you’re running the test outside the scope of it’s group test suite, so the @BeforeClass runs as expected. Only in ant will the @Before… fail to get called.