2 Exploring core JUnit

 

This chapter covers

  • Understanding the JUnit life cycle
  • Working with the core JUnit classes, methods, and annotations
  • Demonstrating the JUnit mechanisms

Mistakes are the portals of discovery.

--James Joyce

In chapter 1, we decided that we need a reliable, repeatable way to test programs. Our solution is to write or reuse a framework to drive the test code that exercises our program API. As our program grows, with new classes and new methods added to the existing classes, we need to grow our test code as well. Experience has taught us that classes sometimes interact in unexpected ways, so we need to make sure we can run all of our tests at any time, no matter what code changes have taken place. But how do we run multiple test classes? How do we find out which tests passed and which ones failed?

In this chapter, we look at how JUnit provides the functionality to answer those questions. The chapter begins with an overview of the core JUnit concepts: the test class, methods, and annotations. Then we take a detailed look at the many testing mechanisms of JUnit 5 and the JUnit life cycle.

This chapter is written in the practical spirit of the Manning “in Action” series, looking mainly at the usage of the new core features. For comprehensive documentation of each class, method, and annotation, please visit the JUnit 5 user guide (https://junit.org/junit5/docs/current/user-guide) or the JUnit 5 Javadoc (https://junit.org/junit5/docs/current/api).

2.1 Core annotations

2.1.1 The @DisplayName annotation

2.1.2 The @Disabled annotation

2.2 Nested tests

2.3 Tagged tests

2.4 Assertions

2.5 Assumptions

2.6 Dependency injection in JUnit 5

2.6.1 TestInfoParameterResolver

2.6.2 TestReporterParameterResolver

2.6.3 RepetitionInfoParameterResolver

2.7 Repeated tests