Skip to content

dsl-builders/expectations

Repository files navigation

Expectations DSL Builder

This is a simple DSL builder for writing parametrized tests in JUnit 5 using @TestFactory

Installation

Add this dependency into your application’s build.gradle script with the latest version of the library):

Gradle
plugins {
  id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'builders.dsl:expectations:1.0.0'
}

Or add the following dependency to your Maven project:

Maven
<dependency>
  <groupId>builders.dsl</groupId>
  <artifactId>expectations</artifactId>
  <version>1.0.0</version>
  <scope>test</scope>
</dependency>

Usage

Implicit Assertions

To create a parametrized test using Expectations DSL builder, you need to create a test method returning Expectations and annotate it with @TestFactory annotation.

Fluent Expectations
@TestFactory                                                                        // (1)
Expectations basicTest3Fluent() {                                                   // (2)
    Calculator calculator = new Calculator();

    return given("a", "b", "c")                                                     // (3)
            .are(2, 3, 5)                                                           // (4)
            .and(3, 5, 8)
            .and(4, 7, 11)
            .expect(
                    "#a + #b = #c",                                                 // (5)
                    (a, b, c) -> calculator.add(a, b) == c                          // (6)
            );
}
  1. Annotate method with @TestFactory

  2. Make the method return Expectations

  3. Define the parameters' headers using given

  4. Define at least one data row using are

  5. Define the test title template using the headers preceded by # to define the placeholders

  6. Define the test

See the Full Documentation foe more information.