Skip to content

Commit

Permalink
Fail @ParameterizedTest if there are no registered ArgumentProviders
Browse files Browse the repository at this point in the history
  • Loading branch information
juliette-derancourt committed Dec 14, 2024
1 parent dcb4f49 commit 549f481
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.platform.commons.support.AnnotationSupport.findRepeatableAnnotations;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
Expand Down Expand Up @@ -74,8 +75,13 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
ParameterizedTestNameFormatter formatter = createNameFormatter(extensionContext, methodContext);
AtomicLong invocationCount = new AtomicLong(0);

List<ArgumentsSource> argumentsSources = findRepeatableAnnotations(methodContext.method, ArgumentsSource.class);

Preconditions.condition(!argumentsSources.isEmpty(),
"Configuration error: You must configure at least one arguments source for this @ParameterizedTest");

// @formatter:off
return findRepeatableAnnotations(methodContext.method, ArgumentsSource.class)
return argumentsSources
.stream()
.map(ArgumentsSource::value)
.map(clazz -> ParameterizedTestSpiInstantiator.instantiate(ArgumentsProvider.class, clazz, extensionContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,23 @@ void throwsExceptionWhenParameterizedTestIsNotInvokedAtLeastOnce() {

@Test
void doesNotThrowExceptionWhenParametrizedTestDoesNotRequireArguments() {
var extensionContextWithAnnotatedTestMethod = getExtensionContextReturningSingleMethod(
new TestCaseAllowNoArgumentsMethod());
var extensionContext = getExtensionContextReturningSingleMethod(new TestCaseAllowNoArgumentsMethod());

var stream = this.parameterizedTestExtension.provideTestTemplateInvocationContexts(
extensionContextWithAnnotatedTestMethod);
var stream = this.parameterizedTestExtension.provideTestTemplateInvocationContexts(extensionContext);
// cause the stream to be evaluated
stream.toArray();
stream.close();
}

@Test
void throwsExceptionWhenParameterizedTestHasNoArgumentsSource() {
var extensionContext = getExtensionContextReturningSingleMethod(new TestCaseWithNoArgumentsSource());

assertThrows(PreconditionViolationException.class,
() -> this.parameterizedTestExtension.provideTestTemplateInvocationContexts(extensionContext),
"Configuration error: You must configure at least one arguments source for this @ParameterizedTest");
}

@Test
void throwsExceptionWhenArgumentsProviderIsNotStatic() {
var extensionContextWithAnnotatedTestMethod = getExtensionContextReturningSingleMethod(
Expand Down Expand Up @@ -323,19 +330,36 @@ void method() {

static class TestCaseWithAnnotatedMethod {

@SuppressWarnings("JUnitMalformedDeclaration")
@ParameterizedTest
@ArgumentsSource(ZeroArgumentsProvider.class)
void method() {
}
}

static class TestCaseAllowNoArgumentsMethod {

@ParameterizedTest(allowZeroInvocations = true)
@ArgumentsSource(ZeroArgumentsProvider.class)
void method() {
}
}

static class TestCaseWithNoArgumentsSource {

@ParameterizedTest(allowZeroInvocations = true)
@SuppressWarnings("JUnitMalformedDeclaration")
void method() {
}
}

static class ZeroArgumentsProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.empty();
}
}

static class ArgumentsProviderWithCloseHandlerTestCase {

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void failsWhenArgumentsRequiredButNoneProvided() {
}

@Test
void failsWhenArgumentsAreNotRequiredAndNoneProvided() {
void doesNotFailWhenArgumentsAreNotRequiredAndNoneProvided() {
var result = execute(ZeroArgumentsTestCase.class, "testThatDoesNotRequireArguments", String.class);
result.allEvents().assertEventsMatchExactly( //
event(engine(), started()), event(container(ZeroArgumentsTestCase.class), started()),
Expand All @@ -467,6 +467,15 @@ void failsWhenArgumentsAreNotRequiredAndNoneProvided() {
event(engine(), finishedSuccessfully()));
}

@Test
void failsWhenNoArgumentsSourceIsDeclared() {
var result = execute(ZeroArgumentsTestCase.class, "testThatHasNoArgumentsSource", String.class);
result.containerEvents().assertThatEvents() //
.haveExactly(1, //
event(displayName("testThatHasNoArgumentsSource(String)"), finishedWithFailure(message(
"Configuration error: You must configure at least one arguments source for this @ParameterizedTest"))));
}

private EngineExecutionResults execute(DiscoverySelector... selectors) {
return EngineTestKit.engine(new JupiterTestEngine()).selectors(selectors).execute();
}
Expand Down Expand Up @@ -2428,6 +2437,12 @@ void testThatDoesNotRequireArguments(String argument) {
fail("This test should not be executed, because no arguments are provided.");
}

@ParameterizedTest(allowZeroInvocations = true)
@SuppressWarnings("JUnitMalformedDeclaration")
void testThatHasNoArgumentsSource(String argument) {
fail("This test should not be executed, because no arguments source is declared.");
}

public static Stream<Arguments> zeroArgumentsProvider() {
return Stream.empty();
}
Expand Down

0 comments on commit 549f481

Please sign in to comment.