JUnit 4 Simple Basic Template

1. Overview

In this post, we will see the JUnit 4 basic template that all the JUnit test cases follow. This post also explains the basic usage of JUnit annotations.
The following templates are a good starting point. Copy/paste and edit these templates to suit your coding style.

2. Basic Test Template

import org.junit.*;
import static org.junit.Assert.*;
 
public class SampleTest {
 
    private java.util.List emptyList;
 
    /**
     * Sets up the test fixture. 
     * (Called before every test case method.)
     */
    @Before
    public void setUp() {
        emptyList = new java.util.ArrayList();
    }
 
    /**
     * Tears down the test fixture. 
     * (Called after every test case method.)
     */
    @After
    public void tearDown() {
        emptyList = null;
    }
    
    @Test
    public void testSomeBehavior() {
        assertEquals("Empty list should have 0 elements", 0, emptyList.size());
    }
 
    @Test(expected=IndexOutOfBoundsException.class)
    public void testForException() {
        Object o = emptyList.get(0);
    }
}

3. Basic Test Template with Fixture

You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class.
Here's a simple example:
package junit;
 
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
 
public class SimpleTest {
 
    private Collection collection;
    
    @BeforeClass
    public static void oneTimeSetUp() {
        // one-time initialization code        
    }
 
    @AfterClass
    public static void oneTimeTearDown() {
        // one-time cleanup code
    }
 
    @Before
    public void setUp() {
        collection = new ArrayList();
    }
    
    @After
    public void tearDown() {
        collection.clear();
    }
 
    @Test
    public void testEmptyCollection() {
        assertTrue(collection.isEmpty());
    }
    
    @Test
    public void testOneItemCollection() {
        collection.add("itemA");
        assertEquals(1, collection.size());
    }
}
Given this test, the methods will execute in the following order:
oneTimeSetUp()
setUp()
testEmptyCollection()
tearDown()
setUp()
testOneItemCollection()
tearDown()
oneTimeTearDown()

4. Real-World Example using JUnit Framework

Let's understand a little bit more about using JUnit tests in a real-world example.
Consider we have a School Management System application and we want to write a sample test cases for the School entity. 
In this example, we will see the sample CRUD JUnit test cases for the School entity:
public class SchoolServiceImplTest { 
    private SchoolService schoolService;
       
    /**
     * This method initialize all domain objects required for test methods.
     */
    @Before
    public final void setUp() throws Exception {

        // Instantiates a School instance;
        school = new School();
        school.setName("sms School");
        school.setAddress("sms School");
        school.setContactNo("0123456789");
        school.setFaxNo("0123456789");
school.setWebsite("javaguides.net"); school.setStartedDate(date); school.setModifiedTime(date); } @Test public final void testAddSchool() throws Exception { School newSchool = schoolService.addSchool(school); assertNotNull("School Type object should not null ", newSchool); } @Test public final void testUpdateSchool() { School newSchool = schoolService.addSchool(school); assertNotNull(newSchool); newSchool.setContactNo("0145785545"); schoolService.updateSchool(newSchool); } @Test public final void testFindSchool() throws AkuraAppException { School newSchool = schoolService.addSchool(school); assertNotNull(newSchool); School findSchool = schoolService.findSchool( newSchool.getSchoolId()); assertNotNull("School Type object should not null ", findSchool); } @Test public final void testGetSchoolList() throws AkuraAppException { School newSchool = schoolService.addSchool(school); assertNotNull(newSchool); List<School> schoolList = schoolService.getSchoolList(); } @After public final void teardown() throws SchoolNotFoundException { schoolDao.delete(school); } }

4. Conclusion

In this post, we have learned about JUnit basic template from which we can edit and write test cases. We also have seen the usage of the fixture and written test cases to a real-world example of School Management System application.
The source code of this article available on my GitHub repository at https://github.com/RameshMF/junit-developer-guide.

Comments