What is the use of @ValueSource(classes= .....)

Viewed 3308

In the User Guide for JUnit5 it is mentioned that one of the types that can be used for @ValueSource is java.lang.Class.

What is the use case for this feature? How can I use it?

@ParameterizedTest
@ValueSource(classes = {/*What goes here?*/})

void test(/*What goes here?*/) {

}
1 Answers

There is nothing special about class literals. @ValueSource allows us to specify literal values of different types, and Java supports literal values to refer to references to the instances of class Class<?>. So, these would end up being the input to whatever parameterized unit test we are writing. For example:

@ParameterizedTest
@ValueSource(classes = { String.class, Integer.class })
void testWithValueSource(Class<?> argument) {
    assertEquals( "java.lang", argument.getPackage().getName());
}
Related