Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ on GitHub.
in their enclosing class. This undesired change in behavior has now been reverted so
that tests in `@Nested` test classes are always executed _after_ tests in enclosing test
classes again.
* Fix support for `AnnotationBasedArgumentsProvider` implementations that override the
deprecated `provideArguments(ExtensionContext, Annotation)` method.

[[release-notes-5.13.1-junit-jupiter-deprecations-and-breaking-changes]]
==== Deprecations and Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public final class AnnotationConsumerInitializer {
private static final List<AnnotationConsumingMethodSignature> annotationConsumingMethodSignatures = asList( //
new AnnotationConsumingMethodSignature("accept", 1, 0), //
new AnnotationConsumingMethodSignature("provideArguments", 3, 2), //
new AnnotationConsumingMethodSignature("provideArguments", 2, 1), //
new AnnotationConsumingMethodSignature("convert", 3, 2));

private AnnotationConsumerInitializer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.AnnotationBasedArgumentConverter;
import org.junit.jupiter.params.converter.JavaTimeConversionPattern;
import org.junit.jupiter.params.provider.AnnotationBasedArgumentsProvider;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.FieldSource;
import org.junit.platform.commons.JUnitException;

@DisplayName("AnnotationConsumerInitializer")
Expand All @@ -52,10 +56,11 @@ void shouldInitializeAnnotationConsumer() throws NoSuchMethodException {
source -> assertThat(source.value()).containsExactly("a", "b"));
}

@Test
@ParameterizedTest
@FieldSource("argumentsProviders")
@DisplayName("should initialize annotation-based ArgumentsProvider")
void shouldInitializeAnnotationBasedArgumentsProvider() throws NoSuchMethodException {
var instance = new SomeAnnotationBasedArgumentsProvider();
void shouldInitializeAnnotationBasedArgumentsProvider(AbstractAnnotationBasedArgumentsProvider instance)
throws NoSuchMethodException {
var method = SubjectClass.class.getDeclaredMethod("foo");
var initialisedAnnotationConsumer = initialize(method, instance);

Expand Down Expand Up @@ -102,20 +107,32 @@ void shouldThrowExceptionWhenParameterIsNotAnnotated() throws NoSuchMethodExcept
assertThatThrownBy(() -> initialize(parameter, instance)).isInstanceOf(JUnitException.class);
}

@Test
void shouldInitializeForEachAnnotations() throws NoSuchMethodException {
var instance = spy(new SomeAnnotationBasedArgumentsProvider());
@ParameterizedTest
@FieldSource("argumentsProviders")
void shouldInitializeForEachAnnotations(AbstractAnnotationBasedArgumentsProvider provider)
throws NoSuchMethodException {
var instance = spy(provider);
var method = SubjectClass.class.getDeclaredMethod("repeatableAnnotation", String.class);

initialize(method, instance);

verify(instance, times(2)).accept(any(CsvSource.class));
}

private static class SomeAnnotationBasedArgumentsProvider extends AnnotationBasedArgumentsProvider<CsvSource> {
static Supplier<List<Named<? extends AbstractAnnotationBasedArgumentsProvider>>> argumentsProviders = () -> List.of( //
Named.of("current", new SomeAnnotationBasedArgumentsProvider()), //
Named.of("deprecated", new DeprecatedAnnotationBasedArgumentsProvider()) //
);

private static abstract class AbstractAnnotationBasedArgumentsProvider
extends AnnotationBasedArgumentsProvider<CsvSource> {

List<CsvSource> annotations = new ArrayList<>();

}

private static class SomeAnnotationBasedArgumentsProvider extends AbstractAnnotationBasedArgumentsProvider {

@Override
protected Stream<? extends Arguments> provideArguments(ParameterDeclarations parameters,
ExtensionContext context, CsvSource annotation) {
Expand All @@ -124,6 +141,16 @@ protected Stream<? extends Arguments> provideArguments(ParameterDeclarations par
}
}

private static class DeprecatedAnnotationBasedArgumentsProvider extends AbstractAnnotationBasedArgumentsProvider {

@Override
@SuppressWarnings("deprecation")
protected Stream<? extends Arguments> provideArguments(ExtensionContext context, CsvSource annotation) {
annotations.add(annotation);
return Stream.empty();
}
}

private static class SomeAnnotationBasedArgumentConverter
extends AnnotationBasedArgumentConverter<JavaTimeConversionPattern> {

Expand Down