Sign in Go Pro

JUnit 5 Fundamentals

Argument Conversion Techniques for Parameterized Tests

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

In this lesson, we explore a very practical concept of the JUnit Jupiter API, which makes parameterized tests so powerful and easy to write: Argument Conversion. It allows for a limited notion of type inference when it comes to the arguments of a @ParamterizedTest method, and it comes in three flavours.

For instance, when using the @CsvSource annotation, all data is given as strings. The implicit conversion makes it possible to parse an integer inside of the CSV string into an actual int variable on the test method.

The second variant of this concept, the fallback-string-to-object conversion, tries to create arbitrary types from a string representation. If your custom class can be constructed from a single string, and it has a public constructor or factory method, JUnit Jupiter can instantiate this class and pass it as a parameter to your @ParameterizedTest automatically.

For all other cases, developers are given the power to write explicit converters for arguments. By annotating a parameter of a test method with @ConvertWith, an ArgumentConverter implementation can be given to the TestEngine, so that it knows how to create this particular type.

Summary

  • Implicit argument conversion: Type inference for primitives, strings, Java Date/Time APIs, Currency, Locale and more built-in types
  • Fallback-string-to-object conversion: Transparently instantiate a class from a single string through its public constructor
  • Explicit argument conversion: Implement an ArgumentConverter and annotate the offending parameter with @ConvertWith

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

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