Member-only story
JUnit 5 with Java 14 Records
Records is a new preview feature of Java 14, and it allows to significantly simplify the code. In this article, I’ll show how to use this feature in your unit tests to make them concise and readable.
I’ll start with Maven project setup. I import two modules from JUnit 5: junit-jupiter-api
containing the basic unit test API, and junit-jupiter-params
containing extensions for parameterized tests:
The records are only available as a preview feature in Java 14. So I need to add the --enable-preview
compiler argument both for source code compilation (done by maven-compiler-plugin
), and for test running (executed by maven-surefire-plugin
). I put the plugin setup in the pluginManagement
section:
I also add source
and target
configuration parameters, to make sure the compiler uses Java 14 syntax.