chapter four

4 Migrating from JUnit 4 to JUnit 5

 

This chapter covers

  • Implementing the migration from JUnit 4 to JUnit 5
  • Working with a hybrid approach for mature projects
  • Comparing the needed JUnit 4 and JUnit 5 dependencies
  • Comparing the equivalent JUnit 4 and JUnit 5 annotations
  • Comparing the JUnit 4 rules and JUnit 5 extensions

Nothing in this world can survive and remain useful without an update.

—Charles M. Tadros

So far, this book has introduced JUnit and its latest version, 5, and explained the big architectural step between JUnit 4 and JUnit 5. I discussed the core classes and methods and presented them in action so that you have a good understanding of how to build your tests in an efficient way. I emphasized the importance of software architecture in general and shown the big architectural change between JUnit 4 and JUnit 5.

This chapter demonstrates the effective step from JUnit 4 to JUnit 5 inside a project managed by our example company, Tested Data Systems Inc. The company keeps its customer information in a repository and addresses this repository to get the information. In addition, the company needs to track payments and other business rules.

JUnit 4 and JUnit 5 may work together within the same application. This fact is of particular benefit for implementing migration in phases and not necessarily at once.

4.1       The steps between JUnit 4 and JUnit 5

4.2       Needed dependencies

4.3       Annotations, classes, and methods

4.3.1   Rules vs. the extension model

4.3.2   Custom rules

4.4       Summary