Testing an Abstract Class With JUnit 1. Overview In this tutorial, we’ll analyze various use cases and possible alternative solutions to unit-testing of abstract classes with non-abstract methods. Note that testing abstract classes should almost always go through the public API of the concrete implementations, so don’t apply the below… Continue Reading junit-test-abstract-class

JUnit 5 Temporary Directory Support 1. Overview When testing, we often need access to a temporary file. However, managing the creation and deletion of these files ourselves can be cumbersome. In this quick tutorial, we’ll take a look at how JUnit 5 alleviates this by providing the TempDirectory extension. For… Continue Reading junit-5-temporary-directory

JUnit5 Programmatic Extension Registration with @RegisterExtension 1. Overview JUnit 5 provides multiple methods for registering extensions. For an overview of some of these methods, refer to our Guide to JUnit 5 Extensions. In this quick tutorial, we’ll focus on programmatic registration of JUnit 5 extensions, using the @RegisterExtension annotation. 2. @RegisterExtension… Continue Reading junit-5-registerextension-annotation

Running JUnit Tests Programmatically, from a Java Application 1. Overview In this tutorial, we’ll show how to run JUnit tests directly from Java code – there are scenarios where this option comes in handy. If you are new to JUnit, or if you want to upgrade to JUnit 5, you can… Continue Reading junit-tests-run-programmatically-from-java

JUnit 5 Conditional Test Execution with Annotations 1. Overview In this tutorial, we’re going to take a look at conditional test execution with annotations in JUnit 5. These annotations are from the JUnit Jupiter library’s condition package and allow us to specify different types of conditions under which our tests should or… Continue Reading junit-5-conditional-test-execution

@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll 1. Introduction In this short tutorial, we’re going to explain the differences between the @Before, @BeforeClass, @BeforeEach and @BeforeAll annotations in JUnit 4 and 5 – with practical examples of how to use them. We’ll also cover briefly their @After complementary annotations. Let’s start… Continue Reading junit-before-beforeclass-beforeeach-beforeall