diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 0bb949702e2d..222d54f252fd 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -19,14 +19,17 @@ jobs: strategy: fail-fast: false matrix: - language: ['java', 'javascript'] + language: + - java + - javascript steps: - name: Check out repository uses: actions/checkout@v3 - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} + tools: latest - name: Build uses: ./.github/actions/run-gradle with: @@ -35,4 +38,4 @@ jobs: -Dscan.tag.CodeQL allMainClasses - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2 diff --git a/.github/workflows/cross-version.yml b/.github/workflows/cross-version.yml index a752dcad2022..a6a6e0c3fde0 100644 --- a/.github/workflows/cross-version.yml +++ b/.github/workflows/cross-version.yml @@ -18,6 +18,7 @@ env: jobs: openjdk: strategy: + fail-fast: false matrix: jdk: [19, 20, 21] name: "OpenJDK ${{ matrix.jdk }}" diff --git a/README.md b/README.md index 37241253e2d2..59318bd5bb92 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This repository is the home of the next generation of JUnit, _JUnit 5_. ## Latest Releases -- General Availability (GA): [JUnit 5.9.2](https://github.com/junit-team/junit5/releases/tag/r5.9.1) (January 10, 2023) +- General Availability (GA): [JUnit 5.9.3](https://github.com/junit-team/junit5/releases/tag/r5.9.3) (April 26, 2023) - Preview (Milestone/Release Candidate): n/a ## Documentation diff --git a/documentation/src/docs/asciidoc/release-notes/index.adoc b/documentation/src/docs/asciidoc/release-notes/index.adoc index ceabf38312a2..6b41a3649a5e 100644 --- a/documentation/src/docs/asciidoc/release-notes/index.adoc +++ b/documentation/src/docs/asciidoc/release-notes/index.adoc @@ -16,6 +16,8 @@ authors as well as build tool and IDE vendors. include::{includedir}/link-attributes.adoc[] +include::{basedir}/release-notes-5.9.3.adoc[] + include::{basedir}/release-notes-5.9.2.adoc[] include::{basedir}/release-notes-5.9.1.adoc[] diff --git a/documentation/src/docs/asciidoc/release-notes/release-notes-5.9.3.adoc b/documentation/src/docs/asciidoc/release-notes/release-notes-5.9.3.adoc new file mode 100644 index 000000000000..09dc2a8e9f45 --- /dev/null +++ b/documentation/src/docs/asciidoc/release-notes/release-notes-5.9.3.adoc @@ -0,0 +1,59 @@ +[[release-notes-5.9.3]] +== 5.9.3 + +*Date of Release:* April 26, 2023 + +*Scope:* Bug fixes and enhancements since 5.9.2 + +For a complete list of all _closed_ issues and pull requests for this release, consult the +link:{junit5-repo}+/milestone/67?closed=1+[5.9.3] milestone page in the +JUnit repository on GitHub. + + +[[release-notes-5.9.3-junit-platform]] +=== JUnit Platform + +No changes. + + +[[release-notes-5.9.3-junit-jupiter]] +=== JUnit Jupiter + +==== Bug Fixes + +* Parameter types for _local_ `@MethodSource` factory method names are now validated. For + example, `@MethodSource("myFactory(example.NonexistentType)")` will now result in an + exception stating that `example.NonexistentType` cannot be resolved to a valid type. +* The syntax for parameter types in _local_ `@MethodSource` factory method names now + supports canonical array names -- for example, you may now specify `int[]` as in + `@MethodSource("myFactory(int[])"` instead of the _binary_ name `[I` as in + `@MethodSource("myFactory([I)"` (which was already supported) and + `@MethodSource("myFactory(java.lang.String[])` instead of + `@MethodSource("myFactory([Ljava.lang.String;)`. +* The search algorithm used to find `@MethodSource` factory methods now applies consistent + semantics for _local_ qualified method names and fully-qualified method names for + overloaded factory methods. +* The `+{displayName}+` placeholder for `@ParameterizedTest` invocation names is no longer + parsed using `java.text.MessageFormat`.Consequently, any text in the display name of + the `@ParameterizedTest` method will be included unmodified in the invocation display + name.For example, Kotlin method names and custom display names configured via + `@DisplayName` can now contain apostrophes (`'`) as well as text resembling + `MessageFormat` elements such as `+{0}+` or `+{data}+`. +* Exceptions thrown for files that cannot be deleted when cleaning up a temporary + directory created via `@TempDir` now include the root cause. +* Lifecycle methods are allowed to be declared as `private` again for backwards + compatibility; however, using `private` visibility for lifecycle methods is strongly + discouraged and will be disallowed in a future release. + +==== New Features and Improvements + +* The search algorithm used to find `@MethodSource` factory methods now falls back to + lenient search semantics when a factory method cannot be found by qualified name + (without a parameter list) and also provides better diagnostics when a unique factory + method cannot be found. + + +[[release-notes-5.9.3-junit-vintage]] +=== JUnit Vintage + +No changes. diff --git a/gradle.properties b/gradle.properties index 627bc14582a8..646efedee055 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,13 +1,13 @@ group = org.junit -version = 5.9.2 +version = 5.9.3 jupiterGroup = org.junit.jupiter platformGroup = org.junit.platform -platformVersion = 1.9.2 +platformVersion = 1.9.3 vintageGroup = org.junit.vintage -vintageVersion = 5.9.2 +vintageVersion = 5.9.3 defaultBuiltBy = JUnit Team diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterAll.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterAll.java index 061ee118c4c5..52743ebef416 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterAll.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterAll.java @@ -29,17 +29,20 @@ * *

Method Signatures

* - *

{@code @AfterAll} methods must have a {@code void} return type, must not - * be {@code private}, and must be {@code static} by default. Consequently, - * {@code @AfterAll} methods are not supported in {@link Nested @Nested} test - * classes or as interface default methods unless the test class is - * annotated with {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. + *

{@code @AfterAll} methods must have a {@code void} return type and must be + * {@code static} by default. Consequently, {@code @AfterAll} methods are not + * supported in {@link Nested @Nested} test classes or as interface default + * methods unless the test class is annotated with + * {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. * However, beginning with Java 16 {@code @AfterAll} methods may be declared as * {@code static} in {@link Nested @Nested} test classes, and the * {@code Lifecycle.PER_CLASS} restriction no longer applies. {@code @AfterAll} * methods may optionally declare parameters to be resolved by * {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}. * + *

Using {@code private} visibility for {@code @BeforeAll} methods is + * strongly discouraged and will be disallowed in a future release. + * *

Inheritance and Execution Order

* *

{@code @AfterAll} methods are inherited from superclasses as long as diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterEach.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterEach.java index df5a522e114f..8dfd018fa4f9 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterEach.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/AfterEach.java @@ -28,8 +28,9 @@ * *

Method Signatures

* - *

{@code @AfterEach} methods must have a {@code void} return type, - * must not be {@code private}, and must not be {@code static}. + *

{@code @AfterEach} methods must have a {@code void} return type and must + * not be {@code static}. Using {@code private} visibility is strongly + * discouraged and will be disallowed in a future release. * They may optionally declare parameters to be resolved by * {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}. * diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeAll.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeAll.java index 01b56d393c1a..30eba9b73746 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeAll.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeAll.java @@ -29,17 +29,20 @@ * *

Method Signatures

* - *

{@code @BeforeAll} methods must have a {@code void} return type, must not - * be {@code private}, and must be {@code static} by default. Consequently, - * {@code @BeforeAll} methods are not supported in {@link Nested @Nested} test - * classes or as interface default methods unless the test class is - * annotated with {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. + *

{@code @BeforeAll} methods must have a {@code void} return type and must + * be {@code static} by default. Consequently, {@code @BeforeAll} methods are + * not supported in {@link Nested @Nested} test classes or as interface + * default methods unless the test class is annotated with + * {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. * However, beginning with Java 16 {@code @BeforeAll} methods may be declared as * {@code static} in {@link Nested @Nested} test classes, and the * {@code Lifecycle.PER_CLASS} restriction no longer applies. {@code @BeforeAll} * methods may optionally declare parameters to be resolved by * {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}. * + *

Using {@code private} visibility for {@code @BeforeAll} methods is + * strongly discouraged and will be disallowed in a future release. + * *

Inheritance and Execution Order

* *

{@code @BeforeAll} methods are inherited from superclasses as long as diff --git a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeEach.java b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeEach.java index 79d0f73f0b4d..37ca2498c889 100644 --- a/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeEach.java +++ b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeEach.java @@ -28,8 +28,9 @@ * *

Method Signatures

* - *

{@code @BeforeEach} methods must have a {@code void} return type, - * must not be {@code private}, and must not be {@code static}. + *

{@code @BeforeEach} methods must have a {@code void} return type and must + * not be {@code static}. Using {@code private} visibility is strongly + * discouraged and will be disallowed in a future release. * They may optionally declare parameters to be resolved by * {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}. * diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtils.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtils.java index 27c90790b140..9adb38ff3945 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtils.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/LifecycleMethodUtils.java @@ -61,7 +61,6 @@ private static List findMethodsAndAssertStaticAndNonPrivate(Class tes if (requireStatic) { methods.forEach(method -> assertStatic(annotationType, method)); } - methods.forEach(method -> assertNonPrivate(annotationType, method)); return methods; } @@ -69,10 +68,7 @@ private static List findMethodsAndAssertNonStaticAndNonPrivate(Class Class annotationType, HierarchyTraversalMode traversalMode) { List methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode); - methods.forEach(method -> { - assertNonStatic(annotationType, method); - assertNonPrivate(annotationType, method); - }); + methods.forEach(method -> assertNonStatic(annotationType, method)); return methods; } @@ -99,13 +95,6 @@ private static void assertNonStatic(Class annotationType, } } - private static void assertNonPrivate(Class annotationType, Method method) { - if (ReflectionUtils.isPrivate(method)) { - throw new JUnitException(String.format("@%s method '%s' must not be private.", - annotationType.getSimpleName(), method.toGenericString())); - } - } - private static void assertVoid(Class annotationType, Method method) { if (!returnsVoid(method)) { throw new JUnitException(String.format("@%s method '%s' must not return a value.", diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java index ba5f53d71971..624c9adbce80 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java @@ -30,6 +30,7 @@ import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.Collections; @@ -319,6 +320,7 @@ private void resetPermissionsAndTryToDeleteAgain(Path path, IOException exceptio } catch (Exception suppressed) { exception.addSuppressed(suppressed); + failures.put(path, exception); } } else { @@ -340,13 +342,12 @@ private static void resetPermissions(Path path) { } private IOException createIOExceptionWithAttachedFailures(SortedMap failures) { - // @formatter:off - String joinedPaths = failures.keySet().stream() - .map(this::tryToDeleteOnExit) - .map(this::relativizeSafely) - .map(String::valueOf) + Path emptyPath = Paths.get(""); + String joinedPaths = failures.keySet().stream() // + .map(this::tryToDeleteOnExit) // + .map(this::relativizeSafely) // + .map(path -> emptyPath.equals(path) ? "" : path.toString()) // .collect(joining(", ")); - // @formatter:on IOException exception = new IOException("Failed to delete temp directory " + dir.toAbsolutePath() + ". The following paths could not be deleted (see suppressed exceptions for details): " + joinedPaths); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/condition/AbstractExecutionConditionTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/condition/AbstractExecutionConditionTests.java index 7a21eb3b7e6c..7e02a7d3360b 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/condition/AbstractExecutionConditionTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/condition/AbstractExecutionConditionTests.java @@ -50,7 +50,7 @@ @TestInstance(Lifecycle.PER_CLASS) abstract class AbstractExecutionConditionTests { - private final ExtensionContext context = mock(ExtensionContext.class); + private final ExtensionContext context = mock(); private ConditionEvaluationResult result; diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/support/TypeBasedParameterResolverTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/support/TypeBasedParameterResolverTests.java index 3c02d637d0f7..40a333efb5d8 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/support/TypeBasedParameterResolverTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/api/extension/support/TypeBasedParameterResolverTests.java @@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.lang.annotation.ElementType; @@ -32,7 +33,6 @@ import org.junit.jupiter.api.extension.ParameterResolver; import org.junit.platform.commons.PreconditionViolationException; import org.junit.platform.commons.util.ReflectionUtils; -import org.mockito.Mockito; /** * @since 5.6 @@ -86,13 +86,13 @@ void resolve() { } private static ParameterContext parameterContext(Parameter parameter) { - ParameterContext parameterContext = Mockito.mock(ParameterContext.class); + ParameterContext parameterContext = mock(); when(parameterContext.getParameter()).thenReturn(parameter); return parameterContext; } private static ExtensionContext extensionContext() { - ExtensionContext extensionContext = Mockito.mock(ExtensionContext.class); + ExtensionContext extensionContext = mock(); when(extensionContext.getDisplayName()).thenReturn("Displaying"); return extensionContext; } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/InvalidLifecycleMethodConfigurationTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/InvalidLifecycleMethodConfigurationTests.java index 87cffb5af787..37dc164993ab 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/InvalidLifecycleMethodConfigurationTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/InvalidLifecycleMethodConfigurationTests.java @@ -35,45 +35,25 @@ class InvalidLifecycleMethodConfigurationTests extends AbstractJupiterTestEngine @Test void executeValidTestCaseAlongsideTestCaseWithInvalidNonStaticBeforeAllDeclaration() { - assertExecutionResults(TestCaseWithInvalidNonStaticBeforeAllMethod.class); - } - - @Test - void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateBeforeAllDeclaration() { - assertExecutionResults(TestCaseWithInvalidPrivateBeforeAllMethod.class); + assertContainerFailed(TestCaseWithInvalidNonStaticBeforeAllMethod.class); } @Test void executeValidTestCaseAlongsideTestCaseWithInvalidNonStaticAfterAllDeclaration() { - assertExecutionResults(TestCaseWithInvalidNonStaticAfterAllMethod.class); - } - - @Test - void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateAfterAllDeclaration() { - assertExecutionResults(TestCaseWithInvalidPrivateAfterAllMethod.class); + assertContainerFailed(TestCaseWithInvalidNonStaticAfterAllMethod.class); } @Test void executeValidTestCaseAlongsideTestCaseWithInvalidStaticBeforeEachDeclaration() { - assertExecutionResults(TestCaseWithInvalidStaticBeforeEachMethod.class); - } - - @Test - void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateBeforeEachDeclaration() { - assertExecutionResults(TestCaseWithInvalidPrivateBeforeEachMethod.class); + assertContainerFailed(TestCaseWithInvalidStaticBeforeEachMethod.class); } @Test void executeValidTestCaseAlongsideTestCaseWithInvalidStaticAfterEachDeclaration() { - assertExecutionResults(TestCaseWithInvalidStaticAfterEachMethod.class); - } - - @Test - void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateAfterEachDeclaration() { - assertExecutionResults(TestCaseWithInvalidPrivateAfterEachMethod.class); + assertContainerFailed(TestCaseWithInvalidStaticAfterEachMethod.class); } - private void assertExecutionResults(Class invalidTestClass) { + private void assertContainerFailed(Class invalidTestClass) { EngineExecutionResults executionResults = executeTests(selectClass(TestCase.class), selectClass(invalidTestClass)); Events containers = executionResults.containerEvents(); @@ -112,18 +92,6 @@ void test() { } } - static class TestCaseWithInvalidPrivateBeforeAllMethod { - - // must not be private - @BeforeAll - private static void beforeAll() { - } - - @Test - void test() { - } - } - static class TestCaseWithInvalidNonStaticAfterAllMethod { // must be static @@ -136,18 +104,6 @@ void test() { } } - static class TestCaseWithInvalidPrivateAfterAllMethod { - - // must not be private - @AfterAll - private static void afterAll() { - } - - @Test - void test() { - } - } - static class TestCaseWithInvalidStaticBeforeEachMethod { // must NOT be static @@ -160,18 +116,6 @@ void test() { } } - static class TestCaseWithInvalidPrivateBeforeEachMethod { - - // must NOT be private - @BeforeEach - private void beforeEach() { - } - - @Test - void test() { - } - } - static class TestCaseWithInvalidStaticAfterEachMethod { // must NOT be static @@ -184,16 +128,4 @@ void test() { } } - static class TestCaseWithInvalidPrivateAfterEachMethod { - - // must NOT be private - @AfterEach - private void afterEach() { - } - - @Test - void test() { - } - } - } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/CachingJupiterConfigurationTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/CachingJupiterConfigurationTests.java index 8c7fc8681bf5..cb4f88f73b47 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/CachingJupiterConfigurationTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/CachingJupiterConfigurationTests.java @@ -34,7 +34,7 @@ */ class CachingJupiterConfigurationTests { - private final JupiterConfiguration delegate = mock(JupiterConfiguration.class); + private final JupiterConfiguration delegate = mock(); private final JupiterConfiguration cache = new CachingJupiterConfiguration(delegate); @Test diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/DefaultJupiterConfigurationTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/DefaultJupiterConfigurationTests.java index 1f527013eb08..2592fe256b35 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/DefaultJupiterConfigurationTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/DefaultJupiterConfigurationTests.java @@ -45,14 +45,14 @@ void getDefaultTestInstanceLifecyclePreconditions() { @Test void getDefaultTestInstanceLifecycleWithNoConfigParamSet() { - JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock(ConfigurationParameters.class)); + JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock()); Lifecycle lifecycle = configuration.getDefaultTestInstanceLifecycle(); assertThat(lifecycle).isEqualTo(PER_METHOD); } @Test void getDefaultTempDirCleanupModeWithNoConfigParamSet() { - JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock(ConfigurationParameters.class)); + JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock()); CleanupMode cleanupMode = configuration.getDefaultTempDirCleanupMode(); assertThat(cleanupMode).isEqualTo(ALWAYS); } @@ -74,7 +74,7 @@ void getDefaultTestInstanceLifecycleWithConfigParamSet() { @Test void shouldGetDefaultDisplayNameGeneratorWithConfigParamSet() { - ConfigurationParameters parameters = mock(ConfigurationParameters.class); + ConfigurationParameters parameters = mock(); String key = Constants.DEFAULT_DISPLAY_NAME_GENERATOR_PROPERTY_NAME; when(parameters.get(key)).thenReturn(Optional.of(CustomDisplayNameGenerator.class.getName())); JupiterConfiguration configuration = new DefaultJupiterConfiguration(parameters); @@ -86,7 +86,7 @@ void shouldGetDefaultDisplayNameGeneratorWithConfigParamSet() { @Test void shouldGetStandardAsDefaultDisplayNameGeneratorWithoutConfigParamSet() { - ConfigurationParameters parameters = mock(ConfigurationParameters.class); + ConfigurationParameters parameters = mock(); String key = Constants.DEFAULT_DISPLAY_NAME_GENERATOR_PROPERTY_NAME; when(parameters.get(key)).thenReturn(Optional.empty()); JupiterConfiguration configuration = new DefaultJupiterConfiguration(parameters); @@ -98,7 +98,7 @@ void shouldGetStandardAsDefaultDisplayNameGeneratorWithoutConfigParamSet() { @Test void shouldGetNothingAsDefaultTestMethodOrderWithoutConfigParamSet() { - ConfigurationParameters parameters = mock(ConfigurationParameters.class); + ConfigurationParameters parameters = mock(); String key = Constants.DEFAULT_TEST_METHOD_ORDER_PROPERTY_NAME; when(parameters.get(key)).thenReturn(Optional.empty()); JupiterConfiguration configuration = new DefaultJupiterConfiguration(parameters); @@ -109,7 +109,7 @@ void shouldGetNothingAsDefaultTestMethodOrderWithoutConfigParamSet() { } private void assertDefaultConfigParam(String configValue, Lifecycle expected) { - ConfigurationParameters configParams = mock(ConfigurationParameters.class); + ConfigurationParameters configParams = mock(); when(configParams.get(KEY)).thenReturn(Optional.ofNullable(configValue)); Lifecycle lifecycle = new DefaultJupiterConfiguration(configParams).getDefaultTestInstanceLifecycle(); assertThat(lifecycle).isEqualTo(expected); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/InstantiatingConfigurationParameterConverterTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/InstantiatingConfigurationParameterConverterTests.java index 6923183aedce..5848cda93b4a 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/InstantiatingConfigurationParameterConverterTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/config/InstantiatingConfigurationParameterConverterTests.java @@ -38,7 +38,7 @@ class InstantiatingConfigurationParameterConverterTests { @Test void shouldInstantiateConfiguredClass(LogRecordListener listener) { - ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class); + ConfigurationParameters configurationParameters = mock(); when(configurationParameters.get(KEY)).thenReturn(Optional.of(CustomDisplayNameGenerator.class.getName())); InstantiatingConfigurationParameterConverter converter = new InstantiatingConfigurationParameterConverter<>( @@ -54,7 +54,7 @@ void shouldInstantiateConfiguredClass(LogRecordListener listener) { @Test void shouldReturnEmptyOptionalIfNoConfigurationFound() { - ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class); + ConfigurationParameters configurationParameters = mock(); when(configurationParameters.get(KEY)).thenReturn(Optional.empty()); InstantiatingConfigurationParameterConverter converter = new InstantiatingConfigurationParameterConverter<>( @@ -66,7 +66,7 @@ void shouldReturnEmptyOptionalIfNoConfigurationFound() { @Test void shouldReturnEmptyOptionalIfConfigurationIsBlank() { - ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class); + ConfigurationParameters configurationParameters = mock(); when(configurationParameters.get(KEY)).thenReturn(Optional.of("")); InstantiatingConfigurationParameterConverter converter = new InstantiatingConfigurationParameterConverter<>( @@ -78,7 +78,7 @@ void shouldReturnEmptyOptionalIfConfigurationIsBlank() { @Test void shouldTrimAndInstantiateConfiguredClass(LogRecordListener listener) { - ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class); + ConfigurationParameters configurationParameters = mock(); String classNameWithSpaces = " " + CustomDisplayNameGenerator.class.getName() + " "; when(configurationParameters.get(KEY)).thenReturn(Optional.of(classNameWithSpaces)); @@ -95,7 +95,7 @@ void shouldTrimAndInstantiateConfiguredClass(LogRecordListener listener) { @Test void shouldReturnEmptyOptionalIfNoClassFound(LogRecordListener listener) { - ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class); + ConfigurationParameters configurationParameters = mock(); when(configurationParameters.get(KEY)).thenReturn(Optional.of("random-string")); InstantiatingConfigurationParameterConverter converter = new InstantiatingConfigurationParameterConverter<>( @@ -111,7 +111,7 @@ void shouldReturnEmptyOptionalIfNoClassFound(LogRecordListener listener) { @Test void shouldReturnEmptyOptionalIfClassFoundIsNotATypeOfExpectedType(LogRecordListener listener) { - ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class); + ConfigurationParameters configurationParameters = mock(); when(configurationParameters.get(KEY)).thenReturn(Optional.of(Object.class.getName())); InstantiatingConfigurationParameterConverter converter = new InstantiatingConfigurationParameterConverter<>( @@ -127,7 +127,7 @@ void shouldReturnEmptyOptionalIfClassFoundIsNotATypeOfExpectedType(LogRecordList @Test void shouldReturnEmptyOptionalIfClassNameIsNotFullyQualified(LogRecordListener listener) { - ConfigurationParameters configurationParameters = mock(ConfigurationParameters.class); + ConfigurationParameters configurationParameters = mock(); when(configurationParameters.get(KEY)).thenReturn( Optional.of(CustomDisplayNameGenerator.class.getSimpleName())); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/DisplayNameUtilsTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/DisplayNameUtilsTests.java index 7d61cf86cab6..022f93af9caf 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/DisplayNameUtilsTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/DisplayNameUtilsTests.java @@ -71,7 +71,7 @@ void shouldGetDisplayNameFromSupplierIfNoDisplayNameAnnotationPresent() { @Nested class ClassDisplayNameSupplierTests { - private JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private JupiterConfiguration configuration = mock(); @Test void shouldGetDisplayNameFromDisplayNameGenerationAnnotation() { @@ -107,7 +107,7 @@ void shouldGetDisplayNameFromDefaultDisplayNameGenerator() { @Nested class NestedClassDisplayNameTests { - private JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private JupiterConfiguration configuration = mock(); @Test void shouldGetDisplayNameFromDisplayNameGenerationAnnotation() { @@ -131,7 +131,7 @@ void shouldGetDisplayNameFromDefaultDisplayNameGenerator() { @Nested class MethodDisplayNameTests { - private JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private JupiterConfiguration configuration = mock(); @Test void shouldGetDisplayNameFromDisplayNameGenerationAnnotation() throws Exception { diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/ExtensionContextTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/ExtensionContextTests.java index e9075148b54d..af140f29f877 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/ExtensionContextTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/ExtensionContextTests.java @@ -59,7 +59,7 @@ */ public class ExtensionContextTests { - private final JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private final JupiterConfiguration configuration = mock(); @BeforeEach void setUp() { diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptorTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptorTests.java index 05a3bd435214..ecd65438b861 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptorTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/JupiterTestDescriptorTests.java @@ -53,7 +53,7 @@ class JupiterTestDescriptorTests { private static final UniqueId uniqueId = UniqueId.root("enigma", "foo"); - private final JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private final JupiterConfiguration configuration = mock(); @BeforeEach void setUp() { diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java index 530454f56bc5..7718e771ae2f 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestFactoryTestDescriptorTests.java @@ -30,7 +30,6 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.engine.config.JupiterConfiguration; import org.junit.jupiter.engine.execution.JupiterEngineExecutionContext; -import org.junit.jupiter.engine.extension.MutableExtensionRegistry; import org.junit.platform.engine.TestSource; import org.junit.platform.engine.UniqueId; import org.junit.platform.engine.support.descriptor.ClasspathResourceSource; @@ -39,7 +38,6 @@ import org.junit.platform.engine.support.descriptor.FileSource; import org.junit.platform.engine.support.descriptor.MethodSource; import org.junit.platform.engine.support.descriptor.UriSource; -import org.junit.platform.engine.support.hierarchical.Node; import org.junit.platform.engine.support.hierarchical.OpenTest4JAwareThrowableCollector; /** @@ -134,17 +132,17 @@ class Streams { @BeforeEach void before() throws Exception { - jupiterConfiguration = mock(JupiterConfiguration.class); + jupiterConfiguration = mock(); when(jupiterConfiguration.getDefaultDisplayNameGenerator()).thenReturn(new DisplayNameGenerator.Standard()); - extensionContext = mock(ExtensionContext.class); + extensionContext = mock(); isClosed = false; context = new JupiterEngineExecutionContext(null, null) // .extend() // .withThrowableCollector(new OpenTest4JAwareThrowableCollector()) // .withExtensionContext(extensionContext) // - .withExtensionRegistry(mock(MutableExtensionRegistry.class)) // + .withExtensionRegistry(mock()) // .build(); Method testMethod = CustomStreamTestCase.class.getDeclaredMethod("customStream"); @@ -158,7 +156,7 @@ void streamsFromTestFactoriesShouldBeClosed() { Stream dynamicTestStream = Stream.empty(); prepareMockForTestInstanceWithCustomStream(dynamicTestStream); - descriptor.invokeTestMethod(context, mock(Node.DynamicTestExecutor.class)); + descriptor.invokeTestMethod(context, mock()); assertTrue(isClosed); } @@ -168,7 +166,7 @@ void streamsFromTestFactoriesShouldBeClosedWhenTheyThrow() { Stream integerStream = Stream.of(1, 2); prepareMockForTestInstanceWithCustomStream(integerStream); - descriptor.invokeTestMethod(context, mock(Node.DynamicTestExecutor.class)); + descriptor.invokeTestMethod(context, mock()); assertTrue(isClosed); } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestInstanceLifecycleUtilsTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestInstanceLifecycleUtilsTests.java index 080e42840be8..46fab2deece5 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestInstanceLifecycleUtilsTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestInstanceLifecycleUtilsTests.java @@ -50,7 +50,7 @@ class TestInstanceLifecycleUtilsTests { @Test void getTestInstanceLifecyclePreconditions() { PreconditionViolationException exception = assertThrows(PreconditionViolationException.class, - () -> getTestInstanceLifecycle(null, new DefaultJupiterConfiguration(mock(ConfigurationParameters.class)))); + () -> getTestInstanceLifecycle(null, new DefaultJupiterConfiguration(mock()))); assertThat(exception).hasMessage("testClass must not be null"); exception = assertThrows(PreconditionViolationException.class, @@ -60,14 +60,13 @@ void getTestInstanceLifecyclePreconditions() { @Test void getTestInstanceLifecycleWithNoConfigParamSet() { - Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), - new DefaultJupiterConfiguration(mock(ConfigurationParameters.class))); + Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), new DefaultJupiterConfiguration(mock())); assertThat(lifecycle).isEqualTo(PER_METHOD); } @Test void getTestInstanceLifecycleWithConfigParamSet() { - ConfigurationParameters configParams = mock(ConfigurationParameters.class); + ConfigurationParameters configParams = mock(); when(configParams.get(KEY)).thenReturn(Optional.of(PER_CLASS.name().toLowerCase())); Lifecycle lifecycle = getTestInstanceLifecycle(getClass(), new DefaultJupiterConfiguration(configParams)); assertThat(lifecycle).isEqualTo(PER_CLASS); @@ -75,7 +74,7 @@ void getTestInstanceLifecycleWithConfigParamSet() { @Test void getTestInstanceLifecycleWithLocalConfigThatOverridesCustomDefaultSetViaConfigParam() { - ConfigurationParameters configParams = mock(ConfigurationParameters.class); + ConfigurationParameters configParams = mock(); when(configParams.get(KEY)).thenReturn(Optional.of(PER_CLASS.name().toLowerCase())); Lifecycle lifecycle = getTestInstanceLifecycle(TestCase.class, new DefaultJupiterConfiguration(configParams)); assertThat(lifecycle).isEqualTo(PER_METHOD); @@ -84,16 +83,14 @@ void getTestInstanceLifecycleWithLocalConfigThatOverridesCustomDefaultSetViaConf @Test void getTestInstanceLifecycleFromMetaAnnotationWithNoConfigParamSet() { Class testClass = BaseMetaAnnotatedTestCase.class; - Lifecycle lifecycle = getTestInstanceLifecycle(testClass, - new DefaultJupiterConfiguration(mock(ConfigurationParameters.class))); + Lifecycle lifecycle = getTestInstanceLifecycle(testClass, new DefaultJupiterConfiguration(mock())); assertThat(lifecycle).isEqualTo(PER_CLASS); } @Test void getTestInstanceLifecycleFromSpecializedClassWithNoConfigParamSet() { Class testClass = SpecializedTestCase.class; - Lifecycle lifecycle = getTestInstanceLifecycle(testClass, - new DefaultJupiterConfiguration(mock(ConfigurationParameters.class))); + Lifecycle lifecycle = getTestInstanceLifecycle(testClass, new DefaultJupiterConfiguration(mock())); assertThat(lifecycle).isEqualTo(PER_CLASS); } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestTemplateInvocationTestDescriptorTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestTemplateInvocationTestDescriptorTests.java index 78d92bfcb2c3..b00060eb8e0b 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestTemplateInvocationTestDescriptorTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestTemplateInvocationTestDescriptorTests.java @@ -31,11 +31,11 @@ class TestTemplateInvocationTestDescriptorTests { void invocationsDoNotDeclareExclusiveResources() throws Exception { Class testClass = MyTestCase.class; Method testTemplateMethod = testClass.getDeclaredMethod("testTemplate"); - JupiterConfiguration configuration = mock(JupiterConfiguration.class); + JupiterConfiguration configuration = mock(); when(configuration.getDefaultDisplayNameGenerator()).thenReturn(new DisplayNameGenerator.Standard()); TestTemplateTestDescriptor parent = new TestTemplateTestDescriptor(UniqueId.root("segment", "template"), testClass, testTemplateMethod, configuration); - TestTemplateInvocationContext invocationContext = mock(TestTemplateInvocationContext.class); + TestTemplateInvocationContext invocationContext = mock(); when(invocationContext.getDisplayName(anyInt())).thenReturn("invocation"); TestTemplateInvocationTestDescriptor testDescriptor = new TestTemplateInvocationTestDescriptor( diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptorTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptorTests.java index b808248cb804..f9649ddac33f 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptorTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptorTests.java @@ -32,7 +32,7 @@ * @since 5.0 */ class TestTemplateTestDescriptorTests { - private JupiterConfiguration jupiterConfiguration = mock(JupiterConfiguration.class); + private JupiterConfiguration jupiterConfiguration = mock(); @Test void inheritsTagsFromParent() throws Exception { diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/discovery/DiscoverySelectorResolverTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/discovery/DiscoverySelectorResolverTests.java index 0798a2ca9e67..a71d0665e14b 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/discovery/DiscoverySelectorResolverTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/discovery/DiscoverySelectorResolverTests.java @@ -91,8 +91,8 @@ */ class DiscoverySelectorResolverTests { - private final JupiterConfiguration configuration = mock(JupiterConfiguration.class); - private final LauncherDiscoveryListener discoveryListener = mock(LauncherDiscoveryListener.class); + private final JupiterConfiguration configuration = mock(); + private final LauncherDiscoveryListener discoveryListener = mock(); private final JupiterEngineDescriptor engineDescriptor = new JupiterEngineDescriptor(engineId(), configuration); @BeforeEach diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/AbstractExecutableInvokerTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/AbstractExecutableInvokerTests.java index f633790b34ac..786782339281 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/AbstractExecutableInvokerTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/AbstractExecutableInvokerTests.java @@ -37,12 +37,12 @@ abstract class AbstractExecutableInvokerTests { private static final String ENIGMA = "enigma"; - protected final MethodSource instance = mock(MethodSource.class); + protected final MethodSource instance = mock(); protected Method method; - protected final ExtensionContext extensionContext = mock(ExtensionContext.class); + protected final ExtensionContext extensionContext = mock(); - private final JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private final JupiterConfiguration configuration = mock(); protected final MutableExtensionRegistry extensionRegistry = MutableExtensionRegistry.createRegistryWithDefaultExtensions( configuration); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContextTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContextTests.java index 50696360fcfb..d6e619d886bd 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContextTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContextTests.java @@ -35,9 +35,9 @@ */ class JupiterEngineExecutionContextTests { - private final JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private final JupiterConfiguration configuration = mock(); - private final EngineExecutionListener engineExecutionListener = mock(EngineExecutionListener.class); + private final EngineExecutionListener engineExecutionListener = mock(); private final JupiterEngineExecutionContext originalContext = new JupiterEngineExecutionContext( engineExecutionListener, configuration); @@ -51,10 +51,10 @@ void executionListenerIsHandedOnWhenContextIsExtended() { @Test void extendWithAllAttributes() { - ExtensionContext extensionContext = mock(ExtensionContext.class); + ExtensionContext extensionContext = mock(); MutableExtensionRegistry extensionRegistry = MutableExtensionRegistry.createRegistryWithDefaultExtensions( configuration); - TestInstancesProvider testInstancesProvider = mock(TestInstancesProvider.class); + TestInstancesProvider testInstancesProvider = mock(); JupiterEngineExecutionContext newContext = originalContext.extend() // .withExtensionContext(extensionContext) // .withExtensionRegistry(extensionRegistry) // @@ -68,11 +68,11 @@ void extendWithAllAttributes() { @Test void canOverrideAttributeWhenContextIsExtended() { - ExtensionContext extensionContext = mock(ExtensionContext.class); + ExtensionContext extensionContext = mock(); MutableExtensionRegistry extensionRegistry = MutableExtensionRegistry.createRegistryWithDefaultExtensions( configuration); - TestInstancesProvider testInstancesProvider = mock(TestInstancesProvider.class); - ExtensionContext newExtensionContext = mock(ExtensionContext.class); + TestInstancesProvider testInstancesProvider = mock(); + ExtensionContext newExtensionContext = mock(); JupiterEngineExecutionContext newContext = originalContext.extend() // .withExtensionContext(extensionContext) // diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ParameterResolutionUtilsTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ParameterResolutionUtilsTests.java index 8bdcbb2adeb3..1c371f6f775f 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ParameterResolutionUtilsTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/execution/ParameterResolutionUtilsTests.java @@ -43,12 +43,12 @@ class ParameterResolutionUtilsTests { private static final String ENIGMA = "enigma"; - private final MethodSource instance = mock(MethodSource.class); + private final MethodSource instance = mock(); private Method method; - private final ExtensionContext extensionContext = mock(ExtensionContext.class); + private final ExtensionContext extensionContext = mock(); - private final JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private final JupiterConfiguration configuration = mock(); private final MutableExtensionRegistry extensionRegistry = MutableExtensionRegistry.createRegistryWithDefaultExtensions( configuration); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/CloseablePathCleanupTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/CloseablePathCleanupTests.java index a2d6b710dfe7..7d9557609345 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/CloseablePathCleanupTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/CloseablePathCleanupTests.java @@ -44,7 +44,7 @@ */ class CloseablePathCleanupTests extends AbstractJupiterTestEngineTests { - private final ExtensionContext extensionContext = mock(ExtensionContext.class); + private final ExtensionContext extensionContext = mock(); private TempDirectory.CloseablePath closeablePath; diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java index b7000fbc63da..cbe91c6ea9f6 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/ExtensionRegistryTests.java @@ -41,7 +41,7 @@ class ExtensionRegistryTests { private static final int NUM_DEFAULT_EXTENSIONS = 6; - private final JupiterConfiguration configuration = mock(JupiterConfiguration.class); + private final JupiterConfiguration configuration = mock(); private MutableExtensionRegistry registry = createRegistryWithDefaultExtensions(configuration); diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryPerDeclarationTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryPerDeclarationTests.java index 6cc8336bb202..c98e99e0514d 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryPerDeclarationTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryPerDeclarationTests.java @@ -29,6 +29,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.DirectoryNotEmptyException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; @@ -52,10 +53,10 @@ import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.TestReporter; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.extension.BeforeEachCallback; -import org.junit.jupiter.api.extension.Extension; import org.junit.jupiter.api.extension.ExtensionConfigurationException; import org.junit.jupiter.api.extension.ParameterResolutionException; import org.junit.jupiter.api.extension.RegisterExtension; @@ -63,6 +64,7 @@ import org.junit.jupiter.engine.AbstractJupiterTestEngineTests; import org.junit.jupiter.engine.Constants; import org.junit.jupiter.engine.extension.TempDirectory.FileOperations; +import org.junit.platform.engine.reporting.ReportEntry; import org.junit.platform.testkit.engine.EngineExecutionResults; /** @@ -192,15 +194,27 @@ void canBeUsedViaStaticFieldInsideNestedTestClasses() { .assertStatistics(stats -> stats.started(2).succeeded(2)); } - @Test - @DisplayName("only attempts to delete undeletable directories once") - void onlyAttemptsToDeleteUndeletableDirectoriesOnce() { - var results = executeTestsForClass(UndeletableDirectoryTestCase.class); + @TestFactory + @DisplayName("only attempts to delete undeletable paths once") + Stream onlyAttemptsToDeleteUndeletablePathsOnce() { + return Stream.of( // + dynamicTest("directory", () -> onlyAttemptsToDeleteUndeletablePathOnce(UndeletableDirectoryTestCase.class)), // + dynamicTest("file", () -> onlyAttemptsToDeleteUndeletablePathOnce(UndeletableFileTestCase.class)) // + ); + } + + private void onlyAttemptsToDeleteUndeletablePathOnce(Class testClass) { + var results = executeTestsForClass(testClass); + + var tempDir = results.testEvents().reportingEntryPublished().stream().map( + it -> it.getPayload(ReportEntry.class).orElseThrow()).map( + it -> Path.of(it.getKeyValuePairs().get(UndeletableTestCase.TEMP_DIR))).findAny().orElseThrow(); assertSingleFailedTest(results, // instanceOf(IOException.class), // - message(it -> it.startsWith("Failed to delete temp directory")), // - suppressed(0, instanceOf(IOException.class), message("Simulated failure")), // + message("Failed to delete temp directory " + tempDir.toAbsolutePath() + ". " + // + "The following paths could not be deleted (see suppressed exceptions for details): , undeletable"), // + suppressed(0, instanceOf(DirectoryNotEmptyException.class)), // suppressed(1, instanceOf(IOException.class), message("Simulated failure"))); } @@ -1012,18 +1026,43 @@ private static Map getTempDirs(TestInfo testInfo) { } } - static class UndeletableDirectoryTestCase { + static class UndeletableTestCase { + + static final Path UNDELETABLE_PATH = Path.of("undeletable"); + static final String TEMP_DIR = "TEMP_DIR"; @RegisterExtension - Extension injector = (BeforeEachCallback) context -> context // + BeforeEachCallback injector = context -> context // .getStore(TempDirectory.NAMESPACE) // .put(TempDirectory.FILE_OPERATIONS_KEY, (FileOperations) path -> { - throw new IOException("Simulated failure"); + if (path.endsWith(UNDELETABLE_PATH)) { + throw new IOException("Simulated failure"); + } + else { + Files.delete(path); + } }); + @TempDir + Path tempDir; + + @BeforeEach + void reportTempDir(TestReporter reporter) { + reporter.publishEntry(TEMP_DIR, tempDir.toString()); + } + } + + static class UndeletableDirectoryTestCase extends UndeletableTestCase { @Test - void test(@TempDir Path tempDir) throws Exception { - Files.createDirectory(tempDir.resolve("test-sub-dir")); + void test() throws Exception { + Files.createDirectory(tempDir.resolve(UNDELETABLE_PATH)); + } + } + + static class UndeletableFileTestCase extends UndeletableTestCase { + @Test + void test() throws Exception { + Files.createFile(tempDir.resolve(UNDELETABLE_PATH)); } } } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestReporterParameterResolverTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestReporterParameterResolverTests.java index 2dbc6e8e723e..6acbcafa6126 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestReporterParameterResolverTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestReporterParameterResolverTests.java @@ -13,6 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.lang.reflect.Method; @@ -20,10 +21,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestReporter; -import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.platform.commons.util.ReflectionUtils; -import org.mockito.Mockito; /** * @since 5.0 @@ -45,8 +44,7 @@ void supports() { void resolve() { Parameter parameter = findParameterOfMethod("methodWithTestReporterParameter", TestReporter.class); - TestReporter testReporter = this.resolver.resolveParameter(parameterContext(parameter), - Mockito.mock(ExtensionContext.class)); + TestReporter testReporter = this.resolver.resolveParameter(parameterContext(parameter), mock()); assertNotNull(testReporter); } @@ -56,7 +54,7 @@ private Parameter findParameterOfMethod(String methodName, Class... parameter } private static ParameterContext parameterContext(Parameter parameter) { - ParameterContext parameterContext = Mockito.mock(ParameterContext.class); + ParameterContext parameterContext = mock(); when(parameterContext.getParameter()).thenReturn(parameter); return parameterContext; } diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TimeoutConfigurationTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TimeoutConfigurationTests.java index 94b9e756eb00..d37d922fb9ad 100644 --- a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TimeoutConfigurationTests.java +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TimeoutConfigurationTests.java @@ -47,7 +47,7 @@ */ class TimeoutConfigurationTests { - ExtensionContext extensionContext = mock(ExtensionContext.class); + ExtensionContext extensionContext = mock(); TimeoutConfiguration config = new TimeoutConfiguration(extensionContext); @Test diff --git a/junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestNameFormatter.java b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestNameFormatter.java index 4d8899d89639..a505bd81f91e 100644 --- a/junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestNameFormatter.java +++ b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestNameFormatter.java @@ -31,6 +31,7 @@ class ParameterizedTestNameFormatter { private static final char ELLIPSIS = '\u2026'; + private static final String TEMPORARY_DISPLAY_NAME_PLACEHOLDER = "~~~JUNIT_DISPLAY_NAME~~~"; private final String pattern; private final String displayName; @@ -61,7 +62,8 @@ private String formatSafely(int invocationIndex, Object[] arguments) { String pattern = prepareMessageFormatPattern(invocationIndex, namedArguments); MessageFormat format = new MessageFormat(pattern); Object[] humanReadableArguments = makeReadable(format, namedArguments); - return format.format(humanReadableArguments); + String formatted = format.format(humanReadableArguments); + return formatted.replace(TEMPORARY_DISPLAY_NAME_PLACEHOLDER, this.displayName); } private Object[] extractNamedArguments(Object[] arguments) { @@ -72,7 +74,7 @@ private Object[] extractNamedArguments(Object[] arguments) { private String prepareMessageFormatPattern(int invocationIndex, Object[] arguments) { String result = pattern// - .replace(DISPLAY_NAME_PLACEHOLDER, this.displayName)// + .replace(DISPLAY_NAME_PLACEHOLDER, TEMPORARY_DISPLAY_NAME_PLACEHOLDER)// .replace(INDEX_PLACEHOLDER, String.valueOf(invocationIndex)); if (result.contains(ARGUMENTS_WITH_NAMES_PLACEHOLDER)) { diff --git a/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodArgumentsProvider.java b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodArgumentsProvider.java index a02649c3c3e4..d9fdffab336f 100644 --- a/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodArgumentsProvider.java +++ b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodArgumentsProvider.java @@ -12,7 +12,6 @@ import static java.lang.String.format; import static java.util.Arrays.stream; -import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import static org.junit.jupiter.params.provider.Arguments.arguments; import static org.junit.platform.commons.util.AnnotationUtils.isAnnotated; @@ -29,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.support.AnnotationConsumer; import org.junit.platform.commons.JUnitException; +import org.junit.platform.commons.PreconditionViolationException; import org.junit.platform.commons.util.CollectionUtils; import org.junit.platform.commons.util.Preconditions; import org.junit.platform.commons.util.ReflectionUtils; @@ -41,6 +41,9 @@ class MethodArgumentsProvider implements ArgumentsProvider, AnnotationConsumer isFactoryMethod = // + method -> isConvertibleToStream(method.getReturnType()) && !isTestMethod(method); + @Override public void accept(MethodSource annotation) { this.methodNames = annotation.value(); @@ -48,25 +51,42 @@ public void accept(MethodSource annotation) { @Override public Stream provideArguments(ExtensionContext context) { + Class testClass = context.getRequiredTestClass(); + Method testMethod = context.getRequiredTestMethod(); Object testInstance = context.getTestInstance().orElse(null); // @formatter:off return stream(this.methodNames) - .map(factoryMethodName -> getFactoryMethod(context, factoryMethodName)) + .map(factoryMethodName -> findFactoryMethod(testClass, testMethod, factoryMethodName)) .map(factoryMethod -> context.getExecutableInvoker().invoke(factoryMethod, testInstance)) .flatMap(CollectionUtils::toStream) .map(MethodArgumentsProvider::toArguments); // @formatter:on } - private Method getFactoryMethod(ExtensionContext context, String factoryMethodName) { - Method testMethod = context.getRequiredTestMethod(); + private static Method findFactoryMethod(Class testClass, Method testMethod, String factoryMethodName) { + String originalFactoryMethodName = factoryMethodName; + + // If the user did not provide a factory method name, find a "default" local + // factory method with the same name as the parameterized test method. if (StringUtils.isBlank(factoryMethodName)) { factoryMethodName = testMethod.getName(); + return findFactoryMethodBySimpleName(testClass, testMethod, factoryMethodName); } - if (looksLikeAFullyQualifiedMethodName(factoryMethodName)) { - return getFactoryMethodByFullyQualifiedName(factoryMethodName); + + // Convert local factory method name to fully-qualified method name. + if (!looksLikeAFullyQualifiedMethodName(factoryMethodName)) { + factoryMethodName = testClass.getName() + "#" + factoryMethodName; } - return getFactoryMethodBySimpleOrQualifiedName(context.getRequiredTestClass(), testMethod, factoryMethodName); + + // Find factory method using fully-qualified name. + Method factoryMethod = findFactoryMethodByFullyQualifiedName(testMethod, factoryMethodName); + + // Ensure factory method has a valid return type and is not a test method. + Preconditions.condition(isFactoryMethod.test(factoryMethod), () -> format( + "Could not find valid factory method [%s] for test class [%s] but found the following invalid candidate: %s", + originalFactoryMethodName, testClass.getName(), factoryMethod)); + + return factoryMethod; } private static boolean looksLikeAFullyQualifiedMethodName(String factoryMethodName) { @@ -83,56 +103,54 @@ private static boolean looksLikeAFullyQualifiedMethodName(String factoryMethodNa return indexOfDot < indexOfOpeningParenthesis; } // If we get this far, we conclude the supplied factory method name "looks" - // like it was intended to be a fully qualified method name, even if the + // like it was intended to be a fully-qualified method name, even if the // syntax is invalid. We do this in order to provide better diagnostics for - // the user when a fully qualified method name is in fact invalid. + // the user when a fully-qualified method name is in fact invalid. return true; } - private Method getFactoryMethodByFullyQualifiedName(String fullyQualifiedMethodName) { + private static Method findFactoryMethodByFullyQualifiedName(Method testMethod, String fullyQualifiedMethodName) { String[] methodParts = ReflectionUtils.parseFullyQualifiedMethodName(fullyQualifiedMethodName); String className = methodParts[0]; String methodName = methodParts[1]; String methodParameters = methodParts[2]; + Class clazz = loadRequiredClass(className); - return ReflectionUtils.findMethod(loadRequiredClass(className), methodName, methodParameters).orElseThrow( - () -> new JUnitException(format("Could not find factory method [%s(%s)] in class [%s]", methodName, - methodParameters, className))); - } + // Attempt to find an exact match first. + Method factoryMethod = ReflectionUtils.findMethod(clazz, methodName, methodParameters).orElse(null); + if (factoryMethod != null) { + return factoryMethod; + } - private Method getFactoryMethodBySimpleOrQualifiedName(Class testClass, Method testMethod, - String simpleOrQualifiedMethodName) { - String[] methodParts = ReflectionUtils.parseQualifiedMethodName(simpleOrQualifiedMethodName); - String methodSimpleName = methodParts[0]; - String methodParameters = methodParts[1]; + boolean explicitParameterListSpecified = // + StringUtils.isNotBlank(methodParameters) || fullyQualifiedMethodName.endsWith("()"); - List factoryMethods = findFactoryMethodsBySimpleName(testClass, testMethod, methodSimpleName); - if (factoryMethods.size() == 1) { - return factoryMethods.get(0); - } + // If we didn't find an exact match but an explicit parameter list was specified, + // that's a user configuration error. + Preconditions.condition(!explicitParameterListSpecified, + () -> format("Could not find factory method [%s(%s)] in class [%s]", methodName, methodParameters, + className)); - List exactMatches = filterFactoryMethodsWithMatchingParameters(factoryMethods, - simpleOrQualifiedMethodName, methodParameters); - Preconditions.condition(exactMatches.size() == 1, - () -> format("%d factory methods named [%s] were found in class [%s]: %s", factoryMethods.size(), - simpleOrQualifiedMethodName, testClass.getName(), factoryMethods)); - return exactMatches.get(0); + // Otherwise, fall back to the same lenient search semantics that are used + // to locate a "default" local factory method. + return findFactoryMethodBySimpleName(clazz, testMethod, methodName); } /** - * Find all methods in the given {@code testClass} with the desired {@code factoryMethodName} - * which have return types that can be converted to a {@link Stream}, ignoring the - * {@code testMethod} itself as well as any {@code @Test}, {@code @TestTemplate}, - * or {@code @TestFactory} methods with the same name. + * Find the factory method by searching for all methods in the given {@code clazz} + * with the desired {@code factoryMethodName} which have return types that can be + * converted to a {@link Stream}, ignoring the {@code testMethod} itself as well + * as any {@code @Test}, {@code @TestTemplate}, or {@code @TestFactory} methods + * with the same name. + * @return the single factory method matching the search criteria + * @throws PreconditionViolationException if the factory method was not found or + * multiple competing factory methods with the same name were found */ - private List findFactoryMethodsBySimpleName(Class testClass, Method testMethod, - String factoryMethodName) { + private static Method findFactoryMethodBySimpleName(Class clazz, Method testMethod, String factoryMethodName) { Predicate isCandidate = candidate -> factoryMethodName.equals(candidate.getName()) && !testMethod.equals(candidate); - List candidates = ReflectionUtils.findMethods(testClass, isCandidate); + List candidates = ReflectionUtils.findMethods(clazz, isCandidate); - Predicate isFactoryMethod = method -> isConvertibleToStream(method.getReturnType()) - && !isTestMethod(method); List factoryMethods = candidates.stream().filter(isFactoryMethod).collect(toList()); Preconditions.condition(factoryMethods.size() > 0, () -> { @@ -142,40 +160,23 @@ private List findFactoryMethodsBySimpleName(Class testClass, Method t if (candidates.size() > 0) { return format( "Could not find valid factory method [%s] in class [%s] but found the following invalid candidates: %s", - factoryMethodName, testClass.getName(), candidates); + factoryMethodName, clazz.getName(), candidates); } // Otherwise, report that we didn't find anything. - return format("Could not find factory method [%s] in class [%s]", factoryMethodName, testClass.getName()); + return format("Could not find factory method [%s] in class [%s]", factoryMethodName, clazz.getName()); }); - return factoryMethods; - } - - private static List filterFactoryMethodsWithMatchingParameters(List factoryMethods, - String factoryMethodName, String factoryMethodParameters) { - - if (!factoryMethodName.endsWith(")")) { - // If parameters are not specified, nothing is filtered. - return factoryMethods; - } - - // Compare against canonical parameter list, ignoring whitespace. - String parameterList = factoryMethodParameters.replaceAll("\\s+", ""); - Predicate hasRequiredParameters = method -> { - if (parameterList.isEmpty()) { - return method.getParameterCount() == 0; - } - return parameterList.equals(stream(method.getParameterTypes()).map(Class::getName).collect(joining(","))); - }; - - return factoryMethods.stream().filter(hasRequiredParameters).collect(toList()); + Preconditions.condition(factoryMethods.size() == 1, + () -> format("%d factory methods named [%s] were found in class [%s]: %s", factoryMethods.size(), + factoryMethodName, clazz.getName(), factoryMethods)); + return factoryMethods.get(0); } - private boolean isTestMethod(Method candidate) { + private static boolean isTestMethod(Method candidate) { return isAnnotated(candidate, Test.class) || isAnnotated(candidate, TestTemplate.class) || isAnnotated(candidate, TestFactory.class); } - private Class loadRequiredClass(String className) { + private static Class loadRequiredClass(String className) { return ReflectionUtils.tryToLoadClass(className).getOrThrow( cause -> new JUnitException(format("Could not load class [%s]", className), cause)); } diff --git a/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodSource.java b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodSource.java index 2d0ffe9eb8a5..94f6225fd53f 100644 --- a/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodSource.java +++ b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodSource.java @@ -110,12 +110,20 @@ * The names of factory methods within the test class or in external classes * to use as sources for arguments. * - *

Factory methods in external classes must be referenced by fully - * qualified method name — for example, - * {@code com.example.StringsProviders#blankStrings} or - * {@code com.example.TopLevelClass$NestedClass#classMethod} for a factory + *

Factory methods in external classes must be referenced by + * fully-qualified method name — for example, + * {@code "com.example.StringsProviders#blankStrings"} or + * {@code "com.example.TopLevelClass$NestedClass#classMethod"} for a factory * method in a static nested class. * + *

If a factory method accepts arguments that are provided by a + * {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolver}, + * you can supply the formal parameter list in the qualified method name to + * disambiguate between overloaded variants of the factory method. For example, + * {@code "blankStrings(int)"} for a local qualified method name or + * {@code "com.example.StringsProviders#blankStrings(int)"} for a fully-qualified + * method name. + * *

If no factory method names are declared, a method within the test class * that has the same name as the test method will be used as the factory * method by default. diff --git a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestNameFormatterTests.java b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestNameFormatterTests.java index c7a7616bd6c6..1cbabc827991 100644 --- a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestNameFormatterTests.java +++ b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestNameFormatterTests.java @@ -60,6 +60,24 @@ void formatsDisplayName() { assertEquals("enigma", formatter.format(2)); } + @Test + void formatsDisplayNameContainingApostrophe() { + String displayName = "display'Zero"; + var formatter = formatter(DISPLAY_NAME_PLACEHOLDER, "display'Zero"); + + assertEquals(displayName, formatter.format(1)); + assertEquals(displayName, formatter.format(2)); + } + + @Test + void formatsDisplayNameContainingFormatElements() { + String displayName = "{enigma} {0} '{1}'"; + var formatter = formatter(DISPLAY_NAME_PLACEHOLDER, displayName); + + assertEquals(displayName, formatter.format(1)); + assertEquals(displayName, formatter.format(2)); + } + @Test void formatsInvocationIndex() { var formatter = formatter(INDEX_PLACEHOLDER, "enigma"); @@ -229,13 +247,11 @@ void truncatesArgumentsThatExceedMaxLength() { } private static ParameterizedTestNameFormatter formatter(String pattern, String displayName) { - return new ParameterizedTestNameFormatter(pattern, displayName, mock(ParameterizedTestMethodContext.class), - 512); + return new ParameterizedTestNameFormatter(pattern, displayName, mock(), 512); } private static ParameterizedTestNameFormatter formatter(String pattern, int argumentMaxLength) { - return new ParameterizedTestNameFormatter(pattern, "display name", mock(ParameterizedTestMethodContext.class), - argumentMaxLength); + return new ParameterizedTestNameFormatter(pattern, "display name", mock(), argumentMaxLength); } private static ParameterizedTestNameFormatter formatter(String pattern, String displayName, Method method) { diff --git a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/converter/TypedArgumentConverterTests.java b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/converter/TypedArgumentConverterTests.java index 2a59392cb56f..c32a0313968e 100644 --- a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/converter/TypedArgumentConverterTests.java +++ b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/converter/TypedArgumentConverterTests.java @@ -13,6 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.junit.jupiter.api.Assertions.assertAll; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.lang.annotation.ElementType; @@ -30,7 +31,6 @@ import org.junit.jupiter.params.provider.ValueSource; import org.junit.platform.commons.PreconditionViolationException; import org.junit.platform.commons.util.ReflectionUtils; -import org.mockito.Mockito; /** * Tests for {@link TypedArgumentConverter}. @@ -92,7 +92,7 @@ void targetTypeMismatch() { } private ParameterContext parameterContext(Parameter parameter) { - ParameterContext parameterContext = Mockito.mock(ParameterContext.class); + ParameterContext parameterContext = mock(); when(parameterContext.getParameter()).thenReturn(parameter); return parameterContext; } diff --git a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/EnumArgumentsProviderTests.java b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/EnumArgumentsProviderTests.java index 87a8dfd15c69..eb265d3e8ffc 100644 --- a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/EnumArgumentsProviderTests.java +++ b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/EnumArgumentsProviderTests.java @@ -24,14 +24,13 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.provider.EnumSource.Mode; import org.junit.platform.commons.PreconditionViolationException; -import org.mockito.Mockito; /** * @since 5.0 */ class EnumArgumentsProviderTests { - private ExtensionContext extensionContext = Mockito.mock(ExtensionContext.class); + private ExtensionContext extensionContext = mock(); @Test void providesAllEnumConstants() { diff --git a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/MethodArgumentsProviderTests.java b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/MethodArgumentsProviderTests.java index c8fd3444e5c4..1bd49479bb06 100644 --- a/junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/MethodArgumentsProviderTests.java +++ b/junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/MethodArgumentsProviderTests.java @@ -13,7 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.engine.extension.MutableExtensionRegistry.createRegistryWithDefaultExtensions; -import static org.junit.jupiter.params.provider.MethodArgumentsProviderTests.DefaultFactoryMethodNameTestCase.TEST_METHOD; +import static org.junit.platform.commons.util.ReflectionUtils.findMethod; import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -23,6 +23,7 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; @@ -34,7 +35,6 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolver; -import org.junit.jupiter.engine.config.JupiterConfiguration; import org.junit.jupiter.engine.execution.DefaultExecutableInvoker; import org.junit.jupiter.engine.extension.MutableExtensionRegistry; import org.junit.jupiter.params.ParameterizedTest; @@ -49,26 +49,6 @@ class MethodArgumentsProviderTests { private MutableExtensionRegistry extensionRegistry; - @Test - void throwsExceptionWhenFactoryMethodDoesNotExist() { - var exception = assertThrows(JUnitException.class, () -> provideArguments("unknownMethod").toArray()); - - assertThat(exception.getMessage()).isEqualTo( - "Could not find factory method [unknownMethod] in class [" + TestCase.class.getName() + "]"); - } - - @Test - void throwsExceptionForIllegalReturnType() { - var exception = assertThrows(PreconditionViolationException.class, - () -> provideArguments("providerWithIllegalReturnType").toArray()); - - assertThat(exception.getMessage())// - .containsSubsequence("Could not find valid factory method [providerWithIllegalReturnType] in class [", - TestCase.class.getName() + "]", // - "but found the following invalid candidates: ", // - "[static java.lang.Object org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase.providerWithIllegalReturnType()]"); - } - @Test void providesArgumentsUsingStream() { var arguments = provideArguments("stringStreamProvider"); @@ -194,7 +174,7 @@ void providesArgumentsUsingListOfObjectArrays() { @Test void throwsExceptionWhenNonStaticFactoryMethodIsReferencedAndStaticIsRequired() { - var exception = assertThrows(JUnitException.class, + var exception = assertThrows(PreconditionViolationException.class, () -> provideArguments(NonStaticTestCase.class, null, false, "nonStaticStringStreamProvider").toArray()); assertThat(exception).hasMessageContaining("Cannot invoke non-static method"); @@ -208,9 +188,11 @@ void providesArgumentsFromNonStaticFactoryMethodWhenStaticIsNotRequired() { } @Test - void providesArgumentsUsingDefaultFactoryMethodName() throws Exception { - Class testClass = DefaultFactoryMethodNameTestCase.class; - var testMethod = testClass.getDeclaredMethod(TEST_METHOD, String.class); + void providesArgumentsUsingDefaultFactoryMethodName() { + var testClass = DefaultFactoryMethodNameTestCase.class; + var methodName = "testDefaultFactoryMethodName"; + var testMethod = findMethod(testClass, methodName, String.class).get(); + var arguments = provideArguments(testClass, testMethod, false, ""); assertThat(arguments).containsExactly(array("foo"), array("bar")); @@ -245,35 +227,6 @@ void providesArgumentsUsingExternalAndInternalFactoryMethodsCombined() { assertThat(arguments).containsExactly(array("foo"), array("bar"), array("string1"), array("string2")); } - @Test - void throwsExceptionWhenClassForExternalFactoryMethodCannotBeLoaded() { - var exception = assertThrows(JUnitException.class, - () -> provideArguments("com.example.NonExistentExternalFactoryMethods#stringsProvider").toArray()); - - assertThat(exception.getMessage()).isEqualTo( - "Could not load class [com.example.NonExistentExternalFactoryMethods]"); - } - - @Test - void throwsExceptionWhenExternalFactoryMethodCannotBeFound() { - var exception = assertThrows(JUnitException.class, - () -> provideArguments(ExternalFactoryMethods.class.getName() + "#nonExistentMethod").toArray()); - - assertThat(exception.getMessage()).isEqualTo("Could not find factory method [nonExistentMethod()] in class [" - + ExternalFactoryMethods.class.getName() + "]"); - } - - @Test - void throwsExceptionWhenFullyQualifiedMethodNameIsInvalid() { - var exception = assertThrows(JUnitException.class, - () -> provideArguments(ExternalFactoryMethods.class.getName() + ".wrongSyntax").toArray()); - - assertThat(exception.getMessage()).isEqualTo( - "[" + ExternalFactoryMethods.class.getName() + ".wrongSyntax] is not a valid fully qualified method name: " - + "it must start with a fully qualified class name followed by a '#' and then the method name, " - + "optionally followed by a parameter list enclosed in parentheses."); - } - @Nested class PrimitiveArrays { @@ -364,31 +317,41 @@ void providesArgumentsUsing2dObjectArray() { @Nested class ParameterResolution { + private final Method testMethod = findMethod(TestCase.class, "test").get(); + @BeforeEach void registerParameterResolver() { - JupiterConfiguration configuration = mock(JupiterConfiguration.class); - extensionRegistry = createRegistryWithDefaultExtensions(configuration); + extensionRegistry = createRegistryWithDefaultExtensions(mock()); extensionRegistry.registerExtension(StringResolver.class); + extensionRegistry.registerExtension(StringArrayResolver.class); + extensionRegistry.registerExtension(IntArrayResolver.class); } @Test - void providesArgumentsUsingDefaultFactoryMethodWithParameter() throws Exception { - Class testClass = TestCase.class; - var testMethod = testClass.getDeclaredMethod("overloadedStringStreamProvider", Object.class); - var arguments = provideArguments(testClass, testMethod, false, ""); + void providesArgumentsInferringDefaultFactoryMethodThatAcceptsArgument() { + Method testMethod = findMethod(TestCase.class, "overloadedStringStreamProvider", Object.class).get(); + String factoryMethodName = ""; // signals to use default + var arguments = provideArguments(testMethod, factoryMethodName); assertThat(arguments).containsExactly(array("foo!"), array("bar!")); } @Test - void providesArgumentsUsingFactoryMethodWithParameter() { + void providesArgumentsUsingSimpleNameForFactoryMethodThatAcceptsArgumentWithoutSpecifyingParameterList() { var arguments = provideArguments("stringStreamProviderWithParameter"); assertThat(arguments).containsExactly(array("foo!"), array("bar!")); } @Test - void providesArgumentsUsingFullyQualifiedNameWithParameter() { + void providesArgumentsUsingFullyQualifiedNameForFactoryMethodThatAcceptsArgumentWithoutSpecifyingParameterList() { + var arguments = provideArguments(TestCase.class.getName() + "#stringStreamProviderWithParameter"); + + assertThat(arguments).containsExactly(array("foo!"), array("bar!")); + } + + @Test + void providesArgumentsUsingFullyQualifiedNameSpecifyingParameter() { var arguments = provideArguments( TestCase.class.getName() + "#stringStreamProviderWithParameter(java.lang.String)"); @@ -396,85 +359,338 @@ void providesArgumentsUsingFullyQualifiedNameWithParameter() { } @Test - void providesArgumentsUsingSimpleNameWithoutParameter() { - var arguments = provideArguments("stringStreamProviderWithOrWithoutParameter()"); + void providesArgumentsUsingLocalQualifiedNameSpecifyingParameter() { + var arguments = provideArguments(testMethod, "stringStreamProviderWithParameter(java.lang.String)"); + + assertThat(arguments).containsExactly(array("foo!"), array("bar!")); + } + + @Test + void providesArgumentsUsingFullyQualifiedNameForOverloadedFactoryMethodSpecifyingEmptyParameterList() { + var arguments = provideArguments( + TestCase.class.getName() + "#stringStreamProviderWithOrWithoutParameter()"); assertThat(arguments).containsExactly(array("foo"), array("bar")); } @Test - void providesArgumentsUsingSimpleNameWithParameter() { - var arguments = provideArguments("stringStreamProviderWithOrWithoutParameter(java.lang.String)"); + void providesArgumentsUsingLocalQualifiedNameForOverloadedFactoryMethodSpecifyingEmptyParameterList() { + var arguments = provideArguments(this.testMethod, "stringStreamProviderWithOrWithoutParameter()"); + + assertThat(arguments).containsExactly(array("foo"), array("bar")); + } + + @Test + void providesArgumentsUsingFullyQualifiedNameForOverloadedFactoryMethodSpecifyingParameter() { + var arguments = provideArguments( + TestCase.class.getName() + "#stringStreamProviderWithOrWithoutParameter(java.lang.String)"); assertThat(arguments).containsExactly(array("foo!"), array("bar!")); } - @ParameterizedTest - @ValueSource(strings = { "java.lang.String,java.lang.String", "java.lang.String, java.lang.String", - "java.lang.String, java.lang.String" }) - void providesArgumentsUsingSimpleNameWithMultipleParameters(String params) { - var arguments = provideArguments("stringStreamProviderWithOrWithoutParameter(" + params + ")"); - assertThat(arguments).containsExactly(array("foo!!"), array("bar!!")); + @Test + void providesArgumentsUsingLocalQualifiedNameForOverloadedFactoryMethodSpecifyingParameter() { + var arguments = provideArguments(testMethod, + "stringStreamProviderWithOrWithoutParameter(java.lang.String)"); + + assertThat(arguments).containsExactly(array("foo!"), array("bar!")); + } + + @Test + void failsToProvideArgumentsUsingFullyQualifiedNameSpecifyingInvalidParameterType() { + String method = TestCase.class.getName() + "#stringStreamProviderWithParameter(example.FooBar)"; + var exception = assertThrows(JUnitException.class, () -> provideArguments(method).toArray()); + + assertThat(exception).hasMessage(""" + Failed to load parameter type [example.FooBar] for method [stringStreamProviderWithParameter] \ + in class [org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase]."""); } @Test - void throwsExceptionWhenSeveralFactoryMethodsWithSameNameAreAvailable() { + void failsToProvideArgumentsUsingLocalQualifiedNameSpecifyingInvalidParameterType() { + var method = "stringStreamProviderWithParameter(example.FooBar)"; + var exception = assertThrows(JUnitException.class, + () -> provideArguments(this.testMethod, method).toArray()); + + assertThat(exception).hasMessage(""" + Failed to load parameter type [example.FooBar] for method [stringStreamProviderWithParameter] \ + in class [org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase]."""); + } + + @Test + void failsToProvideArgumentsUsingFullyQualifiedNameSpecifyingIncorrectParameterType() { + String method = TestCase.class.getName() + "#stringStreamProviderWithParameter(java.lang.Integer)"; var exception = assertThrows(PreconditionViolationException.class, - () -> provideArguments("stringStreamProviderWithOrWithoutParameter").toArray()); + () -> provideArguments(method).toArray()); - assertThat(exception.getMessage())// - .startsWith("3 factory methods named [stringStreamProviderWithOrWithoutParameter] were found in " - + "class [org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase]: ")// - .contains("stringStreamProviderWithOrWithoutParameter()", - "stringStreamProviderWithOrWithoutParameter(java.lang.String)", - "stringStreamProviderWithOrWithoutParameter(java.lang.String,java.lang.String)"); + assertThat(exception).hasMessage(""" + Could not find factory method [stringStreamProviderWithParameter(java.lang.Integer)] in \ + class [org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase]"""); } @Test - void providesArgumentsUsingFactoryMethodSelectedViaFullyQualifiedNameWithParameter() { - var arguments = provideArguments( - TestCase.class.getName() + "#stringStreamProviderWithOrWithoutParameter(java.lang.String)"); + void failsToProvideArgumentsUsingLocalQualifiedNameSpecifyingIncorrectParameterType() { + var method = "stringStreamProviderWithParameter(java.lang.Integer)"; + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments(this.testMethod, method).toArray()); - assertThat(arguments).containsExactly(array("foo!"), array("bar!")); + assertThat(exception).hasMessage(""" + Could not find factory method [stringStreamProviderWithParameter(java.lang.Integer)] in \ + class [org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase]"""); + } + + @ParameterizedTest + @ValueSource(strings = { + "org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase#stringStreamProviderWithArrayParameter(java.lang.String[])", + "org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase#stringStreamProviderWithArrayParameter([Ljava.lang.String;)", }) + void providesArgumentsUsingFullyQualifiedNameSpecifyingObjectArrayParameter(String method) { + var arguments = provideArguments(method); + + assertThat(arguments).containsExactly(array("foo :)"), array("bar :)")); + } + + @ParameterizedTest + @ValueSource(strings = { // + "stringStreamProviderWithArrayParameter(java.lang.String[])", + "stringStreamProviderWithArrayParameter([Ljava.lang.String;)" }) + void providesArgumentsUsingLocalQualifiedNameSpecifyingObjectArrayParameter(String method) { + var arguments = provideArguments(this.testMethod, method); + + assertThat(arguments).containsExactly(array("foo :)"), array("bar :)")); + } + + @ParameterizedTest + @ValueSource(strings = { + "org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase#stringStreamProviderWithArrayParameter(int[])", + "org.junit.jupiter.params.provider.MethodArgumentsProviderTests$TestCase#stringStreamProviderWithArrayParameter([I)", }) + void providesArgumentsUsingFullyQualifiedNameSpecifyingPrimitiveArrayParameter(String method) { + var arguments = provideArguments(method); + + assertThat(arguments).containsExactly(array("foo 42"), array("bar 42")); + } + + @ParameterizedTest + @ValueSource(strings = { // + "stringStreamProviderWithArrayParameter(int[])", // + "stringStreamProviderWithArrayParameter([I)" }) + void providesArgumentsUsingLocalQualifiedNameSpecifyingPrimitiveArrayParameter(String method) { + var arguments = provideArguments(this.testMethod, method); + + assertThat(arguments).containsExactly(array("foo 42"), array("bar 42")); } @ParameterizedTest @ValueSource(strings = { "java.lang.String,java.lang.String", "java.lang.String, java.lang.String", "java.lang.String, java.lang.String" }) - void providesArgumentsUsingFactoryMethodSelectedViaFullyQualifiedNameWithMultipleParameters(String params) { - var arguments = provideArguments( - TestCase.class.getName() + "#stringStreamProviderWithOrWithoutParameter(" + params + ")"); + void providesArgumentsUsingFullyQualifiedNameSpecifyingMultipleParameters(String params) { + var method = TestCase.class.getName() + "#stringStreamProviderWithOrWithoutParameter(" + params + ")"; + var arguments = provideArguments(method); + + assertThat(arguments).containsExactly(array("foo!!"), array("bar!!")); + } + + @ParameterizedTest + @ValueSource(strings = { "java.lang.String,java.lang.String", "java.lang.String, java.lang.String", + "java.lang.String, java.lang.String" }) + void providesArgumentsUsingLocalQualifiedNameSpecifyingMultipleParameters(String params) { + var arguments = provideArguments(this.testMethod, + "stringStreamProviderWithOrWithoutParameter(" + params + ")"); assertThat(arguments).containsExactly(array("foo!!"), array("bar!!")); } @Test - void providesArgumentsUsingFactoryMethodSelectedViaFullyQualifiedNameWithoutParameter() { - var arguments = provideArguments( - TestCase.class.getName() + "#stringStreamProviderWithOrWithoutParameter()"); + void providesArgumentsUsingFullyQualifiedNameForOverloadedFactoryMethodWhenParameterListIsNotSpecified() { + var arguments = provideArguments(TestCase.class.getName() + "#stringStreamProviderWithOrWithoutParameter"); + + assertThat(arguments).containsExactly(array("foo"), array("bar")); + } + + @Test + void providesArgumentsUsingLocalQualifiedNameForOverloadedFactoryMethodWhenParameterListIsNotSpecified() { + var arguments = provideArguments("stringStreamProviderWithOrWithoutParameter").toArray(); assertThat(arguments).containsExactly(array("foo"), array("bar")); } } + @Nested + class ErrorCases { + + @Test + void throwsExceptionWhenFullyQualifiedMethodNameSyntaxIsInvalid() { + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments("org.example.wrongSyntax").toArray()); + + assertThat(exception.getMessage()).isEqualTo( + "[org.example.wrongSyntax] is not a valid fully qualified method name: " + + "it must start with a fully qualified class name followed by a '#' and then the method name, " + + "optionally followed by a parameter list enclosed in parentheses."); + } + + @Test + void throwsExceptionWhenClassForExternalFactoryMethodCannotBeLoaded() { + var exception = assertThrows(JUnitException.class, + () -> provideArguments("com.example.NonExistentClass#stringsProvider").toArray()); + + assertThat(exception.getMessage()).isEqualTo("Could not load class [com.example.NonExistentClass]"); + } + + @Test + void throwsExceptionWhenExternalFactoryMethodDoesNotExist() { + String factoryClass = ExternalFactoryMethods.class.getName(); + + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments(factoryClass + "#nonExistentMethod").toArray()); + + assertThat(exception.getMessage()).isEqualTo( + "Could not find factory method [nonExistentMethod] in class [%s]", factoryClass); + } + + @Test + void throwsExceptionWhenLocalFactoryMethodDoesNotExist() { + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments("nonExistentMethod").toArray()); + + assertThat(exception.getMessage()).isEqualTo( + "Could not find factory method [nonExistentMethod] in class [%s]", TestCase.class.getName()); + } + + @Test + void throwsExceptionWhenExternalFactoryMethodAcceptingSingleArgumentDoesNotExist() { + String factoryClass = ExternalFactoryMethods.class.getName(); + + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments(factoryClass + "#nonExistentMethod(int)").toArray()); + + assertThat(exception.getMessage()).isEqualTo( + "Could not find factory method [nonExistentMethod(int)] in class [%s]", factoryClass); + } + + @Test + void throwsExceptionWhenLocalFactoryMethodAcceptingSingleArgumentDoesNotExist() { + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments("nonExistentMethod(int)").toArray()); + + assertThat(exception.getMessage()).isEqualTo( + "Could not find factory method [nonExistentMethod(int)] in class [%s]", TestCase.class.getName()); + } + + @Test + void throwsExceptionWhenExternalFactoryMethodAcceptingMultipleArgumentsDoesNotExist() { + String factoryClass = ExternalFactoryMethods.class.getName(); + + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments(factoryClass + "#nonExistentMethod(int, java.lang.String)").toArray()); + + assertThat(exception.getMessage()).isEqualTo( + "Could not find factory method [nonExistentMethod(int, java.lang.String)] in class [%s]", factoryClass); + } + + @Test + void throwsExceptionWhenLocalFactoryMethodAcceptingMultipleArgumentsDoesNotExist() { + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments("nonExistentMethod(java.lang.String,int)").toArray()); + + assertThat(exception.getMessage()).isEqualTo( + "Could not find factory method [nonExistentMethod(java.lang.String,int)] in class [%s]", + TestCase.class.getName()); + } + + @Test + void throwsExceptionWhenExternalFactoryMethodHasInvalidReturnType() { + String testClass = TestCase.class.getName(); + String factoryClass = ExternalFactoryMethods.class.getName(); + String factoryMethod = factoryClass + "#factoryWithInvalidReturnType"; + + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments(TestCase.class, null, false, factoryMethod).toArray()); + + assertThat(exception.getMessage())// + .containsSubsequence("Could not find valid factory method [" + factoryMethod + "] for test class [", + testClass + "]", // + "but found the following invalid candidate: ", + "static java.lang.Object " + factoryClass + ".factoryWithInvalidReturnType()"); + } + + @Test + void throwsExceptionWhenLocalFactoryMethodHasInvalidReturnType() { + String testClass = TestCase.class.getName(); + String factoryClass = testClass; + String factoryMethod = "factoryWithInvalidReturnType"; + + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments(factoryMethod).toArray()); + + assertThat(exception.getMessage())// + .containsSubsequence("Could not find valid factory method [" + factoryMethod + "] for test class [", + factoryClass + "]", // + "but found the following invalid candidate: ", // + "static java.lang.Object " + factoryClass + ".factoryWithInvalidReturnType()"); + } + + @Test + void throwsExceptionWhenMultipleDefaultFactoryMethodCandidatesExist() { + var testClass = MultipleDefaultFactoriesTestCase.class; + var methodName = "test"; + var testMethod = findMethod(testClass, methodName, String.class).get(); + + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments(testClass, testMethod, false, "").toArray()); + + assertThat(exception.getMessage()).contains(// + "2 factory methods named [test] were found in class [", testClass.getName() + "]: ", // + "$MultipleDefaultFactoriesTestCase.test()", // + "$MultipleDefaultFactoriesTestCase.test(int)"// + ); + } + + @Test + void throwsExceptionWhenMultipleInvalidDefaultFactoryMethodCandidatesExist() { + var testClass = MultipleInvalidDefaultFactoriesTestCase.class; + var methodName = "test"; + var testMethod = findMethod(testClass, methodName, String.class).get(); + + var exception = assertThrows(PreconditionViolationException.class, + () -> provideArguments(testClass, testMethod, false, "").toArray()); + + assertThat(exception.getMessage()).contains(// + "Could not find valid factory method [test] in class [", testClass.getName() + "]", // + "but found the following invalid candidates: ", // + "$MultipleInvalidDefaultFactoriesTestCase.test()", // + "$MultipleInvalidDefaultFactoriesTestCase.test(int)"// + ); + } + + } + // ------------------------------------------------------------------------- private static Object[] array(Object... objects) { return objects; } - private Stream provideArguments(String... methodNames) { - return provideArguments(TestCase.class, null, false, methodNames); + private Stream provideArguments(String... factoryMethodNames) { + return provideArguments(TestCase.class, null, false, factoryMethodNames); + } + + private Stream provideArguments(Method testMethod, String factoryMethodName) { + return provideArguments(TestCase.class, testMethod, false, factoryMethodName); } private Stream provideArguments(Class testClass, Method testMethod, boolean allowNonStaticMethod, - String... methodNames) { + String... factoryMethodNames) { if (testMethod == null) { try { + class DummyClass { + @SuppressWarnings("unused") + public void dummyMethod() { + }; + } // ensure we have a non-null method, even if it's not a real test method. - testMethod = getClass().getMethod("toString"); + testMethod = DummyClass.class.getMethod("dummyMethod"); } catch (Exception ex) { throw new RuntimeException(ex); @@ -483,7 +699,7 @@ private Stream provideArguments(Class testClass, Method testMethod, var methodSource = mock(MethodSource.class); - when(methodSource.value()).thenReturn(methodNames); + when(methodSource.value()).thenReturn(factoryMethodNames); var extensionContext = mock(ExtensionContext.class); when(extensionContext.getTestClass()).thenReturn(Optional.of(testClass)); @@ -506,21 +722,58 @@ private Stream provideArguments(Class testClass, Method testMethod, static class DefaultFactoryMethodNameTestCase { - static final String TEST_METHOD = "testDefaultFactoryMethodName"; + // Test + void testDefaultFactoryMethodName(String param) { + } + // Factory static Stream testDefaultFactoryMethodName() { return Stream.of("foo", "bar"); } + } - void testDefaultFactoryMethodName(String param) { + static class MultipleDefaultFactoriesTestCase { + + // Test + void test(String param) { + } + + // Factory + static Stream test() { + return Stream.of(); + } + + // Another Factory + static Stream test(int num) { + return Stream.of(); + } + } + + static class MultipleInvalidDefaultFactoriesTestCase { + + // Test + void test(String param) { + } + + // NOT a Factory + static String test() { + return null; + } + + // Also NOT a Factory + static Object test(int num) { + return null; } } static class TestCase { + void test() { + } + // --- Invalid --------------------------------------------------------- - static Object providerWithIllegalReturnType() { + static Object factoryWithInvalidReturnType() { return -1; } @@ -534,6 +787,16 @@ static Stream stringStreamProviderWithParameter(String parameter) { return Stream.of("foo" + parameter, "bar" + parameter); } + static Stream stringStreamProviderWithArrayParameter(String[] parameter) { + String suffix = Arrays.stream(parameter).collect(Collectors.joining()); + return Stream.of("foo " + suffix, "bar " + suffix); + } + + static Stream stringStreamProviderWithArrayParameter(int[] parameter) { + return stringStreamProviderWithArrayParameter( + Arrays.stream(parameter).mapToObj(String::valueOf).toArray(String[]::new)); + } + static Stream stringStreamProviderWithOrWithoutParameter() { return stringStreamProvider(); } @@ -546,6 +809,10 @@ static Stream stringStreamProviderWithOrWithoutParameter(String paramete return stringStreamProviderWithParameter(parameter1 + parameter2); } + // Overloaded method, but not a valid return type for a factory method + static void stringStreamProviderWithOrWithoutParameter(String parameter1, int parameter2) { + } + // @ParameterizedTest // @MethodSource // use default, inferred factory method void overloadedStringStreamProvider(Object parameter) { @@ -673,6 +940,10 @@ Stream nonStaticStringStreamProvider() { static class ExternalFactoryMethods { + static Object factoryWithInvalidReturnType() { + return -1; + } + static Stream stringsProvider() { return Stream.of("string1", "string2"); } @@ -698,4 +969,30 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte } } + static class StringArrayResolver implements ParameterResolver { + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { + return parameterContext.getParameter().getType() == String[].class; + } + + @Override + public String[] resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { + return new String[] { ":", ")" }; + } + } + + static class IntArrayResolver implements ParameterResolver { + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { + return parameterContext.getParameter().getType() == int[].class; + } + + @Override + public int[] resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { + return new int[] { 4, 2 }; + } + } + } diff --git a/junit-jupiter-params/src/test/kotlin/ParameterizedTestNameFormatterIntegrationTests.kt b/junit-jupiter-params/src/test/kotlin/ParameterizedTestNameFormatterIntegrationTests.kt new file mode 100644 index 000000000000..829f98aefabc --- /dev/null +++ b/junit-jupiter-params/src/test/kotlin/ParameterizedTestNameFormatterIntegrationTests.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2015-2023 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.TestInfo +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class ParameterizedTestNameFormatterIntegrationTests { + + @ValueSource(strings = ["foo", "bar"]) + @ParameterizedTest + fun defaultDisplayName(param: String, info: TestInfo) { + if (param.equals("foo")) { + assertEquals("[1] foo", info.displayName) + } else { + assertEquals("[2] bar", info.displayName) + } + } + + @ValueSource(strings = ["foo", "bar"]) + @ParameterizedTest(name = "{0}") + fun `1st argument`(param: String, info: TestInfo) { + if (param.equals("foo")) { + assertEquals("foo", info.displayName) + } else { + assertEquals("bar", info.displayName) + } + } + + @ValueSource(strings = ["foo", "bar"]) + @ParameterizedTest(name = "{displayName}") + fun `it's an {enigma} '{0}'`(@Suppress("UNUSED_PARAMETER") param: String, info: TestInfo) { + assertEquals("it's an {enigma} '{0}'(String, TestInfo)", info.displayName) + } + + @ValueSource(strings = ["foo", "bar"]) + @ParameterizedTest(name = "{displayName} - {0}") + fun `displayName and 1st 'argument'`(param: String, info: TestInfo) { + if (param.equals("foo")) { + assertEquals("displayName and 1st 'argument'(String, TestInfo) - foo", info.displayName) + } else { + assertEquals("displayName and 1st 'argument'(String, TestInfo) - bar", info.displayName) + } + } + + @ValueSource(strings = ["foo", "bar"]) + @ParameterizedTest(name = "{0} - {displayName}") + fun `1st 'argument' and displayName`(param: String, info: TestInfo) { + if (param.equals("foo")) { + assertEquals("foo - 1st 'argument' and displayName(String, TestInfo)", info.displayName) + } else { + assertEquals("bar - 1st 'argument' and displayName(String, TestInfo)", info.displayName) + } + } +} diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java index 9b2fb05d8fb0..c20c93b9c2e4 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java @@ -909,44 +909,21 @@ public static String[] parseFullyQualifiedMethodName(String fullyQualifiedMethod + "and then the method name, optionally followed by a parameter list enclosed in parentheses."); String className = fullyQualifiedMethodName.substring(0, indexOfFirstHashtag); - String qualifiedMethodName = fullyQualifiedMethodName.substring(indexOfFirstHashtag + 1); - String[] methodPart = parseQualifiedMethodName(qualifiedMethodName); - - return new String[] { className, methodPart[0], methodPart[1] }; - } - - /** - * Parse the supplied method name into a 2-element {@code String[]} with - * the following content. - * - *

- * - * @param qualifiedMethodName a qualified method name, never {@code null} or blank - * @return a 2-element array of strings containing the parsed values - */ - @API(status = INTERNAL, since = "1.9.2") - public static String[] parseQualifiedMethodName(String qualifiedMethodName) { - String methodName = qualifiedMethodName; + String methodPart = fullyQualifiedMethodName.substring(indexOfFirstHashtag + 1); + String methodName = methodPart; String methodParameters = ""; - if (qualifiedMethodName.endsWith("()")) { - methodName = qualifiedMethodName.substring(0, qualifiedMethodName.length() - 2); + if (methodPart.endsWith("()")) { + methodName = methodPart.substring(0, methodPart.length() - 2); } - else if (qualifiedMethodName.endsWith(")")) { - int indexOfLastOpeningParenthesis = qualifiedMethodName.lastIndexOf('('); - if ((indexOfLastOpeningParenthesis > 0) - && (indexOfLastOpeningParenthesis < qualifiedMethodName.length() - 1)) { - methodName = qualifiedMethodName.substring(0, indexOfLastOpeningParenthesis); - methodParameters = qualifiedMethodName.substring(indexOfLastOpeningParenthesis + 1, - qualifiedMethodName.length() - 1); + else if (methodPart.endsWith(")")) { + int indexOfLastOpeningParenthesis = methodPart.lastIndexOf('('); + if ((indexOfLastOpeningParenthesis > 0) && (indexOfLastOpeningParenthesis < methodPart.length() - 1)) { + methodName = methodPart.substring(0, indexOfLastOpeningParenthesis); + methodParameters = methodPart.substring(indexOfLastOpeningParenthesis + 1, methodPart.length() - 1); } } - - return new String[] { methodName, methodParameters }; + return new String[] { className, methodName, methodParameters }; } /** diff --git a/platform-tests/src/test/java/org/junit/platform/commons/support/ReflectionSupportTests.java b/platform-tests/src/test/java/org/junit/platform/commons/support/ReflectionSupportTests.java index efa4e8105cfe..f14ff26e1fc4 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/support/ReflectionSupportTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/support/ReflectionSupportTests.java @@ -48,7 +48,7 @@ class ReflectionSupportTests { void loadClassDelegates() { assertEquals(ReflectionUtils.loadClass("-"), ReflectionSupport.loadClass("-")); assertEquals(ReflectionUtils.loadClass("A"), ReflectionSupport.loadClass("A")); - assertEquals(ReflectionUtils.loadClass("java.io.Bits"), ReflectionSupport.loadClass("java.io.Bits")); + assertEquals(ReflectionUtils.loadClass("java.nio.Bits"), ReflectionSupport.loadClass("java.nio.Bits")); } @Test @@ -64,7 +64,8 @@ void tryToLoadClassDelegates() { ReflectionSupport.tryToLoadClass("-").toOptional()); assertEquals(ReflectionUtils.tryToLoadClass("A").toOptional(), ReflectionSupport.tryToLoadClass("A").toOptional()); - assertEquals(ReflectionUtils.tryToLoadClass("java.io.Bits"), ReflectionSupport.tryToLoadClass("java.io.Bits")); + assertEquals(ReflectionUtils.tryToLoadClass("java.nio.Bits"), + ReflectionSupport.tryToLoadClass("java.nio.Bits")); } @Test diff --git a/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java b/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java index a9e3c4ece463..d667bce7767b 100644 --- a/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java +++ b/platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java @@ -55,8 +55,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.fixtures.TrackLogRecords; import org.junit.jupiter.api.io.TempDir; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; import org.junit.platform.commons.JUnitException; import org.junit.platform.commons.PreconditionViolationException; import org.junit.platform.commons.logging.LogRecordListener; @@ -702,25 +700,6 @@ void parseFullyQualifiedMethodNameForMethodWithMultipleParameters() { .containsExactly("com.example.Test", "method", "int, java.lang.Object"); } - @ParameterizedTest - @ValueSource(strings = { "method", "method()" }) - void parseSimpleMethodNameForMethodWithoutParameters(String methodName) { - assertThat(ReflectionUtils.parseQualifiedMethodName(methodName))// - .containsExactly("method", ""); - } - - @Test - void parseSimpleMethodNameForMethodWithSingleParameter() { - assertThat(ReflectionUtils.parseQualifiedMethodName("method(java.lang.Object)"))// - .containsExactly("method", "java.lang.Object"); - } - - @Test - void parseSimpleMethodNameForMethodWithMultipleParameters() { - assertThat(ReflectionUtils.parseQualifiedMethodName("method(int, java.lang.Object)"))// - .containsExactly("method", "int, java.lang.Object"); - } - @Test @SuppressWarnings("deprecation") void getOutermostInstancePreconditions() { diff --git a/platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherExecutionResultTests.java b/platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherExecutionResultTests.java index fd9be9511ab9..1d37cfc93e5b 100644 --- a/platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherExecutionResultTests.java +++ b/platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherExecutionResultTests.java @@ -24,7 +24,7 @@ class ConsoleLauncherExecutionResultTests { private final CommandLineOptions options = new CommandLineOptions(); - private final TestExecutionSummary summary = mock(TestExecutionSummary.class); + private final TestExecutionSummary summary = mock(); @Test void hasStatusCode0ForNoTotalFailures() { diff --git a/platform-tests/src/test/java/org/junit/platform/console/tasks/ColorPaletteTests.java b/platform-tests/src/test/java/org/junit/platform/console/tasks/ColorPaletteTests.java index a5dd82529432..3a0909c35edb 100644 --- a/platform-tests/src/test/java/org/junit/platform/console/tasks/ColorPaletteTests.java +++ b/platform-tests/src/test/java/org/junit/platform/console/tasks/ColorPaletteTests.java @@ -22,7 +22,6 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.platform.console.options.Theme; -import org.junit.platform.engine.ConfigurationParameters; import org.junit.platform.engine.TestDescriptor; import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.engine.UniqueId; @@ -187,7 +186,7 @@ void flat_single_color() { private void demoTestRun(TestExecutionListener listener) { TestDescriptor testDescriptor = new TestDescriptorStub(UniqueId.forEngine("demo-engine"), "My Test"); - TestPlan testPlan = TestPlan.from(List.of(testDescriptor), mock(ConfigurationParameters.class)); + TestPlan testPlan = TestPlan.from(List.of(testDescriptor), mock()); listener.testPlanExecutionStarted(testPlan); listener.executionStarted(TestIdentifier.from(testDescriptor)); listener.executionFinished(TestIdentifier.from(testDescriptor), TestExecutionResult.successful()); diff --git a/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/DefaultParallelExecutionConfigurationStrategyTests.java b/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/DefaultParallelExecutionConfigurationStrategyTests.java index 5fcf9b4cb82d..b6cb64223aa9 100644 --- a/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/DefaultParallelExecutionConfigurationStrategyTests.java +++ b/platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/DefaultParallelExecutionConfigurationStrategyTests.java @@ -30,7 +30,7 @@ */ class DefaultParallelExecutionConfigurationStrategyTests { - private ConfigurationParameters configParams = mock(ConfigurationParameters.class); + private ConfigurationParameters configParams = mock(); @BeforeEach void setUp() { diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/TestPlanTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/TestPlanTests.java index b5a0145511de..866e085aa438 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/TestPlanTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/TestPlanTests.java @@ -23,7 +23,7 @@ class TestPlanTests { - private final ConfigurationParameters configParams = mock(ConfigurationParameters.class); + private final ConfigurationParameters configParams = mock(); private EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("foo"), "Foo"); diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/core/CompositeTestExecutionListenerTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/core/CompositeTestExecutionListenerTests.java index 9cfc55503a6b..187513476a77 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/core/CompositeTestExecutionListenerTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/core/CompositeTestExecutionListenerTests.java @@ -24,7 +24,6 @@ import org.junit.jupiter.api.fixtures.TrackLogRecords; import org.junit.platform.commons.logging.LogRecordListener; import org.junit.platform.commons.util.ReflectionUtils; -import org.junit.platform.engine.ConfigurationParameters; import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.engine.UniqueId; import org.junit.platform.engine.reporting.ReportEntry; @@ -92,8 +91,7 @@ void shouldNotThrowExceptionButLogIfTesPlanExecutionStartedListenerMethodFails( LogRecordListener logRecordListener) { var testDescriptor = getDemoMethodTestDescriptor(); - compositeTestExecutionListener().testPlanExecutionStarted( - TestPlan.from(Set.of(testDescriptor), mock(ConfigurationParameters.class))); + compositeTestExecutionListener().testPlanExecutionStarted(TestPlan.from(Set.of(testDescriptor), mock())); assertThatTestListenerErrorLogged(logRecordListener, ThrowingTestExecutionListener.class, "testPlanExecutionStarted"); @@ -104,8 +102,7 @@ void shouldNotThrowExceptionButLogIfTesPlanExecutionFinishedListenerMethodFails( LogRecordListener logRecordListener) { var testDescriptor = getDemoMethodTestDescriptor(); - compositeTestExecutionListener().testPlanExecutionFinished( - TestPlan.from(Set.of(testDescriptor), mock(ConfigurationParameters.class))); + compositeTestExecutionListener().testPlanExecutionFinished(TestPlan.from(Set.of(testDescriptor), mock())); assertThatTestListenerErrorLogged(logRecordListener, ThrowingTestExecutionListener.class, "testPlanExecutionFinished"); diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/core/ExecutionListenerAdapterTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/core/ExecutionListenerAdapterTests.java index 7fb8ea2ef334..c120c5c79349 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/core/ExecutionListenerAdapterTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/core/ExecutionListenerAdapterTests.java @@ -17,9 +17,7 @@ import org.junit.jupiter.api.Test; import org.junit.platform.commons.util.ReflectionUtils; -import org.junit.platform.engine.ConfigurationParameters; import org.junit.platform.engine.TestDescriptor; -import org.junit.platform.engine.TestEngine; import org.junit.platform.engine.UniqueId; import org.junit.platform.engine.reporting.ReportEntry; import org.junit.platform.engine.support.descriptor.DemoMethodTestDescriptor; @@ -36,8 +34,7 @@ class ExecutionListenerAdapterTests { void testReportingEntryPublished() { var testDescriptor = getSampleMethodTestDescriptor(); - var discoveryResult = new LauncherDiscoveryResult(Map.of(mock(TestEngine.class), testDescriptor), - mock(ConfigurationParameters.class)); + var discoveryResult = new LauncherDiscoveryResult(Map.of(mock(), testDescriptor), mock()); var testPlan = InternalTestPlan.from(discoveryResult); var testIdentifier = testPlan.getTestIdentifier(testDescriptor.getUniqueId()); diff --git a/platform-tests/src/test/java/org/junit/platform/launcher/listeners/SummaryGenerationTests.java b/platform-tests/src/test/java/org/junit/platform/launcher/listeners/SummaryGenerationTests.java index 76df87ffb241..7c947bf1d8d7 100644 --- a/platform-tests/src/test/java/org/junit/platform/launcher/listeners/SummaryGenerationTests.java +++ b/platform-tests/src/test/java/org/junit/platform/launcher/listeners/SummaryGenerationTests.java @@ -25,7 +25,6 @@ import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; -import org.junit.platform.engine.ConfigurationParameters; import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.engine.TestSource; import org.junit.platform.engine.UniqueId; @@ -40,7 +39,7 @@ class SummaryGenerationTests { private final SummaryGeneratingListener listener = new SummaryGeneratingListener(); - private final TestPlan testPlan = TestPlan.from(List.of(), mock(ConfigurationParameters.class)); + private final TestPlan testPlan = TestPlan.from(List.of(), mock()); @Test void emptyReport() { diff --git a/platform-tests/src/test/java/org/junit/platform/reporting/legacy/LegacyReportingUtilsTests.java b/platform-tests/src/test/java/org/junit/platform/reporting/legacy/LegacyReportingUtilsTests.java index 35931e1baf9c..2cf36b309f4a 100644 --- a/platform-tests/src/test/java/org/junit/platform/reporting/legacy/LegacyReportingUtilsTests.java +++ b/platform-tests/src/test/java/org/junit/platform/reporting/legacy/LegacyReportingUtilsTests.java @@ -16,7 +16,6 @@ import java.util.Set; import org.junit.jupiter.api.Test; -import org.junit.platform.engine.ConfigurationParameters; import org.junit.platform.engine.TestDescriptor; import org.junit.platform.engine.TestSource; import org.junit.platform.engine.UniqueId; @@ -72,13 +71,13 @@ void legacyReportingClassNameForDescendantOfTestIdentifierWithClassSourceIsClass } private String getClassName(UniqueId uniqueId) { - var testPlan = TestPlan.from(Set.of(engineDescriptor), mock(ConfigurationParameters.class)); + var testPlan = TestPlan.from(Set.of(engineDescriptor), mock()); return LegacyReportingUtils.getClassName(testPlan, testPlan.getTestIdentifier(uniqueId)); } @SuppressWarnings("deprecation") private String getClassNameFromOldLocation(UniqueId uniqueId) { - var testPlan = TestPlan.from(Set.of(engineDescriptor), mock(ConfigurationParameters.class)); + var testPlan = TestPlan.from(Set.of(engineDescriptor), mock()); return org.junit.platform.launcher.listeners.LegacyReportingUtils.getClassName(testPlan, testPlan.getTestIdentifier(uniqueId)); } diff --git a/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListenerTests.java b/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListenerTests.java index a0bcf137d122..1ae505f8ba54 100644 --- a/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListenerTests.java +++ b/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListenerTests.java @@ -42,7 +42,6 @@ import org.joox.Match; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.junit.platform.engine.ConfigurationParameters; import org.junit.platform.engine.TestEngine; import org.junit.platform.engine.UniqueId; import org.junit.platform.engine.reporting.ReportEntry; @@ -370,7 +369,7 @@ void printsExceptionWhenReportsDirCannotBeCreated() throws Exception { var out = new StringWriter(); var listener = new LegacyXmlReportGeneratingListener(reportsDir, new PrintWriter(out)); - listener.testPlanExecutionStarted(TestPlan.from(Set.of(), mock(ConfigurationParameters.class))); + listener.testPlanExecutionStarted(TestPlan.from(Set.of(), mock())); assertThat(out.toString()).containsSubsequence("Could not create reports directory", "FileAlreadyExistsException", "at "); @@ -386,7 +385,7 @@ void printsExceptionWhenReportCouldNotBeWritten() throws Exception { var out = new StringWriter(); var listener = new LegacyXmlReportGeneratingListener(tempDirectory, new PrintWriter(out)); - listener.testPlanExecutionStarted(TestPlan.from(Set.of(engineDescriptor), mock(ConfigurationParameters.class))); + listener.testPlanExecutionStarted(TestPlan.from(Set.of(engineDescriptor), mock())); listener.executionFinished(TestIdentifier.from(engineDescriptor), successful()); assertThat(out.toString()).containsSubsequence("Could not write XML report", "Exception", "at "); @@ -397,7 +396,7 @@ void writesReportEntriesToSystemOutElement() throws Exception { var engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine"); var childUniqueId = UniqueId.root("child", "test"); engineDescriptor.addChild(new TestDescriptorStub(childUniqueId, "test")); - var testPlan = TestPlan.from(Set.of(engineDescriptor), mock(ConfigurationParameters.class)); + var testPlan = TestPlan.from(Set.of(engineDescriptor), mock()); var out = new StringWriter(); var listener = new LegacyXmlReportGeneratingListener(tempDirectory, new PrintWriter(out)); diff --git a/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportDataTests.java b/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportDataTests.java index 65fd5acd0d92..fb88bc9f872b 100644 --- a/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportDataTests.java +++ b/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportDataTests.java @@ -30,7 +30,7 @@ */ class XmlReportDataTests { - private final ConfigurationParameters configParams = mock(ConfigurationParameters.class); + private final ConfigurationParameters configParams = mock(); @Test void resultsOfTestIdentifierWithoutAnyReportedEventsAreEmpty() { diff --git a/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportWriterTests.java b/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportWriterTests.java index 4c278dc6b2fa..8487afb1bceb 100644 --- a/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportWriterTests.java +++ b/platform-tests/src/test/java/org/junit/platform/reporting/legacy/xml/XmlReportWriterTests.java @@ -48,7 +48,7 @@ */ class XmlReportWriterTests { - private final ConfigurationParameters configParams = mock(ConfigurationParameters.class); + private final ConfigurationParameters configParams = mock(); private EngineDescriptor engineDescriptor = new EngineDescriptor(UniqueId.forEngine("engine"), "Engine"); diff --git a/platform-tests/src/test/java/org/junit/platform/runner/JUnitPlatformRunnerTests.java b/platform-tests/src/test/java/org/junit/platform/runner/JUnitPlatformRunnerTests.java index 30bdb61aa671..a515e6dd2990 100644 --- a/platform-tests/src/test/java/org/junit/platform/runner/JUnitPlatformRunnerTests.java +++ b/platform-tests/src/test/java/org/junit/platform/runner/JUnitPlatformRunnerTests.java @@ -465,7 +465,7 @@ void convertsTestIdentifiersIntoDescriptions() { TestDescriptor container2 = new TestDescriptorStub(UniqueId.root("root", "container2"), "container2"); container2.addChild(new TestDescriptorStub(UniqueId.root("root", "test2a"), "test2a")); container2.addChild(new TestDescriptorStub(UniqueId.root("root", "test2b"), "test2b")); - var testPlan = TestPlan.from(List.of(container1, container2), mock(ConfigurationParameters.class)); + var testPlan = TestPlan.from(List.of(container1, container2), mock()); var launcher = mock(Launcher.class); when(launcher.discover(any())).thenReturn(testPlan); @@ -502,7 +502,7 @@ private static void assertExcludes(Filter filter, T excluded) { @Nested class Filtering { - private final ConfigurationParameters configParams = mock(ConfigurationParameters.class); + private final ConfigurationParameters configParams = mock(); @Test void appliesFilter() throws Exception { @@ -777,8 +777,7 @@ private TestDescriptor testDescriptorWithTags(String... tag) { private LauncherDiscoveryRequest instantiateRunnerAndCaptureGeneratedRequest(Class testClass) { var launcher = mock(Launcher.class); var captor = ArgumentCaptor.forClass(LauncherDiscoveryRequest.class); - when(launcher.discover(captor.capture())).thenReturn( - TestPlan.from(Set.of(), mock(ConfigurationParameters.class))); + when(launcher.discover(captor.capture())).thenReturn(TestPlan.from(Set.of(), mock())); new JUnitPlatform(testClass, launcher);