Home » Core Java » junit » JUnit @Before and @BeforeClass Example

About Konstantina Dimtsa

Konstantina Dimtsa
Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.

JUnit @Before and @BeforeClass Example

In this example we are going to explain the difference between the @Before and @BeforeClass annotations of JUnit testing framework, using Eclipse IDE.

1. Create JUnit test

In this section, we will create the test case that will help us explain the difference between those annotations.

Before you create the test class, it is considered as good practice to create a new source folder dedicated to tests. For this purpose, right-click your project, select New -> Source Folder, name the new source folder test and click Finish.

Figure 1: Create new source folder for junit tests.

Figure 1: Create new source folder for junit tests.

You can also have the same result by right-clicking on your project and select Properties -> Java Build Path -> Source and Add Folder.

Now we are ready to proceed with the creation of the JUnit test class.
Right-click the test source folder, create a new package com.javacodegeeks.junit and then, right-click the package and select New → JUnit Test Case.

Figure 2: Create a new junit test class.

Figure 2: Create a new junit test class.

If your project does not contain the JUnit library in its classpath, the following message will be displayed so as to add the JUnit library to the classpath.

Figure 4: Add JUnit library to the classpath.

Figure 3: Add JUnit library to the classpath.

The code of the test class which is named JunitTestExample.java, follows.

JunitTestExample.java:

package com.javacodegeeks.junit;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.*;

public class JunitTestExample {

	
	private ArrayList testList;
		 
		    @BeforeClass
		    public static void onceExecutedBeforeAll() {
		        System.out.println("@BeforeClass: onceExecutedBeforeAll");
		    }
		 
		    @Before
		    public void executedBeforeEach() {
		        testList = new ArrayList();
		        System.out.println("@Before: executedBeforeEach");
		    }
	 
		    @Test
		    public void EmptyCollection() {
		        assertTrue(testList.isEmpty());
		        System.out.println("@Test: EmptyArrayList");
		 
		    }
		 
		    @Test
		    public void OneItemCollection() {
		        testList.add("oneItem");
	        assertEquals(1, testList.size());
		        System.out.println("@Test: OneItemArrayList");
		    }

}

2. What is the difference between @Before and @BeforeClass annotations?

  • @Before
    public void method()
    The Before annotation indicates that this method must be executed before each test in the class, so as to execute some preconditions necessary for the test.
  • @BeforeClass
    public static void method()
    The BeforeClass annotation indicates that the static method to which is attached must be executed once and before all tests in the class. That happens when the test methods share computationally expensive setup (e.g. connect to database).

3. Run the JUnit test case

Now we can run the test case by right-clicking on the test class and select Run As -> JUnit Test.

If we run the above code, we will have the following results:

  • Output:
@BeforeClass: onceExecutedBeforeAll
@Before: executedBeforeEach
@Test: EmptyArrayList
@Before: executedBeforeEach
@Test: OneItemArrayList

Download the source code

This was an example of @Before and @BeforeClass annotations in JUnit testing framework.

Download
You can download the full source code of this example here : JUnitBeforeClassExample
(-1 rating, 1 votes)
Start the discussion Views Tweet it!

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 ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of