Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "User:Michael.norman.oracle.com/Info on JUnit4"
(→improved fixture management) |
(→improved fixture management) |
||
Line 70: | Line 70: | ||
conn = ...; | conn = ...; | ||
} | } | ||
+ | |||
+ | '''<font color="#7f0055">public static void'''</font> tearDownDatabaseConnection() { | ||
+ | // one-time initialization code | ||
+ | conn.close() ...; | ||
+ | } | ||
+ | } | ||
JUnit4: multiple @BeforeClass/@AfterClass annotations | JUnit4: multiple @BeforeClass/@AfterClass annotations | ||
'''<font color="#7f0055">import'''</font> org.junit.AfterClass; | '''<font color="#7f0055">import'''</font> org.junit.AfterClass; |
Revision as of 15:42, 4 December 2007
Contents
Information on JUnit4
- successor to most widely used unit-testing framework for Java, JUnit3
- under active development (last JUnit3 release 3.8.2 2006-03-03)
- JUnit 4 released 2006-02-16
- 4.1 2006-04-27
- 4.2 2006-11-16
- 4.3 2006-11-16
- 4.3.1 2007-03-28
- 4.4 (current as of 071204) 2007-07-18
- JUnit 4 released 2006-02-16
based on Java 5 features
- no longer need to extend class TestCase
- test-method names do not have to start with the prefix test
- mark your test method with a @Test annotation
- use static import's to get assert methods (instead of via inheritance)
importjunit.framework.TestCase; import org.junit.Test; import static org.junit.Assert.*; public class CalculatorTestextendsTestCase { @Test public voidtestadd() { .... assertEquals(4, calculator.add( 1, 3 )); } }
improved test lifecycle management
- setUp() and tearDown() replaced with @Before and @After annotations (allows for multiple @Before/@After methods)
public class CalculatorTest { @Before public void prepareTestData() { ... } @Before public void setupMocks() { ... } @After public void cleanupTestData() { ... } }
improved fixture management
JUnit3:
import junit.framework.*; import junit.extensions.TestSetup; public class AllTestsOneTimeSetup { public static Connection conn; // database connection public static Test suite() { TestSuite suite = new TestSuite(); TestSetup wrapper = new TestSetup(suite) { protected void setUp() { setUpDatabaseConnection(); } protected void tearDown() { tearDownDatabaseConnection(); } }; return wrapper; } public static void setUpDatabaseConnection() { // one-time initialization code conn = ...; } public static void tearDownDatabaseConnection() { // one-time initialization code conn.close() ...; } }
JUnit4: multiple @BeforeClass/@AfterClass annotations
import org.junit.AfterClass; import org.junit.BeforeClass; public class TestWithDatabaseConnection { public static Connection conn; // database connection fixture public static Object foo; // another fixture @BeforeClass public static void setUpDatabaseConnection() { conn = ...; } @BeforeClass public static void setUpFoo() { foo= ...; } @AfterClass public static void tearDownDatabaseConnection() { conn.close() ...; } }