Skip to content

Commit 359761e

Browse files
committed
Improving error message for non-static @MethodSource factory methods issue: #3215
1 parent 5b54e0d commit 359761e

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodArgumentsProvider.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.junit.jupiter.api.Test;
2626
import org.junit.jupiter.api.TestFactory;
27+
import org.junit.jupiter.api.TestInstance;
2728
import org.junit.jupiter.api.TestTemplate;
2829
import org.junit.jupiter.api.extension.ExtensionContext;
2930
import org.junit.platform.commons.JUnitException;
@@ -46,12 +47,26 @@ protected Stream<? extends Arguments> provideArguments(ExtensionContext context,
4647
// @formatter:off
4748
return stream(methodNames)
4849
.map(factoryMethodName -> getFactoryMethod(testClass, testMethod, factoryMethodName))
50+
.map(factoryMethod -> validateStaticFactoryMethod(context, factoryMethod))
4951
.map(factoryMethod -> context.getExecutableInvoker().invoke(factoryMethod, testInstance))
5052
.flatMap(CollectionUtils::toStream)
5153
.map(MethodArgumentsProvider::toArguments);
5254
// @formatter:on
5355
}
5456

57+
private Method validateStaticFactoryMethod(ExtensionContext context, Method factoryMethod) {
58+
if (isPerMethodLifecycle(context)) {
59+
Preconditions.condition(ReflectionUtils.isStatic(factoryMethod),
60+
factoryMethod + " method must not be static");
61+
}
62+
return factoryMethod;
63+
}
64+
65+
private boolean isPerMethodLifecycle(ExtensionContext context) {
66+
return context.getTestInstanceLifecycle().orElse(
67+
TestInstance.Lifecycle.PER_CLASS) == TestInstance.Lifecycle.PER_METHOD;
68+
}
69+
5570
private Method getFactoryMethod(Class<?> testClass, Method testMethod, String factoryMethodName) {
5671
if (!StringUtils.isBlank(factoryMethodName)) {
5772
if (looksLikeAFullyQualifiedMethodName(factoryMethodName)) {

junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/MethodArgumentsProviderTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.junit.jupiter.api.BeforeEach;
3434
import org.junit.jupiter.api.Nested;
3535
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.api.TestInstance;
3637
import org.junit.jupiter.api.extension.ExtensionContext;
3738
import org.junit.jupiter.api.extension.ParameterContext;
3839
import org.junit.jupiter.api.extension.ParameterResolver;
@@ -198,7 +199,7 @@ void throwsExceptionWhenNonStaticFactoryMethodIsReferencedAndStaticIsRequired()
198199
var exception = assertThrows(JUnitException.class,
199200
() -> provideArguments(NonStaticTestCase.class, null, false, "nonStaticStringStreamProvider").toArray());
200201

201-
assertThat(exception).hasMessageContaining("Cannot invoke non-static method");
202+
assertThat(exception).hasMessageContaining("method must not be static");
202203
}
203204

204205
@Test
@@ -609,6 +610,9 @@ private Stream<Object[]> provideArguments(Class<?> testClass, Method testMethod,
609610
var testInstance = allowNonStaticMethod ? ReflectionUtils.newInstance(testClass) : null;
610611
when(extensionContext.getTestInstance()).thenReturn(Optional.ofNullable(testInstance));
611612

613+
var lifeCycle = allowNonStaticMethod ? TestInstance.Lifecycle.PER_CLASS : TestInstance.Lifecycle.PER_METHOD;
614+
when(extensionContext.getTestInstanceLifecycle()).thenReturn(Optional.of(lifeCycle));
615+
612616
var provider = new MethodArgumentsProvider();
613617
provider.accept(methodSource);
614618
return provider.provideArguments(extensionContext).map(Arguments::get);
@@ -796,6 +800,13 @@ Stream<String> nonStaticStringStreamProvider() {
796800
}
797801
}
798802

803+
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
804+
static class NonStaticTestCaseForPerMethodLifecycle {
805+
Stream<String> nonStaticStringStreamProvider() {
806+
return Stream.of("foo", "bar");
807+
}
808+
}
809+
799810
static class ExternalFactoryMethods {
800811

801812
static Stream<String> stringsProvider() {

0 commit comments

Comments
 (0)