How to Migrate From JUnit 4 to JUnit 5 Step by Step
How to Migrate From JUnit 4 to JUnit 5 Step by Step
In this tutorial, we take a look at how you can create and migrate a basic Spring Boot application from JUnit 4 to the refreshed JUnit 5.
Join the DZone community and get the full member experience.Join For Free
The majority of the organizations are facing hurdles in digital transformations due to technical debt. Unit tests are one of them. One of the unit testing frameworks is JUnit. The industry was mostly using version 4 of JUnit to write unit tests. JUnit 5 has been in the market for quite some time, but the migration of JUnit 5 has not picked up steam. This is due to the lack of a clear migration path for developers.
In this exercise you will learn end-to-end migration from JUnit 4 to JUnit 5. You will first start with a basic Spring Boot project using JUnit 4. The project will be a simple calculator application with dependency classes to see how mocking frameworks like Mockito work along with JUnit. You will write a unit test for this calculator application. You will then introduce JUnit 5 in the JUnit 4 project. You will exclude JUnit 4 dependencies. Finally, you will migrate the JUnit 4 test class to JUnit 5. Please note that JUnit 5 is not backward compatible with JUnit 4, but the JUnit team created the JUnit Vintage Project to support JUnit 4 test cases on top of JUnit 5.
You may also enjoy: Take Unit Testing to the Next Level With JUnit 5
JUnit5 provides many new features that will help taking unit testing to next level in form of parameterized test, dynamic test and assertion aggregation to name a few.
If you just want to check out the project without going through steps you can download it from here.
Setup JUnit 4 Project with Test
Add JUnit Dependency
Open the Eclipse editor and create a simple Maven project name as JUnit 4. Please refer to the below image on how to achieve the same. You need to click on "New" under the "File" menu, select "Maven Project" and select a simple archetype. You can think of archetypes as a template under the Maven ecosystem.
Check Version and Spring Boot
Once the project is created, go to the Explorer view in Eclipse and open the pom.xml file of this newly created project. Now add dependencies to POM for spring-boot-starter-web and spring-boot-started-test. You can refer to below snippet for copy and paste as is.
Note that this project is with version 2.0.3 of Spring Boot, with JUnit 4 for unit testing.
The Spring Boot project works on the concept of parent POM inheritance. Parent POM contains version dependencies entry for helper projects like JUnit. This snippet will help you understand why 2.0.3 used JUnit 4.
The right side in the image is the latest version of
spring-boot-test. The JUnit 4 dependency is still part of newer spring-boot-test project because of backward compatibility. It essentially means if there are existing JUnit 4 test cases available in the same project, JUnit’s will not break. In the next few steps, we will see how we will exclude JUnit 4 fully and move to JUnit 5 fully.
Add Functional Code
You need to add a now functional code to the project. Create simple Java classes with names as Calculator, Multiplier, Adder, and Subtractor. Once you add these classes you need to enrich them with functional code. In order to move faster, you can copy and paste the code from the below snippet in corresponding classes or refer to the Github link provided at the top.
Here you are creating a main class and delegating classes. This is necessary to learn the concept of mocking which is essential in any commercial project because most of the projects are designed with separation of concerns in mind. This exercise uses Spring as an IOC container. The Java classes created are injected as bean and autowired.
Add Test Code
You need to create test class called CalculatorTest. To keep it simple, you just copy and paste the code snippet in the created test class. In this class you are using
@RunWith(SpringJUnit4ClassRunner.class) annotation. The
Runwith annotation is JUnit 4 specific annotation. This annotation provides the test run engine.
Your annotated dependency classes like Adder and Subtractor in class with the
MockedBean annotation. This annotation will create mocked instances of the dependency classes and inject it in the execution path during JUnit run. This is the least invasive way of injecting dependency; otherwise, you either have to create a setter and getter in the Calculator class or create an constructor accepting these dependencies. These two approaches will not add any value, but will increase boiler plate code. This strategy of MockedBean will keep code as close to production as possible and still support unit tests.
In this snippet,
SpringJUnit4ClassRunner is used to run the test. This will allow us to autowire Spring dependencies in the test class and instantiate the application context for this during test execution. There are many runners available for JUnit one of them is
Run the test case in Eclipse with run as the JUnit test option by right-clicking on class. It will run with
Migrate JUnit 4 to JUnit 5
Create JUnit 5 Project
In this step you are going to create a new JUnit 5 project. You go to the context menu by right clicking in Project Explorer view of the Unit 4 project. Please refer to the below snippet to gain more clarity and give it new name sb-junit5.
Add JUnit 5 Dependency
As discussed above, the JUnit Vintage engine was responsible for running JUnit 4 tests. In this step, you will exclude that dependency. In order to achieve this you open pom.xml of sb-junit5 project and change spring boot version to latest 2.2.3.RELEASE and exclude the JUnit-vintage-engine dependency from spring boot started test dependency to use JUnit 5 and Mockito. In order to do it faster, you can copy the pom from the snippet directly. This image shows the difference view for pom.xml before and after changes.
Your final pom file will look like this:
JUnit 5 has introduced junit-vintage-engine to support JUnit 4 test cases. You have to exclude this dependency of vintage engine so that only available engine is of JUnit 5.
Modify Test Code
Open CalculatorTest from the copied project and paste the snippet provided below. There are certain things worth noting here. The import statement for JUnit 5 and JUnit 4 is different as the supporting classes have different packages. The JUnit Runner class changed to
SpringExtension. You have to use
ExtendWith annotation instead of
RunWith as per JUnit 5.
JUnit 5 has gotten rid of
RunWith annotation in this version as it has introduced this concept of
ExtendWith; this concept is applicable for all other areas in JUnit 5 where one can extend existing functionality coming out of the box, thus giving user leverage to build something on top of existing stuff without reinventing the wheel. For example, it provides you a mechanism to combine annotations from the package to give new meaning, like you want to run certain tests for sanity vs. full suite. You can create a annotation to filter those test cases combining existing
@Test annotaion like
@SanityTest. There is a change in test method having exception in annotation to assertion like below, it is using new method and lambda expression. We added new annotation
DisplayName from JUnit 5 to be more descriptive about methods what they are doing like below.
There is no change to Mockito API though the new version of Mockito has been added by parent pom of spring boot thus supporting Junit5 with Mockito out of the box. Try running the test class as JUnit test.
We added a new annotation
DisplayName from JUnit 5 to be more descriptive about methods what they are doing like below
@DisplayName (" Test addition failure for one of the arguments are negative")
There is no change to the Mockito API, though new version of Mockito has been added by parent pom of spring boot thus supporting Junit5 with Mockito out of the box without you doing much. Try running the test class as JUnit test. With this you have your first set of tests migrated successfully to Junit5 using Mockito.
In this exercise, you have learned how to move to a new JUnit 5 version from a JUnit 4 project. You learned how backward compatibility is ensured with JUnit 4 in JUnit 5. You learned about what packages needs to be imported with new JUnit 5. You learned about a new way of testing exceptions and usage of display name in JUnit 5. You got a brief about what to exclude from Pom in order to completely shut off JUnit 4. There may be cases in your project where you are not inheriting from parent pom of spring boot in those cases these dependencies need to be managed manually. In the next exercise we will learn the usage of advance features of JUnit 5.
Opinions expressed by DZone contributors are their own.