views:

48

answers:

2

I am using JUnit 4, Maven 2 and latest Eclipse. Problem is simple: I would like to perform some setup (connecting to a database) before my tests are executed.

I tried @BeforeClass in many different locations but Eclipse and Maven are ignoring this. Any help on accomplishing this initial setup?

Thanks!

public abstract class BaseTestCase extends TestCase {

 @BeforeClass
    public static void doBeforeClass() throws Exception {

   System.out.println("MERDA @BeforeClass");

   // DO THE DATABASE SETUP

    }
}

Now the tests extending BaseTestCase:

public class LoginActionTest extends BaseTestCase {

 @Test
 public void testNothing() {

  System.out.println("TEST HERE");

  assertEquals(true, true);
 }
}

Maven and Eclipse just ignore my @BeforeClass ??? Any other way to perform setup before tests?

+1  A: 

I suspect that you are running with JUnit 3. Try renaming your test to something which does not start with "test". If the test is no longer executing, you are using JUnit 3 (which assumes that test methods are methods which starts with "test").

Please post your Eclipse launch config.

JesperE
+4  A: 

Sergio, you were right about extending TestCase causing the problem. If you extend TestCase, JUnit treats your test class as an old (pre JUnit 4 class) and picks org.junit.internal.runners.JUnit38ClassRunner to run it. JUnit38ClassRunner does not know about @BeforeClass annotation. Check out source code of runnerForClass method of AllDefaultPossibilitiesBuilder and runnerForClass method of JUnit3Builder for more details.

Note: This problem is not related to Eclipse or Maven.

Georgy Bolyuba
Yeah. I gave up extending TestCase and everything works fine.
Sergio Oliveira Jr.