JUnit 5 Tutorial: Nice and Easy [Video]
Want a fun and engaging intro to the JUnit 5 testing ecosystem? Check out this live-coding session on a new episode of "Marco Codes."
Join the DZone community and get the full member experience.Join For Free
Ever looked for a comprehensive intro to JUnit 5 that is fun and entertaining at the same time? Then have a look at this brand-new episode of the "Marco Codes" YouTube channel: JUnit 5 Tutorial - Nice & Easy.
In this video, you'll learn how to use JUnit 5 like a professional. Understand what Maven/Gradle dependencies you'll need, how to write tests, how to use other assertion libraries like AssertJ and asserting JSON & XML, and explore new JUnit 5 features like
@ExtendWith. By the end of the tutorial, there won't be many questions left when it comes to JUnit 5.
What’s in the Video?
If you want to test something with JUnit 5, you'll obviously have to start with setting up the right Maven/Gradle dependencies unless you are already using a framework like Spring Boot, which does this automatically for you. Hence, this is how we'll start.
We'll then continue writing our first test. Most people by today know about the
@Test annotation, but there are always many arguments to be had over consistent naming styles of your tests, so we'll cover those soft topics as well.
If you're not paying attention, you will be able to run JUnit 5 tests from inside your IDE, but not from the command line via Maven. There are tiny infrastructure problems you need to be aware of.
We'll then cover assertions. JUnit 5 comes with its own set of assertions, but there's actually a better way: Using a library called AssertJ in combination with JUnit 5. AssertJ offers fluent assertions, and you'll learn how to use those.
Even better, AssertJ integrates with other popular testing libraries to write assertions against XML and JSON files, which comes in handy in most real-life projects.
After having had your first successes with tests and assertions, we'll have a look at the JUnit 5 lifecycle, which means
@AfterEach. In most tests, you usually want to set something up before running them, or do clean-up afterward. Those annotations allow you to do just that.
Then comes the first big new feature of JUnit 5, which is the
@ParameterizedTest annotation. It allows you to write just one test method that JUnit can duplicate on the fly based on an input list of values: from simple lists to CSV file rows to enum values, it's a super useful new feature!
You can take it even further with the
@TestFactory annotation, which allows you to dynamically generate new test methods through code! We'll come up with the simplest example possible, generating new test methods on-the-fly for all XML files in your project's directory.
Sooner or later, people want to run their tests as part of a CI Pipeline. JUnit 5 offers the
@Tag annotation for that, which lets you easily split up unit and integration tests, for example, so that later on, you can easily run them in isolation.
Last but not least is the
@ExtendWith annotation. You might want to use JUnit 5 together with other libraries, such as Mockito or Spring Boot. The
@ExtendWith annotation is the entry point for all the integrations with 3rd party frameworks.
If you managed to watch the entire video, you'll be presented with some hints, tips, and resources, that will help you continue your JUnit learning journey on your own. Check it out and let us know what you think!
Opinions expressed by DZone contributors are their own.