Search code examples
javajunitannotations

Why isn't my @BeforeClass method running?


I have the following code:

    @BeforeClass
    public static void setUpOnce() throws InterruptedException {
        fail("LOL");
    }

And various other methods that are either @Before, @After, @Test or @AfterClass methods.

The test doesn't fail on start up as it seems it should. Can someone help me please?

I have JUnit 4.5

The method is failing in an immediate call to setUp() which is annotated as @before. Class def is :

public class myTests extends TestCase {

Solution

  • do NOT extend TestCase AND use annotations at the same time!
    If you need to create a test suite with annotations, use the RunWith annotation like:

    @RunWith(Suite.class)
    @Suite.SuiteClasses({ MyTests.class, OtherTest.class })
    public class AllTests {
        // empty
    }
    
    
    public class MyTests {  // no extends here
        @BeforeClass
        public static void setUpOnce() throws InterruptedException {
            ...
        @Test
        ...
    

    (by convention: class names with uppercase letter)