JUnit 5 Parameterized Test Yaml Test Data Source

Overview

Yamaledt — JUnit 5 Parameterized Tests Using Yaml and Jamal

Introduction and usage

Note
This is the latest development documentation. This is a SNAPSHOT version. To see the documentation of a release you have to follow the link in the documentation RELEASES.adoc

You can create parameterized unit tests fetching the data from Yaml formatted resource file using Yamaledt. To use the possibility add the dependency to your build configuration. In case of Maven, for example, add the following lines to your dependencies configuration part:

<dependency>
    <groupId>com.javax0.yamaledt</groupId>
    <artifactId>yamaledt</artifactId>
    <version>1.1.2-SNAPSHOT</version>
    <scope>test</scope>
</dependency>

The next step is to create a parameterized test, and the Yaml file that holds the data set for the tests.

    @ParameterizedTest(name = "{0}")
    @Jamal(enabled = false)
    @YamlSource(strict = true)
    void sampleTestWithSimpleParameters(@Name("DisplayName") String dn,
                                        int i,
                                        @Name("k") int k) {
        Assertions.assertEquals(5, i + k);
    }

The JUnit 5 annotation @ParameterizedTest signals that this test is a parameterized test. The annotation @YamlSource specifies the test data source. In this case all the settings are the default, there are no arguments. The data will be read from the resource file sampleTestWithSimpleParameters.yaml. The name is calculated from the name of the test method, and the extension .yaml is appended to it.

Note
The extension by default is .yaml.jam. In the example above we disabled the Jamal processing and therefore the default extension became .yaml. Using Jamal macros in the test data file is an advanced topic. We do not detail it in the introduction section.

The Yaml data source file contains the data in the format:

"adding zero to five is five":
  int: 5
  k: 0
"adding one to four is five":
  int: 4
  k: 1
"adding three to two is five":
  k: 3
  int: 2
"using negative number is also okay":
  k: -1
  int: 6

The Yaml file describes a Map structure. The keys are the test names. These are the strings that will get into the place of the strings, which are defined using the @DisplayName annotation in non-parameterized tests. The value for each key/test is also a Map specifying the values for the test parameters.

The mapping of values can be based on

  • the type of the method arguments, or

  • the value of the annotation @Name.

In the example above we have three parameters.

  • One is named using @Name("DisplayName"),

  • one unnamed, and

  • one named using @Name("k").

In the Yaml file the main key, is used for the parameter named 'DisplayName'. For the unnamed parameter the type is used, which is, in this case is int. Finally, the last parameter gets the value from the map using the key k.

Running this test from an IDE will result a view something like this:

idea sample run

Annotation @YamlSource

The annotation @YamlSource is used to specify parameters for the test. You have to use it together with the annotation @ParameterizedTest.

You have to use this annotation on the test method. The use of the annotation instructs JUnit 5 to execute the test as a parameterized test, and to use the YamalArgumentsProvider, which implements the Yaml data source handling for parameterized tests. The annotation can define parameters for the parameters, like the name of the Yaml file.

The annotation can also be applied to the enclosing class. This is useful in case annotation parameters are shared by multiple test methods. The annotation on the class can define these parameters, and the methods inherit the value from there. For example, you can use the same single Yaml file for test parameter storage and provide an OGNL expression to point to the data of the individual tests. In this case the name of the file can be provided on the class, shared, and the different OGNL expressions can be defined in the annotations on the methods.

The parameters of the annotation are:

  • value may specify the file that holds the test data. If it is not specified then the name of the name of the test method will be used with the extension .yaml.jam or .yaml. The extension .yaml.jam is used by default. The extension .yaml is used when the @Jamal annotation enabled=false disables the Jamal processing. If there is a specified value, then the value is used as a resource name, and no extension is appended to the name. That way you have to specify @YamlSource("testData.yaml"). The name of the method is only considered a file name if the annotation does not define a value on the method and also not on any of the enclosing classes.

  • jamal may specify a @Jamal annotation. The use of this parameter is not recommended. The same result can be achieved adding the @Jamal annotation directly on the test method or on the class containing the test method.

  • ognl may specify an OGNL expression. If this value is defined then the test environment uses only the structure pointed by this expression as test data. Using this along with putting a @YamlSource annotation on the class you can use

Annotation @Jamal

The annotation @Jamal is used to specify parameters for the input Jamal processing.

Jamal is a general purpose text preprocessor that can go to extreme to eliminate input redundancy and copy-paste. This way the data structures that appear repeatedly in the test data structure can be described programmatically. Jamal supports Yaml data structure handling through an extension module, and the Jamal transformation process can be debugged via Web user interface. The core Jamal modules as well as the debugger module, yaml and snippet handling modules are included in the project dependencies. If you want to use any other module you need to add those to the project as a dependency.

For more information about Jamal read the documentation at https://github.com/verhas/jamal

The parameters of the annotation are

  • open can specify the macro opening string. This is {% by default.

  • close can specify the macro closing string This is %} by default.

  • enabled can be used to disable Jamal processing on the input. The default is to interpret the input as a macro text that results Yaml as output. Use @Jamal(enabled=false) annotation on the method to disample Jamal processing. When Jamal processing is disabled the default file name extension will be .yaml when the parameter resource name is calculated from the method name.

  • dump can be used to specify a file name where the output of the Jamal processing in written. This content does not need to be written into a file. It is processed in memory usually. This option is only provided in case you want to look at the generated, already pure Yaml formatted file.

You might also like...

A template for Spring Boot REST API tested with JUnit 5 and Cucumber 6

demo-bdd Un template Spring Boot pour lancer un BDD/ATDD avec Cucumber 6 et JUnit 5. Maven et le JDK 17 seront nécessaires. Exécuter les tests Le proj

Jul 19, 2022

IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests

IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests 📼 Webinar https://blog.jetbrains.com/idea/2021/11/live-stream-recording-intellij-idea-an

Jul 23, 2022

Testes unitários em Java utilizando JUnit 5

Testes unitários em Java utilizando JUnit Este curso tem como objetivo habilitar o(a) aluno(a) a testar soluções desenvolvidas na linguagem Java, torn

Jan 18, 2022

Testes unitários em Java utilizando JUnit

Testes unitários em Java utilizando JUnit Este curso tem como objetivo habilitar o(a) aluno(a) a testar soluções desenvolvidas na linguagem Java, torn

Nov 8, 2022

JUnit 5 extension for easy-random

Easy Random/Faker JUnit 5 extension The simple, stupid random Java™ beans generator for JUnit 5 The easy random extension provides a test with randoml

Nov 9, 2022

Intercept network request by running Selenium tests with JUnit on LambdaTest cloud.

Intercept network request by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Emulate geolocation by running Selenium tests with JUnit on LambdaTest cloud.

Emulate geolocation by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Testes unitários em Java utilizando JUnit

Testes unitários em Java utilizando JUnit Este curso tem como objetivo habilitar o(a) aluno(a) a testar soluções desenvolvidas na linguagem Java, torn

Oct 13, 2022

Run Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022
Releases(1.1.2)
  • 1.1.2(Jun 4, 2021)

  • 1.1.1(May 12, 2021)

  • 1.1.0(May 9, 2021)

    With this release, you can use the annotations also on the test class and you can inherit the parameters from the class. With that, you can switch off Jamal processing for the whole class if you must. Another typical use case to use one single large Yaml input file and the individual test methods can specify OGNL expressions to point to the data structure where their data is.

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(May 9, 2021)

Owner
Peter Verhas
Senior Developer at EPAM Systems
Peter Verhas
A simple yet powerful parameterized test runner for Java.

TestParameterInjector Introduction TestParameterInjector is a JUnit4 test runner that runs its test methods for different combinations of field/parame

Google 324 Dec 30, 2022
Override device mode by running Selenium test with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

null 11 Jul 11, 2022
Capture JavaScript error by running Selenium test with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

null 12 Jul 11, 2022
Randomized Testing (Core JUnit Runner, ANT, Maven)

RANDOMIZED TESTING ================== JUnit test runner and plugins for running JUnit tests with pseudo-randomness. See the following for more infor

null 167 Dec 26, 2022
A TestNG like dataprovider runner for JUnit with many additional features

junit-dataprovider Have a look to this repositories Wiki pages for documenation, usage examples and further information. Table of contents Home What i

TNG Technology Consulting GmbH 237 Sep 7, 2022
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

Testcontainers Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium we

null 6.7k Jan 9, 2023
CodeSheriff is a simple library that helps you in writing JUnit tests that check the quality of your code

CodeSheriff is a simple library that helps you in writing JUnit tests that check the quality of your code. For example, CodeSheriff may fail because you have methods in your code that have more than X lines of code, or that have complexity greater than Y.

Maurício Aniche 62 Feb 10, 2022
Exercicio envolvendo criação de Classe, Junit 5 e exception

ClasseData Complemente a classe Data desenvolvida anteriormente para que lance uma ExcecaoDataInvalida. Com esta única exceção, você deverá ser capaz

Mateus Samartini 3 May 7, 2021
Exercico com Junit 5, UML, Interação de classes e Exception

AcessoClube Exercico com Junit 5, UML, Interação de classes e Exception Exercicio: Projete e desenvolva um sistema de controle de acesso a um clube co

Mateus Samartini 3 May 3, 2021
This is a repository to collect JUnit Tests for GAD exercises at TUM in SS21

TUM_GAD_Tests_SS21 This is a repository to collect JUnit Tests for GAD exercises at TUM in SS21. These tests have been written by students for student

null 41 Oct 31, 2022