Introduction to Testing in Java
Maria Milusheva
Senior Software Engineer
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;
}
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!
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;
@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!
@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})
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
.
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!
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;
}
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
.
All @____Source
annotations can be imported from:
import org.junit.jupiter.params.provider.*;
Introduction to Testing in Java