Introducere în testarea în Java
Maria Milusheva
Senior Software Engineer
Considerați exercițiul de validare a numelui de utilizator din Capitolul 1:
public static boolean isValidUsername(String username) {
if (username == null || username.isEmpty() || username.contains(" ")) {
return false;
}
return username.length() >= 3;
}
Reamintim că testele arătau astfel:
@Test
void isValidUsername_returnsFalse() {
String username = "____";
boolean actual = isValidUsername(username);
assertFalse(actual);
}
Nu este necesar să scriem 3 astfel de teste!
În loc de @Test, utilizăm @ParameterizedTest
Se importă din:
import org.junit.jupiter.params.ParameterizedTest;
Este necesară și o altă adnotare: @ValueSource.
Se importă din:
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 execută acest test de două ori:
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
All tests passed!
@ValueSource este limitat la anumite tipuri.
@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})
Nu se poate transmite null la @ValueSource deoarece nu compilează:
@ParameterizedTest
@ValueSource(strings = {"", "jane doe", null}) // Does not compile
void isValidUsername_returnsFalse(String username) {
boolean actual = isValidUsername(username);
assertFalse(actual);
}
În schimb, utilizați @NullSource.
Dacă se transmite null la @ValueSource, codul nu compilează.
@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!
O altă limitare a @ValueSource - poate transmite doar o valoare per test.
Considerați următoarea metodă:
int countLetters(String input) {
if (input != null) {
return input.length();
}
return 0;
}
Pentru valori multiple, utilizați @CsvSource (CSV = valori separate prin virgulă):
@ParameterizedTest
@CsvSource({"Hello World, 11", "DataCamp, 8", "'', 0", ", 0"})
void countLetters_countsLetters(String input, int expected) {
int actual = countLetters(input);
assertEquals(expected, actual);
}
Notă: ", 0" este interpretat ca null, 0. Pentru un șir gol, utilizați "'', 0" în JUnit 5.10
@CsvSource has similar type limitations as @ValueSource.
Toate adnotările @____Source pot fi importate din:
import org.junit.jupiter.params.provider.*;
Introducere în testarea în Java