JUnit SetUp / TearDown Example
1. Introduction
When we create JUnit test cases, we would normally setup our own configuration and data objects that can be used on our test cases. We needed them to be readily available when we create each of the method test cases and mock what was actually being used by the system at runtime.
We can prepare this within the test method but what a good alternative is override the setup
and tearDown
method. These methods will be called for each test case method calls. This will allow the test case to do a prepration and post clean up process for each of the JUnit method test call.
2. The Source Code(s)
JUnitTestCaseWOAnnotation.java
package com.jgc.areyes1.junit; import com.jgc.areyes1.junit.obj.Account; import junit.framework.TestCase; public class JUnitTestCaseWOAnnotation extends TestCase { private AccountService accountService = new AccountService(); private Account dummyAccount; @Override protected void setUp() throws Exception { System.out.println("Setting it up!"); dummyAccount = accountService.getAccountDetails(); } public void testDummyAccount() { System.out.println("Running: testDummyAccount"); assertNotNull(dummyAccount.getAccountCode()); } public void testDummyAccountTransactions() { System.out.println("Running: testDummyAccountTransactions"); assertEquals(dummyAccount.getAccountTransactions().size(),3); } @Override protected void tearDown() throws Exception { System.out.println("Running: tearDown"); dummyAccount = null; assertNull(dummyAccount); } }
First things first is, we need to override the TestCase object from the JUnit Test class. This will allow the compiler to tag the class as a JUnit Test case class and have a new set of overridable methods for us to modify the behaviour of our specific class. We override the setup
and tearDown
method so that we can do the preparation as well as the clean up process for each test method available.
Here’s the output:
The example above is actually the old way of doing test cases, the new more flexible way is by using annotations to tag the class as a JUnit Test case. We then use the @Before
(setup
) and @After
(tearDown
) for our preparation and clean up. Here’s an example of the annotation based junit test case method.
JUnitTestCaseWAnnotation.java
package com.jgc.areyes1.junit; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.jgc.areyes1.junit.obj.Account; import static org.junit.Assert.*; public class JUnitTestCaseWAnnotation { private AccountService accountService = new AccountService(); private Account dummyAccount; @Before // setup() public void before() throws Exception { System.out.println("Setting it up!"); dummyAccount = accountService.getAccountDetails(); } @Test public void testDummyAccount() { System.out.println("Running: testDummyAccount"); assertNotNull(dummyAccount.getAccountCode()); } @Test public void testDummyAccountTransactions() { System.out.println("Running: testDummyAccountTransactions"); assertEquals(dummyAccount.getAccountTransactions().size(),3); } @After // tearDown() public void after() throws Exception { System.out.println("Running: tearDown"); dummyAccount = null; assertNull(dummyAccount); } }
It utilises the @Before
and @After
annotation for setup
and tearDown
method calls respectively.
Here’s the output:
3. Download the Eclipse project
This was an example of JUnit setup and tearDown, that show cases it’s usage as well as the new annotation based alternatives.
You can download the full source code of this example here : junit-setupteardown-example
What about the AccountService instance? Don’t we have to make sure that it’s properly disposed ?