BobFedorchak
2006-03-10 15:59:04 UTC
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/
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/