Reputation: 171
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?*/) {
}
Upvotes: 5
Views: 7110
Reputation: 523
You can use like this:
@ParameterizedTest
@ValueSource(classes = {NoResultException.class, EmptyResultDataAccessException.class})
public void testOrderNotFound(Class<? extends Exception> clazz) throws Exception {
when(orderDao.getOrder(ORDER_ID)).thenThrow(clazz);
assertThrows(OrderNotFoundException.class,
() -> provisionService.addNumbers(ORDER_ID, USER));
}
Upvotes: 1
Reputation: 251
Example:
@ValueSource(classes = {NoResultException.class, EmptyResultDataAccessException.class})
Upvotes: 0
Reputation:
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());
}
Upvotes: 2