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.
Related posts:
- Simple spring integration testing
Spring has had a really nice unit test framework available for a while now, but the documentation can be a bit daunting. Here’s a super-simple example of adding dependency injection...... - Hibernate Naming Strategies
Using Hibernate annotations with the default naming strategy leaves you with camelCasedColumnNames in your database schema. Gavin King provided a good camelCase to underscore_separated naming strategy with org.hibernate.cfg.ImprovedNamingStrategy. The only...... - IDEA versus Eclipse
IDEA has been giving me grief lately. Fed up with mysterious pauses, bugs in svn moves, and crashes even after performing cache-deletion voodoo in IDE, I’m back to trying Eclipse....... - HOWTO: Configure an Amazon RDS instance to use UTF-8
RDS has been working out pretty well for AdGrok — it’s a one-click MySQL 5.1 instance that seems pretty promising: Automatic replication and failover to another deployment zone (colo) Easy......