Skip to content

Commit

Permalink
Allow repeating ExtendWith annotation on fields and parameters
Browse files Browse the repository at this point in the history
Fixes #4059.
  • Loading branch information
marcphilipp committed Oct 21, 2024
1 parent a3192bd commit fb1254c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ on GitHub.

* Extensions can once again be registered via multiple `@ExtendWith` meta-annotations on
the same composed annotation on a field within a test class.
* `@ExtendWith` annotations can now also be repeated when used directly on fields and
parameters.
* All `@...Source` annotations of parameterized tests can now also be repeated when used
as meta annotations.


[[release-notes-5.11.3-junit-jupiter-deprecations-and-breaking-changes]]
==== Deprecations and Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @see ExtendWith
* @see java.lang.annotation.Repeatable
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ void testTemplateMethodParameter() {
assertTestsSucceeded(TestTemplateMethodParameterTestCase.class, 2);
}

@Test
void multipleRegistrationsViaParameter(@TrackLogRecords LogRecordListener listener) {
assertOneTestSucceeded(MultipleRegistrationsViaParameterTestCase.class);
assertThat(getRegisteredLocalExtensions(listener)).containsExactly("LongParameterResolver", "DummyExtension");
}

@Test
void staticField() {
assertOneTestSucceeded(StaticFieldTestCase.class);
Expand All @@ -147,9 +153,11 @@ void fieldsWithTestInstancePerClass() {
assertOneTestSucceeded(TestInstancePerClassFieldTestCase.class);
}

@Test
void multipleRegistrationsViaField(@TrackLogRecords LogRecordListener listener) {
assertOneTestSucceeded(MultipleRegistrationsViaFieldTestCase.class);
@ParameterizedTest
@ValueSource(classes = { MultipleMixedRegistrationsViaFieldTestCase.class,
MultipleExtendWithRegistrationsViaFieldTestCase.class })
void multipleRegistrationsViaField(Class<?> testClass, @TrackLogRecords LogRecordListener listener) {
assertOneTestSucceeded(testClass);
assertThat(getRegisteredLocalExtensions(listener)).containsExactly("LongParameterResolver", "DummyExtension");
}

Expand Down Expand Up @@ -567,14 +575,37 @@ private static TestTemplateInvocationContext emptyTestTemplateInvocationContext(
}
}

static class MultipleRegistrationsViaFieldTestCase {
@ExtendWith(LongParameterResolver.class)
static class MultipleRegistrationsViaParameterTestCase {

@Test
void test(@ExtendWith(DummyExtension.class) @ExtendWith(LongParameterResolver.class) Long number) {
assertThat(number).isEqualTo(42L);
}
}

static class MultipleMixedRegistrationsViaFieldTestCase {

@ExtendWith(LongParameterResolver.class)
@RegisterExtension
DummyExtension dummy = new DummyExtension();

@Test
void test() {
void test(Long number) {
assertThat(number).isEqualTo(42L);
}
}

static class MultipleExtendWithRegistrationsViaFieldTestCase {

@SuppressWarnings("unused")
@ExtendWith(LongParameterResolver.class)
@ExtendWith(DummyExtension.class)
Object field;

@Test
void test(Long number) {
assertThat(number).isEqualTo(42L);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideInAPackage;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideInAnyPackage;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleName;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.type;
import static com.tngtech.archunit.core.domain.JavaModifier.PUBLIC;
import static com.tngtech.archunit.core.domain.properties.HasModifiers.Predicates.modifier;
import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.name;
Expand Down Expand Up @@ -51,7 +50,6 @@
import com.tngtech.archunit.library.GeneralCodingRules;

import org.apiguardian.api.API;
import org.junit.jupiter.api.extension.ExtendWith;

@AnalyzeClasses(locations = ArchUnitTests.AllJars.class)
class ArchUnitTests {
Expand All @@ -72,7 +70,6 @@ class ArchUnitTests {
.that(nameStartingWith("org.junit.")) //
.and().areAnnotations() //
.and().areAnnotatedWith(Repeatable.class) //
.and(are(not(type(ExtendWith.class)))) // to be resolved in https://github.com/junit-team/junit5/issues/4059
.should(haveContainerAnnotationWithSameRetentionPolicy()) //
.andShould(haveContainerAnnotationWithSameTargetTypes());

Expand Down

0 comments on commit fb1254c

Please sign in to comment.