51

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 {
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
Lynden Shields
  • 1,062
  • 1
  • 10
  • 27

7 Answers7

72

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)

user85421
  • 28,957
  • 10
  • 64
  • 87
20

Make sure you imported @Test from the correct package.

  • Correct package: org.junit.Test
  • Incorrect pacakge: org.junit.jupiter.api.Test

Please note that this is a solution for: If your @Before, @Atter, etc did not get called at all.

Kishore
  • 201
  • 2
  • 4
17

the method must be static and not directly call fail (otherwise the other methods won't be executed).

The following class shows all the standard JUnit 4 method types:

public class Sample {

    @BeforeClass
    public static void beforeClass() {
        System.out.println("@BeforeClass");
    }

    @Before
    public void before() {
        System.out.println("@Before");
    }

    @Test
    public void test() {
        System.out.println("@Test");
    }

    @After
    public void after() {
        System.out.println("@After");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("@AfterClass");
    }

}

and the ouput is (not surprisingly):

@BeforeClass
@Before
@Test
@After
@AfterClass
Vladimir
  • 6,853
  • 2
  • 26
  • 25
2

Make sure that :

  • Your test class doesn't inherits from TestCase
  • The @BeforeClass method is static
  • You don't have more than one @BeforeClass method in test class hierarchy (only the most specialized @BeforeClass method will be executed)
ben75
  • 29,217
  • 10
  • 88
  • 134
  • Running jUnit 4.11 allows more than one @BeforeClass , one in your class and one in a super class, and calls both. – fishjd Apr 22 '16 at 16:40
2

Check your imports.

  • @Before
  • @After
  • @BeforeClass (this should be static)
  • @AfterClass (this should be static)

and @Test annotations should import from same path.

A. Berk
  • 127
  • 1
  • 3
0

In order that the before annotated function will run , I had to do the following: If you use Maven , add a dependency to Junit 4.11+:

      <properties>
              <version.java>1.7</version.java>
              <version.log4j>1.2.13</version.log4j>
              <version.mockito>1.9.0</version.mockito>
              <version.power-mockito>1.4.12</version.power-mockito>
               <version.junit>4.11</version.junit>   
              <version.power-mockito>1.4.12</version.power-mockito>
      </properties>           

and the dependency:

      <dependencies>
        <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>${version.junit}</version>
         <scope>test</scope>
       </dependency>    
        .
        .
        .
      </dependencies>

Make sure your Junit Test class is not extending The TestCase class, since this will cause overlapping with Older version:

public class TuxedoExceptionMapperTest{
   protected TuxedoExceptionMapper subject;

   @Before
   public void before() throws Exception {
     subject = TuxedoExceptionMapper.getInstance();
      System.out.println("Start");
      MockitoAnnotations.initMocks(this);
   }
}
shacharsol
  • 2,326
  • 20
  • 14
0

Things to check:

  • Junit package is: org.junit.Test
  • You don't have a static block. (I learned it the hard way)
Amrit
  • 11
  • 1