I'm trying to unit test a class using mockito (version 5.3.1) and junit (4.13.2).
The test class is annotated @RunWith(MockitoJUnitRunner.class)
, the tested object is
@InjectMocks
TestedBean testedBean;
and all its dependencies are annotated with @Mock
. This approach works correctly already for a few test methods which are all annotated with @Test
.
Now consider these two test methods:
@Test
public void test1() {
String value = "value";
String filter = "val";
boolean expectedResult = true;
// when
var result = testedBean.testedMethod(value, filter, null);
// then
assertThat(result, is(expectedResult));
}
@CsvSource({
"value, val, true"
})
@ParameterizedTest
public void test2(String value, String filter, boolean expectedResult) {
// when
var result = testedBean.testedMethod(value, filter, null);
// then
assertThat(result, is(expectedResult));
}
The @Test
annotated test passes correctly while the @ParameterizedTest
throws a NullPointerException
as this.testedBean
is null
.
How is it possible that the object is constructed as expected in a @Test
but not in a @ParameterizedTest
?
@RunWith(MockitoJUnitRunner.class)
doesn't support parameterized tests. The test class has to be annotated additionally with
@ExtendWith(MockitoExtension.class)
to make it work.