Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CartesianProductResolver::supportsParameter #636

Merged
merged 2 commits into from May 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,7 +14,6 @@

import java.util.List;

import org.junit.jupiter.api.extension.ExtensionConfigurationException;
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 @@ -48,10 +47,7 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
// parameter with correct type (or `null`)
if (parameter == null)
return true;
if (parameterClass.isAssignableFrom(parameter.getClass()))
return true;
throw new ExtensionConfigurationException(
"CartesianTest was supplied arguments but parameter is not supported.");
return parameterClass.isAssignableFrom(parameter.getClass());
}

@Override
Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -380,6 +381,40 @@ void factorySourceWithTestReporter() {
"Look on my works, ye Mighty, and despair!The lone and level sands stretch far away.");
}

@Test
@DisplayName("ignores 'oversupplied' parameters")
void factorySourceWithTestReporterNoSecondParam() {
ExecutionResults results = PioneerTestKit
.executeTestMethodWithParameterTypes(CartesianFactorySourceTestCases.class, "ignoredParam",
String.class, TestReporter.class);

assertThat(results)
.hasNumberOfReportEntries(9)
.withValues("And on the pedestal these words appear:", "My name is Ozymandias, king of kings;",
"Look on my works, ye Mighty, and despair!", "And on the pedestal these words appear:",
"My name is Ozymandias, king of kings;", "Look on my works, ye Mighty, and despair!",
"And on the pedestal these words appear:", "My name is Ozymandias, king of kings;",
"Look on my works, ye Mighty, and despair!");
}

@Test
@DisplayName("works when test class has a constructor with auto-injected values")
void testClassWithConstructor() {
ExecutionResults results = PioneerTestKit.executeTestClass(TestClassWithConstructorTestCases.class);

assertThat(results).hasNumberOfDynamicallyRegisteredTests(4).hasNumberOfSucceededTests(4);
assertThat(results).hasNumberOfReportEntries(4).withValues("13", "14", "23", "24");
}

@Test
@DisplayName("works when test class has @BeforeEach with auto-injected values")
void testClassWithBeforeEach() {
ExecutionResults results = PioneerTestKit.executeTestClass(TestClassWithBeforeEachTestCases.class);

assertThat(results).hasNumberOfDynamicallyRegisteredTests(4).hasNumberOfSucceededTests(4);
assertThat(results).hasNumberOfReportEntries(4).withValues("13", "14", "23", "24");
}

@Nested
@DisplayName("removes redundant parameters from input sets")
class CartesianProductRedundancyTests {
Expand All @@ -396,15 +431,6 @@ void removesExtraFromAnnotation() {

}

@Test
@DisplayName("when test class has a constructor with auto-injected values")
void testClassWithConstructor() {
ExecutionResults results = PioneerTestKit.executeTestClass(TestClassWithConstructorTestCases.class);

assertThat(results).hasNumberOfDynamicallyRegisteredTests(4).hasNumberOfSucceededTests(4);
assertThat(results).hasNumberOfReportEntries(4).withValues("13", "14", "23", "24");
}

}

@Nested
Expand Down Expand Up @@ -741,23 +767,6 @@ void rethrowProviderException() {
.hasMessage("Could not provide arguments because of exception.");
}

@Test
@DisplayName("there is an auto-injected param but arguments were supplied")
void factorySourceWithTestReporter() {
ExecutionResults results = PioneerTestKit
.executeTestMethodWithParameterTypes(CartesianFactorySourceTestCases.class, "competingInject",
String.class, TestReporter.class);

assertThat(results)
.hasNumberOfDynamicallyRegisteredTests(9)
.hasNumberOfFailedTests(9)
.andThenCheckExceptions(exceptions -> assertThat(exceptions)
.extracting(Throwable::getCause)
.hasOnlyElementsOfType(ExtensionConfigurationException.class)
.extracting(Throwable::getMessage)
.containsOnly("CartesianTest was supplied arguments but parameter is not supported."));
}

@Test
@DisplayName("parameter annotation arguments provider implements CartesianMethodArgumentsProvider")
void mismatchingInterfaceParam() {
Expand Down Expand Up @@ -1033,7 +1042,8 @@ void autoInjectedParam(String line, String otherLine, TestReporter reporter) {

@CartesianTest
@CartesianTest.MethodFactory("poem")
void competingInject(String line, TestReporter reporter) {
void ignoredParam(String line, TestReporter reporter) {
reporter.publishEntry(line);
}

static ArgumentSets poem() {
Expand Down Expand Up @@ -1131,6 +1141,24 @@ void shouldHaveTestInfo(@CartesianTest.Values(ints = { 1, 2 }) int i,

}

static class TestClassWithBeforeEachTestCases {

private TestInfo info;

@BeforeEach
void setUp(TestInfo info) {
this.info = info;
}

@CartesianTest
@ReportEntry("{0}{1}")
void shouldHaveTestInfo(@CartesianTest.Values(ints = { 1, 2 }) int i,
@CartesianTest.Values(ints = { 3, 4 }) int j) {
assertThat(info).isNotNull();
}

}

static class CustomCartesianArgumentsProviderTestCases {

@CartesianTest
Expand Down