Wprowadzenie do testowania w Javie
Maria Milusheva
Senior Software Engineer
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;
}
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!
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;
@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!
@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})
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.
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!
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;
}
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.
Wszystkie adnotacje @____Source można zaimportować z:
import org.junit.jupiter.params.provider.*;
Wprowadzenie do testowania w Javie