junit

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:

Figure 1.0 JUnit Test case setup/tearDown - non-annotation based approach
Figure 1.0 JUnit Test case setup/tearDown – non-annotation based approach

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:

Figure 1.0 JUnit Test case setup/tearDown - annotation based approach
Figure 1.0 JUnit Test case setup/tearDown – annotation based approach

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.

Download
You can download the full source code of this example here : junit-setupteardown-example

Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Daniel C
Daniel C
6 years ago

What about the AccountService instance? Don’t we have to make sure that it’s properly disposed ?

Back to top button