@ValueSource

The annotation @ValueSource is used in conjunction with @ParameterizedTest to specify a parameterized test in which the argument source is an array of literal values of String, int, long, or double. These values are specified inside the annotation, using the elements strings, ints, longs, or doubles. Consider the following example:

package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class
ValueSourceStringsParameterizedTest {

@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
void testWithStrings(String argument) {
System.out.println("Parameterized test with (String) parameter: "
+ argument);
assertNotNull(argument);
}
}

The method of this class (testWithStrings) defines a parameterized test in which an array of String is specified. Due to the fact that two String arguments are specified in the annotation @ValueSource (in this example "Hello" and "World"), the test logic will be exercised twice, once per value. This data is injected in the test method using the argument of the method, in this case through the String variable named argument. All in all, when executing this test class, the output will be as follows:

Execution of a parameterized test using @ValueSource and String argument provider

We can also use integer primitive types (int, long, and double) within the @ValueSource annotation. The following example demonstrates how. The methods of this example class (named testWithInts, testWithLongs, and testWithDoubles) use the annotation @ValueSource to define the arguments in the form of integer values, using the primitive types int, long, and double, respectively. To that aim, the elements ints, longs, and doubles of @ValueSource need to be specified:

package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class ValueSourcePrimitiveTypesParameterizedTest {

@ParameterizedTest
@ValueSource(ints = { 0, 1 })
void testWithInts(int argument) {
System.out.println("Parameterized test with (int) argument: " +
argument);
assertNotNull(argument);
}

@ParameterizedTest
@ValueSource(longs = { 2L, 3L })
void testWithLongs(long argument) {
System.out.println(
"Parameterized test with (long)
argument: " + argument);
assertNotNull(argument);
}

@ParameterizedTest
@ValueSource(doubles = { 4d, 5d })
void testWithDoubles(double argument) {
System.out.println("Parameterized test with (double)
argument: " + argument);
assertNotNull(argument);
}

}

As can be seen in the picture here, each test is executed twice, since in each @ValueSource annotation we specify two different input parameters (type int, long, and double, respectively).

Execution of a parameterized test using @ValueSource and primitive types
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.200.71