DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > JUnit 5 Tutorial: Nice and Easy [Video]

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."

Marco Behler user avatar by
Marco Behler
CORE ·
May. 11, 22 · Java Zone · Tutorial
Like (2)
Save
Tweet
2.85K Views

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 @ParameterizedTests, @TestFactorys, and @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 @BeforeAll, @BeforeEach, @AfterAll, @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!  


JUnit

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • After COVID, Developers Really Are the New Kingmakers
  • Open Source Security Risks
  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • The Evolution of Configuration Management: IaC vs. GitOps

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo