Stack Exchange
How to use @MethodSource defined in other class in JUnit 5
Is there any way to use @MethodSource to use a method defined in other class ?
e.g: Below code works, as the stringProvider method is defined in same class.
@ParameterizedTest
@MethodSource("stringProvider")
void methodSourceFromOtherClass(String word) {
    System.out.println(word);
    assertNotNull(word);
}

public static Stream<Arguments> stringProvider() {
    return Stream.of(
            Arguments.of("Values"),
            Arguments.of("From"),
            Arguments.of("MethodSource"));      
}

I have some utility classes, that provides test data. How can I use method from external classes in @methodSource?

Syntax for referring methods from external classes 
@MethodSource("fullyQualifiedClassName#methodName")

e.g.
@ParameterizedTest
@MethodSource("com.niraj.DataProvider#stringProvider")
void methodSourceFromOtherClass(String word) {
    System.out.println(word);
    assertNotNull(word);
}


Note that support for @MethodSource factory methods in external classes was introduced in JUnit Jupiter 5.2: https://github.com/junit-team/junit5/issues/1088

The names of factory methods within the test class or in external classes to use as sources for arguments.  
Factory methods in external classes must be referenced by fully qualified method name - e.g. @MethodSource(com.example.StringsProviders#blankStrings).  
If no factory method names are declared, a method within the test class that has the same name as the test method will be used as the factory method by default - e.g. @MethodSource(blankStrings). 
