Parameterized testing

Introduction to Testing in Java

Maria Milusheva

Senior Software Engineer

Example: Username validation

Consider the username validation exercise from Chapter 1:

public static boolean isValidUsername(String username) {
    if (username == null || username.isEmpty() || username.contains(" ")) {
        return false;
    }
    return username.length() >= 3;
}
Introduction to Testing in Java

Example: Repetitive validation test

Recall that the tests looked like this:

@Test
void isValidUsername_returnsFalse() {
    String username = ____;

    boolean actual = isValidUsername(username);

    assertFalse(actual);
}

We don't have to write 3 of these tests!

1 https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
Introduction to Testing in Java

Parameterized tests: syntax

Instead of @Test, we use @ParameterizedTest

It is imported from:

import org.junit.jupiter.params.ParameterizedTest;

We also need one further annotation: @ValueSource.

It is imported from:

import org.junit.jupiter.params.provider.ValueSource;
Introduction to Testing in Java

Parameterized tests: example test

@ParameterizedTest
@ValueSource(strings = {"", "jane doe"}) // NOTE: It's "strings", not String
void isValidUsername_returnsFalse(String username) { // Tests can take arugments
    boolean actual = isValidUsername(username);

    assertFalse(actual);
}

Junit executes this as two tests:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
All tests passed!
Introduction to Testing in Java

@ValueSource limitations

@ValueSource is limited to certain types.

@ValueSource(bytes = {1, 2, 3, 42})
@ValueSource(shorts = {100, 200, 300})
@ValueSource(ints = {1, 2, 3, 42})
@ValueSource(longs = {1000L, 2000L, 3000L})
@ValueSource(floats = {1.0f, 3.14159f, 2.71828f})
@ValueSource(doubles = {3.14, 2.71828, 1.4142})
@ValueSource(chars = {'a', 'b', 'c', 'Z'})
@ValueSource(booleans = {true, false})
@ValueSource(strings = {"Hello", "JUnit", "5", "Parameter"})
@ValueSource(classes = {String.class, Integer.class, ValueSourceExamples.class})
1 https://junit.org/junit5/docs/5.7.1/api/org.junit.jupiter.params/org/junit/jupiter/params/provider/ValueSource.html
Introduction to Testing in Java

@NullSource

We cannot pass null to @ValueSource as it will not compile:

@ParameterizedTest
@ValueSource(strings = {"", "jane doe", null}) // Does not compile
void isValidUsername_returnsFalse(String username) {
    boolean actual = isValidUsername(username);

    assertFalse(actual);
}

Instead use @NullSource.

Introduction to Testing in Java

Running the full ParameterizedTest

If we pass null to @ValueSource, it will not compile.

@ParameterizedTest
@NullSource // Adds a null input test case
@ValueSource(strings = {"", "jane doe"})
void isValidUsername_returnsFalse(String username) {
    boolean actual = isValidUsername(username);

    assertFalse(actual);
}
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
All tests passed!
Introduction to Testing in Java

Multiple arguments per test

Another limitation of @ValueSource - can only pass one value per test.

Consider the following method:

int countLetters(String input) {
    if (input != null) {
        return input.length();
    }
    return 0;
}
Introduction to Testing in Java

@CsvSource

For multiple values, use @CsvSource (CSV = comma separated values):

@ParameterizedTest
@CsvSource({"Hello World, 11", "DataCamp, 8", "'', 0", ", 0"})
void countLetters_countsLetters(String input, int expected) {
    int actual = countLetters(input);

    assertEquals(expected, actual);
}

Note ", 0" is inputted as null, 0. To input an empty string, use "'', 0" in JUnit 5.10

@CsvSource has similar type limitations as @ValueSource.

Introduction to Testing in Java

Note: Importing Source annotation

All @____Source annotations can be imported from:

import org.junit.jupiter.params.provider.*;
Introduction to Testing in Java

Let's practice!

Introduction to Testing in Java

Preparing Video For Download...