When using JUnit's @MethodSource annotation in a Parameterized Test, it is possible to designate a Static method to generate arguments for a test.
private static Stream<Arguments> argsForTest1() {
return Stream.of(Arguments.of("arg1"), Arguments.of("arg2"));
}
@ParameterizedTest
@MethodSource("argsForTest1")
void test1(String arg) {
assertThat(arg).isNotNull();
}
The code above will be run twice: once with arg="arg1", and then with arg="arg2".
Even though argsForTest1 is a private static method, and it is not invoked directly in any other place in the project, Eclipse is smart enough to know that the @MethodSource("argsForTest1") annotation means that JUnit will call that method, so it is not actually unused.
The issue happens when we add Spring Framework's @Value annotation, that allows us to inject properties into the method arguments.
Given an application.properties file that looks like this:
test.arg1=arg1
test.arg2=arg2
And a test code that looks like this:
private static Stream<Arguments> argsForTest2(@Value("${test.arg1}") String arg1, @Value("${test.arg2}") String arg2) {
return Stream.of(Arguments.of(arg1), Arguments.of(arg2));
}
@ParameterizedTest
@MethodSource("argsForTest2")
void test2(String arg) {
assertThat(arg).isNotNull();
}
With the additional arguments in argsForTest2, Eclipse fails to realize that it is not an unused method, and displays a warning: "The method argsForTest2(String, String) from the type EclipsejunittestApplicationTests is never used locally"
This looks like an edge case that was not accounted for. See eclipsejunittest.zip for a minimal reproducing example.