开发者

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

开发者 https://www.devze.com 2023-01-09 11:06 出处:网络
When I create an and run unit-test, (in Eclipse (Galileo) with JUnit 4.5 or 4.82), the @Before is never executed (?).

When I create an and run unit-test, (in Eclipse (Galileo) with JUnit 4.5 or 4.82),

the @Before is never executed (?).

Below is some sample-code. I would expect the output to be :

  initialize  
  testGetFour

But it is just :

  testGetFour

@BeforeClass and @AfterClass are never executed either.

Can someone tell me how come ?

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

//---

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

public class TestSom开发者_开发知识库eClass 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());
  }  
}


Because you're extending TestCase. This is a JUnit3 class, and so Eclipse treats it as such. JUnit 4 does not require the test class to extend anything.

Remove that, it should work fine.


You should not extend from TestCase (JUnit 3 way of using JUnit) and it will work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号