JUnit 4.x: why is @Before never executed?

When I create and run unit-test (in Eclipse (Galileo) with JUnit 4.5 or 4.82),
@Before never executes (?).

The following is sample code. I expect the result to be as follows:

  initialize  
  testGetFour

But it is simple:

  testGetFour

@BeforeClass and @AfterClass are also not executed.
Can someone tell me how it happened?

public class SomeClass
{
  public static int getFour()
  {
    return 4;
  }  
}

//---

import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase;

public class TestSomeClass extends TestCase
{
  @Before 
  public void initialize() // This method will never execute (?!).
  {
    System.err.println("initialize"); 
  }

  @Test
  public void testGetFour()
  {
    System.err.println("testGetFour");        
    assertEquals(4, SomeClass.getFour());
  }  
}
+3
source share
2 answers

Because you are expanding TestCase. This is a JUnit3 class, and therefore Eclipse considers it as such. JUnit 4 does not require the test class to extend everything.

Remove this, it should work fine.

+6
source

TestCase (JUnit 3 JUnit), .

+3

Source: https://habr.com/ru/post/1756622/


All Articles