Book Image

Mastering Software Testing with JUnit 5

By : Boni Garcia
Book Image

Mastering Software Testing with JUnit 5

By: Boni Garcia

Overview of this book

When building an application it is of utmost importance to have clean code, a productive environment and efficient systems in place. Having automated unit testing in place helps developers to achieve these goals. The JUnit testing framework is a popular choice among Java developers and has recently released a major version update with JUnit 5. This book shows you how to make use of the power of JUnit 5 to write better software. The book begins with an introduction to software quality and software testing. After that, you will see an in-depth analysis of all the features of Jupiter, the new programming and extension model provided by JUnit 5. You will learn how to integrate JUnit 5 with other frameworks such as Mockito, Spring, Selenium, Cucumber, and Docker. After the technical features of JUnit 5, the final part of this book will train you for the daily work of a software tester. You will learn best practices for writing meaningful tests. Finally, you will learn how software testing fits into the overall software development process, and sits alongside continuous integration, defect tracking, and test reporting.
Table of Contents (8 chapters)

Nested tests

Nested tests give the test writer more capabilities to express the relationship and order in a group of tests. JUnit 5 makes it effortless to nest test classes. We simply need to annotate inner classes with @Nested and all test methods in there will be executed as well, going from the regular tests (defined in the top-level class) to the tests defined in each of the inner classes.

The first thing we need to take into account is that only non-static nested classes (that is inner classes) can serve as @Nested tests. Nesting can be arbitrarily deep, and the setup and tear down for each test (that is, @BeforeEach and @AfterEach methods) are inherited in the nested tests. Nevertheless, inner classes cannot define the @BeforeAll and @AfterAll methods, due to the fact that Java does not allow static members in inner classes. However, this restriction can be avoided...