0

In the following test class my test object doesn't get initialised because Before isn't being called.

Any ideas?

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;
    }
}
Visruth
  • 3,430
  • 35
  • 48
Joly
  • 3,218
  • 14
  • 44
  • 70
  • 1
    Why did you remove your old post? I was right at that moment posting a comment for a related question... There might be an answer: http://stackoverflow.com/a/733358/1993402. The conclusion of that: `do NOT extend TestCase AND use annotations at the same time!` – ConcurrentHashMap Sep 13 '14 at 09:54
  • possible duplicate of [Why isn't my @BeforeClass method running?](http://stackoverflow.com/questions/733037/why-isnt-my-beforeclass-method-running) – Joe Sep 13 '14 at 10:43

1 Answers1

0

Remove extends TestCase from your code.

Eg :

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;
    }
}
Visruth
  • 3,430
  • 35
  • 48