Add JUnit5 to an existing Java Eclipse project

This article addresses the issue where you have created a Java project and are now looking to add JUnit unit testing to the project.

It walks through how to set up JUnit5 testing in Eclipse on an existing project.

So now we understand what we are going to cover lets make a start.

Getting Started

Open Eclipse and navigate to your project using the Navigator. 

Highlight your project 

Select from the menu bar Project->Properties 

This opens the Java Build Path dialogue.

Navigate to the Source tab. 

Setting a folder for Test Classes

Press the Add Folder button. 

Press the Create New Folder button. 

Enter “test” as the new folder name and press the Finish button. 

Press the OK button on the next dialogue. 

The Java Build Path dialog is enabled. 

Select the check box “Allow output folders for source folders” located at the bottom left of the dialogue. 

This allows the build process to use different locations between building the source and the tests. 

Under the “<Project Name>/src” folder select the Excluded filter and press the Edit button. 

This opens the Inclusion and Exclusion dialogue. 

In the Exclusion section enter “**/test” and press the Finish button. 

This ensures the test classes are not included in the application build. 

Under the “<Project Name>/test” folder select the Output folder option and press the Edit button. 

The Source Folder Output Location dialog appears.  Type in the location as “<Project Name>/target/test” and press the “OK” button. 

Under the “<Project Name>/test” folder select the Contains test source option and press the Toggle button so the value changes from “No” to “Yes”. 

The finished entries look like this. 

Press the “Apply and Close” button. 

Creating the first Test Class

Now we have set up the project to separate the build from the test we can add our first JUnit test case and at the same time add the library to the project. 

Highlight a class within your project for which you would like to write test cases. 

From the menu bar select File -> New -> JUnit Test Case. 

The JUnit Test Case dialogue appears prepopulated with values for the test class. 

Note that the Class name is the same as the class you want to test with “Test” on the end.  This is the convention for JUnit test classes. 

Press the “Next” button. 

The Test Methods dialog displays the public methods for which you can writes your tests against. 

Select those methods you would like to test. 

Press the “Finish” button. 

If this is the first test class added to the project you will be asked if you want to add the JUnit library to the build path.   

Ensure “Perform the following action:” is selected and the “Add JUnit 5 library to the build path” is highlighted and press the “OK” button. 

The class stub presented will look something like this. 

If this is the first test class, you will need to update the module-info.java file by adding this line “ requires org.junit.jupiter.api;”. 

The Test classes are created under the “test” folder, not the “src” folder.

You are now ready to edit your test class and add your test cases! 

Write a test case to prove everything works

The default class created has a test method for each of the methods selected in the target class.  Each of these test methods is set to return a failure with a message of “Not yet implemented”. 

Let us say we have a class that has two methods 

  1. Public void getMsg(String key, String value) 
  1. Public String getMsg(String key) 

We could write a test for getMsg(String key) something like this: 

@Test 

	void testGetMsg() { 
		PropertiesAccess pa = PropertiesAccess.getInstance(); 
		pa.setProperty("id", "123456"); 
		assertTrue(pa.getProperty("id").toString().equals("123456")); 
	} 

Below is the test class in full. 

To run the test, select Run->Run As->JUnit Test. 

The tests are run and the results are presented as shown below. In this example the test is successful. 

You now have seen how to set up JUnit5 testing in Eclipse on an existing project. 

If you are looking for more information about JUnit 5 then visit their site: https://junit.org/junit5/

Leave a Reply

Your email address will not be published.

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