Sign in Go Pro

JUnit 5 Fundamentals

Writing a custom @Source for Parameterized Tests

03/05/2019

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson
Autoplay

Up next

Previous

About

Overview

While the built-in source annotations cover many use cases for @ParameterizedTest, it's possible to write a custom source, too. In this lesson, we implement a custom source for JSON-based data with the help of a third-party library, going through all the required steps to extend JUnit Jupiter with new functionality.

Writing a custom source only requires two steps: First, the source annotation itself, alongside its configuration. Furthermore, an ArgumentsProvider is needed to host the logic of processing the annotation's values into a meaningful stream of Arguments, which are then handed to the parameterized test method. In this example, a JSON structure is parsed into a JsonNode object, which the @ParameterizedTest declares as its method parameter.

The connection between the custom source annotation and its ArgumentsProvider gives a first look at the powerful extension model of the JUnit Jupiter API. Using the generic AnnotationConsumer interface, our JsonArgumentsProvider can receive a callback from the TestEngine while the annotation is processed at runtime. This allows us to read the values from the annotation.

Summary

  • Custom source annotations utilize @ArgumentsSource and the implementation of an ArgumentsProvider
  • The ArgumentsProvider exposes the data for a @ParameterizedTest through a stream of Arguments
  • The connection between the provider and annotation can be achieved through AnnotationConsumer<T>

Comments

There are no comments on this lesson, start the conversation below ...

>
You need to go PRO to post comments.

Lessons in JUnit 5 Fundamentals

Understand the Core Components of JUnit 5
02:11
Marcel Schnelle
Set up your Java Project for JUnit 5
01:25
Marcel Schnelle
Set up your Android Project for JUnit 5
01:42
Marcel Schnelle
The first @Test
02:06
Marcel Schnelle
Lifecycle with @BeforeEach & @AfterEach
01:09
Marcel Schnelle
Lifecycle with @BeforeAll & @AfterAll
Pro
01:10
Marcel Schnelle
Using 1 Instance per Method with @TestInstance
Pro
01:12
Marcel Schnelle
Using 1 Instance per Class with @TestInstance
Pro
02:01
Marcel Schnelle
Taking advantage of @TestInstance(PER_CLASS) in Kotlin
Pro
01:25
Marcel Schnelle
Bearable Test Method Names with @DisplayName
Pro
02:19
Marcel Schnelle
Checking for equality with assertEquals() & Friends
Pro
02:53
Marcel Schnelle
Using assertTrue() & assertFalse()
Pro
00:51
Marcel Schnelle
Asserting null
Pro
00:38
Marcel Schnelle
Testing Exceptions with assertThrows()
Pro
01:10
Marcel Schnelle
Composing Assertions with assertAll()
Pro
02:00
Marcel Schnelle
Avoiding failure with Assumptions
Pro
01:33
Marcel Schnelle
Ignoring Tests with @Disabled
Pro
01:31
Marcel Schnelle
Conditional Test Execution Pairs: @EnabledOnOs & @DisabledOnOs
Pro
01:43
Marcel Schnelle
Conditional Test Execution Pairs: @EnabledOnJre & @DisabledOnJre
Pro
00:47
Marcel Schnelle
Conditional Test Execution Pairs: @EnabledIfSystemProperty & @DisabledIfSystemProperty
Pro
01:30
Marcel Schnelle
Conditional Test Execution Pairs: @EnabledIfEnvironmentVariable & @DisabledIfEnvironmentVariable
Pro
01:08
Marcel Schnelle
Conditional Test Execution Pairs: @EnabledIf & @DisabledIf
Pro
02:19
Marcel Schnelle
Grouping tests with @Tag
Pro
01:50
Marcel Schnelle
@Nested & Effective BDD in JUnit 5
Pro
03:38
Marcel Schnelle
Running a test multiple times with @RepeatedTest
Pro
05:19
Marcel Schnelle
@ParameterizedTest with @ValueSource
Pro
03:11
Marcel Schnelle
@ParameterizedTest with @EnumSource
Pro
00:31
Marcel Schnelle
@ParameterizedTest with @MethodSource
Pro
02:30
Marcel Schnelle
@ParameterizedTest with @CsvSource & @CsvFileSource
Pro
01:44
Marcel Schnelle
Argument Conversion Techniques for Parameterized Tests
Pro
04:09
Marcel Schnelle
Writing a custom @Source for Parameterized Tests
Pro
04:31
Marcel Schnelle
Introduction to @TestFactory and Dynamic Tests
Pro
01:31
Marcel Schnelle
Grouping Dynamic Tests into Containers
Pro
01:55
Marcel Schnelle
Lifecycle Caveats for Dynamic Tests
Pro
01:06
Marcel Schnelle
Setting up Parallel Test Execution
Pro
02:47
Marcel Schnelle
Parallel Execution Strategies: Dynamic
Pro
02:02
Marcel Schnelle
Parallel Execution Strategies: Fixed
Pro
00:48
Marcel Schnelle
Parallel Execution Strategies: Custom
Pro
03:16
Marcel Schnelle
Synchronizing Parallel Test Execution with @ResourceLock
Pro
03:28
Marcel Schnelle