Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Eclipse displays incorrect warning when using Junit's @MethodSource
Eclipse displays incorrect warning when using Junit's @MethodSource [message #1859316] Tue, 30 May 2023 17:53
Mateus Bandeira is currently offline Mateus BandeiraFriend
Messages: 1
Registered: May 2023
Junior Member
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.
Previous Topic:Upgrading to Eclipse SDK 4.16
Next Topic:What is the directory of the Java JDK installed by Eclipse IDE 2021-12?
Goto Forum:
  


Current Time: Fri Mar 29 13:10:15 GMT 2024

Powered by FUDForum. Page generated in 0.04534 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top