Testy parametryczne

Wprowadzenie do testowania w Javie

Maria Milusheva

Senior Software Engineer

Przykład: walidacja nazwy użytkownika

Rozważmy ćwiczenie z walidacją nazwy użytkownika z rozdziału 1:

public static boolean isValidUsername(String username) {
    if (username == null || username.isEmpty() || username.contains(" ")) {
        return false;
    }
    return username.length() >= 3;
}
Wprowadzenie do testowania w Javie

Przykład: powtarzający się test walidacji

Przypomnijmy, że testy wyglądały następująco:

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

    boolean actual = isValidUsername(username);

    assertFalse(actual);
}

Nie trzeba pisać 3 takich testów!

1 https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
Wprowadzenie do testowania w Javie

Testy parametryczne: składnia

Zamiast @Test używamy @ParameterizedTest

Importujemy z:

import org.junit.jupiter.params.ParameterizedTest;

Potrzebna jest też adnotacja: @ValueSource.

Importujemy z:

import org.junit.jupiter.params.provider.ValueSource;
Wprowadzenie do testowania w Javie

Testy parametryczne: przykład

@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 wykonuje ten test dwukrotnie:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
All tests passed!
Wprowadzenie do testowania w Javie

Ograniczenia @ValueSource

@ValueSource obsługuje tylko określone typy.

@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
Wprowadzenie do testowania w Javie

@NullSource

Przekazanie null do @ValueSource powoduje błąd kompilacji:

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

    assertFalse(actual);
}

Zamiast tego należy użyć @NullSource.

Wprowadzenie do testowania w Javie

Uruchamianie pełnego testu parametrycznego

Przekazanie null do @ValueSource nie skompiluje się.

@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!
Wprowadzenie do testowania w Javie

Wiele argumentów w teście

Kolejne ograniczenie @ValueSource — można przekazać tylko jedną wartość na test.

Rozważmy następującą metodę:

int countLetters(String input) {
    if (input != null) {
        return input.length();
    }
    return 0;
}
Wprowadzenie do testowania w Javie

@CsvSource

Dla wielu wartości należy użyć @CsvSource (CSV = wartości rozdzielone przecinkami):

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

    assertEquals(expected, actual);
}

Uwaga: ", 0" jest traktowane jako null, 0. Aby przekazać pusty ciąg, należy użyć "'', 0" w JUnit 5.10

@CsvSource has similar type limitations as @ValueSource.

Wprowadzenie do testowania w Javie

Uwaga: importowanie adnotacji Source

Wszystkie adnotacje @____Source można zaimportować z:

import org.junit.jupiter.params.provider.*;
Wprowadzenie do testowania w Javie

Czas na ćwiczenia!

Wprowadzenie do testowania w Javie

Preparing Video For Download...