Discussion:
@BeforeClass not working
BobFedorchak
2006-03-10 15:59:04 UTC
Permalink
While looking at JUnit I wrote a quick test that used the @BeforeClass
annotation, yet the method never executed when the test was executed.
Seeing as I am new to JUnit, I suspected it was my code, so to further test
I cut/pasted the sample code from the JUnit FAQ and used that instead. To
my surprise, the methods marked with @BeforeClass and @AfterClass did not
execute, yet the other test methods did. Here is the code I used:

import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
import junit.framework.TestCase;
import java.util.*;

public class MyTest extends TestCase {

private Collection collection;

@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("oneTimeSetUp");
}

@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println("oneTimeTearDown");
}

@Before
public void setUp() {
collection = new ArrayList();
System.out.println("setUp");
}

@After
public void tearDown() {
collection.clear();
System.out.println("tearDown");
}

@Test
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
}

@Test
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
}
}

The only change from the code in the FAQ is the addition of the System.out
calls and the fact that MyTest extends TestCase. As output from this I
received:

.setUp
tearDown
.setUp
tearDown

Time: 0

OK (2 tests)

Has anyone else had aproblem with @BeforeClass? Any help you can provide
would be greatly appreciated.

Bob

--
View this message in context: http://www.nabble.com/%40BeforeClass-not-working-t1259756.html#a3341515
Sent from the JUnit - User forum at Nabble.com.






Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/

<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Kent Beck
2006-03-16 16:41:19 UTC
Permalink
Bob,

Thank you for the report. The problem is the same one experienced by the
person trying to use Cactus with JUnit 4. If JUnit 4 sees a subclass of
junit.framework.TestCase, it assumes the tests are in the old style. If you
change the name of your test methods (and setUp and tearDown), they will no
longer be run. The solution in your case is to remove "extends TestCase".
Then I see the behavior I would expect:

oneTimeSetUp
setUp
tearDown
setUp
tearDown
oneTimeTearDown

Sincerely yours,

Kent Beck
Three Rivers Institute
-----Original Message-----
Behalf Of BobFedorchak
Sent: Friday, March 10, 2006 7:59 AM
annotation, yet the method never executed when the test was executed.
Seeing as I am new to JUnit, I suspected it was my code, so
to further test
I cut/pasted the sample code from the JUnit FAQ and used that
instead. To
@AfterClass did not
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
import junit.framework.TestCase;
import java.util.*;
public class MyTest extends TestCase {
private Collection collection;
@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println("oneTimeTearDown");
}
@Before
public void setUp() {
collection = new ArrayList();
System.out.println("setUp");
}
@After
public void tearDown() {
collection.clear();
System.out.println("tearDown");
}
@Test
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
}
@Test
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
}
}
The only change from the code in the FAQ is the addition of
the System.out
calls and the fact that MyTest extends TestCase. As output
from this I
.setUp
tearDown
.setUp
tearDown
Time: 0
OK (2 tests)
can provide
would be greatly appreciated.
Bob
--
http://www.nabble.com/%40BeforeClass-not-working-t1259756.html
#a3341515
Sent from the JUnit - User forum at Nabble.com.
Yahoo! Groups Links
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/

<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
Sergey Malyarov
2006-03-16 21:27:39 UTC
Permalink
This is because old test runner is used with classes that extend
junit.framework.TestCase.
You can make JUnit run your tests with new test runner by using
@RunWith annotation.

Sergey
Post by BobFedorchak
annotation, yet the method never executed when the test was executed.
Seeing as I am new to JUnit, I suspected it was my code, so to further test
I cut/pasted the sample code from the JUnit FAQ and used that instead. To
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
import junit.framework.TestCase;
import java.util.*;
public class MyTest extends TestCase {
private Collection collection;
@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println("oneTimeTearDown");
}
@Before
public void setUp() {
collection = new ArrayList();
System.out.println("setUp");
}
@After
public void tearDown() {
collection.clear();
System.out.println("tearDown");
}
@Test
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
}
@Test
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
}
}
The only change from the code in the FAQ is the addition of the System.out
calls and the fact that MyTest extends TestCase. As output from this I
.setUp
tearDown
.setUp
tearDown
Time: 0
OK (2 tests)
would be greatly appreciated.
Bob
--
http://www.nabble.com/%40BeforeClass-not-working-t1259756.html#a3341515
Sent from the JUnit - User forum at Nabble.com.
Yahoo! Groups Links
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/junit/

<*> To unsubscribe from this group, send an email to:
junit-***@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/

Loading...