Skip to content

Commit

Permalink
Fix CartesianTest supportsParameter check (#633 / #636)
Browse files Browse the repository at this point in the history
CartesianTest throws an exception if it encounters a parameter it
does not support. This commit fixes that, now it just returns false.

Closes: #633
PR: #636
  • Loading branch information
Michael1993 committed May 17, 2022
1 parent 9bd6f03 commit a733333
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit a733333

Please sign in to comment.