From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Custom extensions in JUnit 5
From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Custom extensions in JUnit 5
- [Instructor] Custom extensions in JUnit 5 provide a powerful mechanism for extending the behavior of test classes or methods. They allow you to encapsulate reusable logic that can be applied to multiple tests, such as setup and teardown operations, conditional test execution, and more. Let's say we want to log the execution time of each test method. We can achieve this by creating a simple extension that logs the start and end times of each test method. Let's create a TimingExtension class. We'll want this class to log the start and end times, so let's create a Logger field, then we'll implement a few interfaces. BeforeTestExecutionCallback, and AfterTestExecutionCallback. These allow us to define behavior that should be executed before and after each test method. In the BeforeTestExecution method, we'll log the start time of the test method and store it in the extension context. We'll add the start time as System.currentTime in milliseconds. In the AfterTestExecution method, we'll…
Contents
-
-
-
-
-
-
Introduction to parameterized tests in JUnit2m 4s
-
(Locked)
Parameterized tests with EnumSource in JUnit 53m 21s
-
(Locked)
Parameterized tests with ValueSource in JUnit 52m 20s
-
(Locked)
Parameterized tests with CsvSource in JUnit 53m 26s
-
(Locked)
Conditional test execution based on Java version and OS2m 12s
-
(Locked)
Conditional test execution based on environment variables2m 35s
-
(Locked)
Conditional test execution based on custom conditions1m 40s
-
(Locked)
Control test execution with @Order2m 37s
-
(Locked)
Create test suites in JUnit 53m 12s
-
(Locked)
Create a test coverage report with JaCoCo4m 9s
-
(Locked)
Ensure test coverage with JaCoCo5m 41s
-
(Locked)
Execute parallel tests in JUnit 52m 41s
-
(Locked)
Custom extensions in JUnit 54m 40s
-
(Locked)
Challenge: Write a test class for a email validator1m 57s
-
(Locked)
Solution: Write a test class for a email validator5m 32s
-
-
-
-
-
-