From 87127ab6849af80458187537add4241c3f6e6707 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 16 Oct 2023 12:20:16 +0200 Subject: [PATCH] Polishing --- .../basic/DisabledInAotProcessingTests.java | 2 +- .../DisabledInAotRuntimeClassLevelTests.java | 2 +- .../DisabledInAotRuntimeMethodLevelTests.java | 2 +- .../context/hierarchies/web/EarTests.java | 9 ++--- .../servlet/samples/spr/EncodedUriTests.java | 33 +++++++------------ .../servlet/samples/spr/HttpOptionsTests.java | 30 +++++++---------- 6 files changed, 28 insertions(+), 50 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java index dd982bd25f..80969737f2 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java @@ -47,7 +47,7 @@ public class DisabledInAotProcessingTests { assertThat(enigma).isEqualTo("puzzle"); } - @Configuration + @Configuration(proxyBeanMethods = false) static class Config { @Bean diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeClassLevelTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeClassLevelTests.java index 82f468bc92..783b1fbf05 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeClassLevelTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeClassLevelTests.java @@ -61,7 +61,7 @@ public class DisabledInAotRuntimeClassLevelTests { } @Configuration(proxyBeanMethods = false) - static class BasicTestConfiguration { + static class Config { @Bean MessageService defaultMessageService() { diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests.java index d0937b48ac..e12707b637 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotRuntimeMethodLevelTests.java @@ -63,7 +63,7 @@ public class DisabledInAotRuntimeMethodLevelTests { } @Configuration(proxyBeanMethods = false) - static class BasicTestConfiguration { + static class Config { @Bean MessageService defaultMessageService() { diff --git a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/EarTests.java b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/EarTests.java index e20d58320b..00aac81571 100644 --- a/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/EarTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/hierarchies/web/EarTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -17,14 +17,12 @@ package org.springframework.test.context.hierarchies.web; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.web.context.WebApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @@ -33,8 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Sam Brannen * @since 3.2.2 */ -@ExtendWith(SpringExtension.class) -@ContextConfiguration +@SpringJUnitConfig class EarTests { @Configuration diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/EncodedUriTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/EncodedUriTests.java index 91831d325a..39560381e5 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/EncodedUriTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/EncodedUriTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -16,24 +16,18 @@ package org.springframework.test.web.servlet.samples.spr; - import java.net.URI; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.BeansException; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.PriorityOrdered; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.ui.Model; @@ -59,23 +53,18 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC * * @author Sebastien Deleuze */ -@ExtendWith(SpringExtension.class) -@WebAppConfiguration -@ContextConfiguration +@SpringJUnitWebConfig public class EncodedUriTests { - @Autowired - private WebApplicationContext wac; - - private MockMvc mockMvc; + private final MockMvc mockMvc; - @BeforeEach - public void setup() { - this.mockMvc = webAppContextSetup(this.wac).build(); + EncodedUriTests(WebApplicationContext wac) { + this.mockMvc = webAppContextSetup(wac).build(); } + @Test - public void test() throws Exception { + void test() throws Exception { String id = "a/b"; URI url = UriComponentsBuilder.fromUriString("/circuit").pathSegment(id).build().encode().toUri(); ResultActions result = mockMvc.perform(get(url)); @@ -88,12 +77,12 @@ public class EncodedUriTests { static class WebConfig implements WebMvcConfigurer { @Bean - public MyController myController() { + MyController myController() { return new MyController(); } @Bean - public HandlerMappingConfigurer myHandlerMappingConfigurer() { + HandlerMappingConfigurer myHandlerMappingConfigurer() { return new HandlerMappingConfigurer(); } @@ -107,7 +96,7 @@ public class EncodedUriTests { private static class MyController { @RequestMapping(value = "/circuit/{id}", method = RequestMethod.GET) - public String getCircuit(@PathVariable String id, Model model) { + String getCircuit(@PathVariable String id, Model model) { model.addAttribute("receivedId", id); return "result"; } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/HttpOptionsTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/HttpOptionsTests.java index 97f8f5a457..175d11e1f4 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/HttpOptionsTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/HttpOptionsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 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. @@ -18,17 +18,12 @@ package org.springframework.test.web.servlet.samples.spr; import java.util.concurrent.atomic.AtomicInteger; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -47,24 +42,21 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC * * @author Arnaud Cogoluègnes */ -@ExtendWith(SpringExtension.class) -@WebAppConfiguration -@ContextConfiguration +@SpringJUnitWebConfig public class HttpOptionsTests { - @Autowired - private WebApplicationContext wac; + private final WebApplicationContext wac; - private MockMvc mockMvc; + private final MockMvc mockMvc; - - @BeforeEach - public void setup() { + HttpOptionsTests(WebApplicationContext wac) { + this.wac = wac; this.mockMvc = webAppContextSetup(this.wac).dispatchOptions(true).build(); } + @Test - public void test() throws Exception { + void test() throws Exception { MyController controller = this.wac.getBean(MyController.class); int initialCount = controller.counter.get(); this.mockMvc.perform(options("/myUrl")).andExpect(status().isOk()); @@ -78,7 +70,7 @@ public class HttpOptionsTests { static class WebConfig implements WebMvcConfigurer { @Bean - public MyController myController() { + MyController myController() { return new MyController(); } } @@ -91,7 +83,7 @@ public class HttpOptionsTests { @RequestMapping(value = "/myUrl", method = RequestMethod.OPTIONS) @ResponseBody - public void handle() { + void handle() { counter.incrementAndGet(); } }