【问题标题】:Why isn't @Before being called为什么@Before 不被调用
【发布时间】:2014-09-13 08:46:20
【问题描述】:

在下面的测试类中,我的测试对象没有被初始化,因为之前没有被调用。

有什么想法吗?

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;
    }
}

【问题讨论】:

标签: java junit


【解决方案1】:

从您的代码中删除 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;
    }
}

【讨论】:

    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    相关资源
    最近更新 更多