2014-09-13 100 views
0

在下面的测试类中,我的测试对象没有被初始化,因为Before没有被调用。为什么不在@Before被调用

任何想法?

import junit.framework.TestCase; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 

public class CalcDateToolTest extends TestCase 
{ 
    DatesCalculator calc; 

    @Before 
    public void initialize() { 
     System.out.println("Before"); 
     calc = new DatesCalculator("15/06/2013", "30/06/2013"); 
    } 

    @Test 
    public void testCalcDayDiff() { 
     assertEquals(true, calc.getFromDate()); 
    } 

    @After 
    public void destroy() { 
     calc = null; 
    } 
} 
+1

你为什么要删除旧的帖子?我当时正确地发表了对相关问题的评论......可能有一个答案:http://stackoverflow.com/a/733358/1993402。结论是:'不要同时扩展TestCase和使用注释!' – ConcurrentHashMap 2014-09-13 09:54:40

+0

可能的重复[为什么不是我的@BeforeClass方法正在运行?](http://stackoverflow.com/questions/733037/why- isnt-my-before-class-method-running) – Joe 2014-09-13 10:43:31

回答

0

从您的代码中删除extends TestCase

如:

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 

public class CalcDateToolTest { 
    DatesCalculator calc; 

    @Before 
    public void initialize() { 
     System.out.println("Before"); 
     calc = new DatesCalculator("15/06/2013", "30/06/2013"); 
    } 

    @Test 
    public void testCalcDayDiff() { 
     assertEquals(true, calc.getFromDate()); 
    } 

    @After 
    public void destroy() { 
     calc = null; 
    } 
} 
+0

Bingo,谢谢!!! – Joly 2014-09-13 10:16:20

+0

嗨@Joly,[请投我的答案](http://stackoverflow.com/tour)。 – Visruth 2014-10-07 15:36:28

相关问题