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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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.

Want to be a JUnit Master ?
Subscribe to our newsletter and download the JUnit Programming Cookbook right now!
In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!

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
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

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
7 years ago

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

Back to top button