Use new features from JUnit Jupiter 5.11

This commit primarily migrates to the new argumentSet() feature but also
applies additional polishing to our use of parameterized tests.

See gh-33395
This commit is contained in:
Sam Brannen
2024-08-16 13:48:19 +02:00
parent 2eff5cb463
commit d749d2949d
23 changed files with 173 additions and 165 deletions

View File

@@ -21,6 +21,7 @@ import java.util.stream.Stream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Named;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -40,6 +41,7 @@ import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Named.named;
import static org.mockito.Mockito.mock;
/**
@@ -51,8 +53,11 @@ import static org.mockito.Mockito.mock;
class CorsAbstractHandlerMappingTests {
@SuppressWarnings("unused")
private static Stream<TestHandlerMapping> pathPatternsArguments() {
return Stream.of(new TestHandlerMapping(new PathPatternParser()), new TestHandlerMapping());
private static Stream<Named<TestHandlerMapping>> pathPatternsArguments() {
return Stream.of(
named("TestHandlerMapping with PathPatternParser", new TestHandlerMapping(new PathPatternParser())),
named("TestHandlerMapping without PathPatternParser", new TestHandlerMapping())
);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,6 +65,7 @@ import org.springframework.web.util.UrlPathHelper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Named.named;
/**
* Test fixture with {@link RequestMappingInfoHandlerMapping}.
@@ -86,11 +87,13 @@ class RequestMappingInfoHandlerMappingTests {
TestRequestMappingInfoHandlerMapping mapping2 = new TestRequestMappingInfoHandlerMapping();
mapping2.setUrlPathHelper(pathHelper);
return Stream.of(mapping1, mapping2).peek(mapping -> {
mapping.setApplicationContext(new StaticWebApplicationContext());
mapping.registerHandler(controller);
mapping.afterPropertiesSet();
});
return Stream.of(named("defaults", mapping1), named("setRemoveSemicolonContent(false)", mapping2))
.peek(named -> {
TestRequestMappingInfoHandlerMapping mapping = named.getPayload();
mapping.setApplicationContext(new StaticWebApplicationContext());
mapping.registerHandler(controller);
mapping.afterPropertiesSet();
});
}

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.stream.Stream;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
@@ -35,6 +36,7 @@ import org.springframework.web.util.pattern.PathPatternParser;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Named.named;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
@@ -47,10 +49,13 @@ import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
class RequestMappingInfoTests {
@SuppressWarnings("unused")
static Stream<RequestMappingInfo.Builder> pathPatternsArguments() {
static Stream<Named<RequestMappingInfo.Builder>> pathPatternsArguments() {
RequestMappingInfo.BuilderConfiguration config = new RequestMappingInfo.BuilderConfiguration();
config.setPathMatcher(new AntPathMatcher());
return Stream.of(RequestMappingInfo.paths(), RequestMappingInfo.paths().options(config));
return Stream.of(
named("PathPatternParser", RequestMappingInfo.paths()),
named("AntPathMatcher", RequestMappingInfo.paths().options(config))
);
}

View File

@@ -27,6 +27,7 @@ import java.util.stream.Stream;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Named;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@@ -58,6 +59,7 @@ import org.springframework.web.util.ServletRequestPathUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.jupiter.api.Named.named;
/**
* Tests for {@link CrossOrigin @CrossOrigin} annotated methods.
@@ -70,7 +72,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
class CrossOriginTests {
@SuppressWarnings("unused")
static Stream<TestRequestMappingInfoHandlerMapping> pathPatternsArguments() {
static Stream<Named<TestRequestMappingInfoHandlerMapping>> pathPatternsArguments() {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
Properties props = new Properties();
props.setProperty("myOrigin", "https://example.com");
@@ -87,7 +89,7 @@ class CrossOriginTests {
wac.getAutowireCapableBeanFactory().initializeBean(mapping2, "mapping2");
wac.close();
return Stream.of(mapping1, mapping2);
return Stream.of(named("PathPatternParser", mapping1), named("AntPathMatcher", mapping2));
}

View File

@@ -60,6 +60,8 @@ import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.jupiter.api.Named.named;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.mock;
/**
@@ -83,7 +85,10 @@ class RequestMappingHandlerMappingTests {
mapping2.setPatternParser(null);
mapping2.setApplicationContext(wac2);
return Stream.of(Arguments.of(mapping1, wac1), Arguments.of(mapping2, wac2));
return Stream.of(
arguments(named("PathPatternParser", mapping1), wac1),
arguments(named("AntPathMatcher", mapping2), wac2)
);
}
@Test