Skip to content

Commit a09cc22

Browse files
wilkinsonamhalbritter
authored andcommitted
Allow a WebEndpointTest to only run against certain infrastructure
Closes gh-32054
1 parent 3cb1f36 commit a09cc22

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/test/WebEndpointTest.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,13 +20,18 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
23+
import java.util.List;
24+
import java.util.function.Function;
2325

2426
import org.junit.jupiter.api.TestTemplate;
2527
import org.junit.jupiter.api.extension.ExtendWith;
2628

29+
import org.springframework.boot.actuate.endpoint.web.test.WebEndpointTestInvocationContextProvider.WebEndpointsInvocationContext;
30+
import org.springframework.context.ConfigurableApplicationContext;
31+
2732
/**
28-
* Signals that a test should be performed against all web endpoint implementations
29-
* (Jersey, Web MVC, and WebFlux)
33+
* Signals that a test should be run against one or more of the web endpoint
34+
* infrastructure implementations (Jersey, Web MVC, and WebFlux)
3035
*
3136
* @author Andy Wilkinson
3237
*/
@@ -36,4 +41,42 @@
3641
@ExtendWith(WebEndpointTestInvocationContextProvider.class)
3742
public @interface WebEndpointTest {
3843

44+
/**
45+
* The infrastructure against which the test should run.
46+
* @return the infrastructure to run the tests against
47+
*/
48+
Infrastructure[] infrastructure() default { Infrastructure.JERSEY, Infrastructure.MVC, Infrastructure.WEBFLUX };
49+
50+
enum Infrastructure {
51+
52+
/**
53+
* Actuator running on the Jersey-based infrastructure.
54+
*/
55+
JERSEY("Jersey", WebEndpointTestInvocationContextProvider::createJerseyContext),
56+
57+
/**
58+
* Actuator running on the WebMVC-based infrastructure.
59+
*/
60+
MVC("WebMvc", WebEndpointTestInvocationContextProvider::createWebMvcContext),
61+
62+
/**
63+
* Actuator running on the WebFlux-based infrastructure.
64+
*/
65+
WEBFLUX("WebFlux", WebEndpointTestInvocationContextProvider::createWebFluxContext);
66+
67+
private final String name;
68+
69+
private final Function<List<Class<?>>, ConfigurableApplicationContext> contextFactory;
70+
71+
Infrastructure(String name, Function<List<Class<?>>, ConfigurableApplicationContext> contextFactory) {
72+
this.name = name;
73+
this.contextFactory = contextFactory;
74+
}
75+
76+
WebEndpointsInvocationContext createInvocationContext() {
77+
return new WebEndpointsInvocationContext(this.name, this.contextFactory);
78+
}
79+
80+
}
81+
3982
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/test/WebEndpointTestInvocationContextProvider.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
import org.junit.jupiter.api.extension.ParameterResolver;
3737
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
3838
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
39+
import org.junit.platform.commons.util.AnnotationUtils;
3940

4041
import org.springframework.boot.actuate.endpoint.invoke.convert.ConversionServiceParameterValueMapper;
4142
import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
@@ -45,6 +46,7 @@
4546
import org.springframework.boot.actuate.endpoint.web.jersey.JerseyEndpointResourceFactory;
4647
import org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping;
4748
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
49+
import org.springframework.boot.actuate.endpoint.web.test.WebEndpointTest.Infrastructure;
4850
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
4951
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
5052
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
@@ -91,32 +93,30 @@ public boolean supportsTestTemplate(ExtensionContext context) {
9193
@Override
9294
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
9395
ExtensionContext extensionContext) {
94-
return Stream.of(
95-
new WebEndpointsInvocationContext("Jersey",
96-
WebEndpointTestInvocationContextProvider::createJerseyContext),
97-
new WebEndpointsInvocationContext("WebMvc",
98-
WebEndpointTestInvocationContextProvider::createWebMvcContext),
99-
new WebEndpointsInvocationContext("WebFlux",
100-
WebEndpointTestInvocationContextProvider::createWebFluxContext));
96+
WebEndpointTest webEndpointTest = AnnotationUtils
97+
.findAnnotation(extensionContext.getRequiredTestMethod(), WebEndpointTest.class)
98+
.orElseThrow(() -> new IllegalStateException("Unable to find WebEndpointTest annotation on %s"
99+
.formatted(extensionContext.getRequiredTestMethod())));
100+
return Stream.of(webEndpointTest.infrastructure()).distinct().map(Infrastructure::createInvocationContext);
101101
}
102102

103-
private static ConfigurableApplicationContext createJerseyContext(List<Class<?>> classes) {
103+
static ConfigurableApplicationContext createJerseyContext(List<Class<?>> classes) {
104104
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext();
105105
classes.add(JerseyEndpointConfiguration.class);
106106
context.register(ClassUtils.toClassArray(classes));
107107
context.refresh();
108108
return context;
109109
}
110110

111-
private static ConfigurableApplicationContext createWebMvcContext(List<Class<?>> classes) {
111+
static ConfigurableApplicationContext createWebMvcContext(List<Class<?>> classes) {
112112
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext();
113113
classes.add(WebMvcEndpointConfiguration.class);
114114
context.register(ClassUtils.toClassArray(classes));
115115
context.refresh();
116116
return context;
117117
}
118118

119-
private static ConfigurableApplicationContext createWebFluxContext(List<Class<?>> classes) {
119+
static ConfigurableApplicationContext createWebFluxContext(List<Class<?>> classes) {
120120
AnnotationConfigReactiveWebServerApplicationContext context = new AnnotationConfigReactiveWebServerApplicationContext();
121121
classes.add(WebFluxEndpointConfiguration.class);
122122
context.register(ClassUtils.toClassArray(classes));

0 commit comments

Comments
 (0)