Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ default.
temporary directories. See the
<<../user-guide/index.adoc#writing-tests-built-in-extensions-TempDirectory, User Guide>>
for details.
* Improve error message for non-static `@MethodSource` factory methods


[[release-notes-5.10.0-M1-junit-vintage]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.JUnitException;
Expand All @@ -50,12 +51,27 @@ protected Stream<? extends Arguments> provideArguments(ExtensionContext context,
// @formatter:off
return stream(methodNames)
.map(factoryMethodName -> findFactoryMethod(testClass, testMethod, factoryMethodName))
.map(factoryMethod -> validateStaticFactoryMethod(context, factoryMethod))
.map(factoryMethod -> context.getExecutableInvoker().invoke(factoryMethod, testInstance))
.flatMap(CollectionUtils::toStream)
.map(MethodArgumentsProvider::toArguments);
// @formatter:on
}

private Method validateStaticFactoryMethod(ExtensionContext context, Method factoryMethod) {
if (isPerMethodLifecycle(context)) {
Preconditions.condition(ReflectionUtils.isStatic(factoryMethod), () -> String.format(
"method '%s' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).",
factoryMethod.toGenericString()));
}
return factoryMethod;
}

private boolean isPerMethodLifecycle(ExtensionContext context) {
return context.getTestInstanceLifecycle().orElse(
TestInstance.Lifecycle.PER_CLASS) == TestInstance.Lifecycle.PER_METHOD;
}

private static Method findFactoryMethod(Class<?> testClass, Method testMethod, String factoryMethodName) {
String originalFactoryMethodName = factoryMethodName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
Expand Down Expand Up @@ -177,7 +178,8 @@ void throwsExceptionWhenNonStaticFactoryMethodIsReferencedAndStaticIsRequired()
var exception = assertThrows(PreconditionViolationException.class,
() -> provideArguments(NonStaticTestCase.class, null, false, "nonStaticStringStreamProvider").toArray());

assertThat(exception).hasMessageContaining("Cannot invoke non-static method");
assertThat(exception).hasMessageContaining(
"must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS)");
}

@Test
Expand Down Expand Up @@ -713,6 +715,9 @@ public void dummyMethod() {
var testInstance = allowNonStaticMethod ? ReflectionUtils.newInstance(testClass) : null;
when(extensionContext.getTestInstance()).thenReturn(Optional.ofNullable(testInstance));

var lifeCycle = allowNonStaticMethod ? TestInstance.Lifecycle.PER_CLASS : TestInstance.Lifecycle.PER_METHOD;
when(extensionContext.getTestInstanceLifecycle()).thenReturn(Optional.of(lifeCycle));

var provider = new MethodArgumentsProvider();
provider.accept(methodSource);
return provider.provideArguments(extensionContext).map(Arguments::get);
Expand Down