|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2002-present the original author or authors. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + *      https://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | + | 
|  | 17 | +package org.springframework.test.context.bean.override.convention; | 
|  | 18 | + | 
|  | 19 | +import org.junit.jupiter.api.DisplayName; | 
|  | 20 | +import org.junit.jupiter.api.Nested; | 
|  | 21 | +import org.junit.jupiter.api.Test; | 
|  | 22 | +import org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope; | 
|  | 23 | +import org.junit.platform.testkit.engine.EngineTestKit; | 
|  | 24 | + | 
|  | 25 | +import org.springframework.context.ApplicationContext; | 
|  | 26 | +import org.springframework.context.annotation.Bean; | 
|  | 27 | +import org.springframework.context.annotation.Configuration; | 
|  | 28 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | 
|  | 29 | + | 
|  | 30 | +import static org.assertj.core.api.Assertions.assertThat; | 
|  | 31 | +import static org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope.DEFAULT_SCOPE_PROPERTY_NAME; | 
|  | 32 | +import static org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope.TEST_METHOD; | 
|  | 33 | +import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; | 
|  | 34 | + | 
|  | 35 | + | 
|  | 36 | +/** | 
|  | 37 | + * Integration tests for {@link TestBean} that use by-name lookup with | 
|  | 38 | + * {@link ExtensionContextScope#TEST_METHOD}. | 
|  | 39 | + * | 
|  | 40 | + * @author Simon Baslé | 
|  | 41 | + * @author Sam Brannen | 
|  | 42 | + * @since 6.2.13 | 
|  | 43 | + */ | 
|  | 44 | +public class TestBeanByNameLookupTestMethodScopedExtensionContextIntegrationTests { | 
|  | 45 | + | 
|  | 46 | +	@Test | 
|  | 47 | +	void runTests() { | 
|  | 48 | +		EngineTestKit.engine("junit-jupiter") | 
|  | 49 | +				.configurationParameter(DEFAULT_SCOPE_PROPERTY_NAME, TEST_METHOD.name()) | 
|  | 50 | +				.selectors(selectClass(TestCase.class)) | 
|  | 51 | +				.execute() | 
|  | 52 | +				.testEvents() | 
|  | 53 | +				.assertStatistics(stats -> stats.started(12).succeeded(12).failed(0)); | 
|  | 54 | +	} | 
|  | 55 | + | 
|  | 56 | + | 
|  | 57 | +	@SpringJUnitConfig | 
|  | 58 | +	public static class TestCase { | 
|  | 59 | + | 
|  | 60 | +		@TestBean(name = "field") | 
|  | 61 | +		String field; | 
|  | 62 | + | 
|  | 63 | +		@TestBean(name = "methodRenamed1", methodName = "field") | 
|  | 64 | +		String methodRenamed1; | 
|  | 65 | + | 
|  | 66 | +		static String field() { | 
|  | 67 | +			return "fieldOverride"; | 
|  | 68 | +		} | 
|  | 69 | + | 
|  | 70 | +		static String nestedField() { | 
|  | 71 | +			return "nestedFieldOverride"; | 
|  | 72 | +		} | 
|  | 73 | + | 
|  | 74 | +		@Test | 
|  | 75 | +		void fieldHasOverride(ApplicationContext ctx) { | 
|  | 76 | +			assertThat(ctx.getBean("field")).as("applicationContext").isEqualTo("fieldOverride"); | 
|  | 77 | +			assertThat(field).as("injection point").isEqualTo("fieldOverride"); | 
|  | 78 | +		} | 
|  | 79 | + | 
|  | 80 | +		@Test | 
|  | 81 | +		void fieldWithMethodNameHasOverride(ApplicationContext ctx) { | 
|  | 82 | +			assertThat(ctx.getBean("methodRenamed1")).as("applicationContext").isEqualTo("fieldOverride"); | 
|  | 83 | +			assertThat(methodRenamed1).as("injection point").isEqualTo("fieldOverride"); | 
|  | 84 | +		} | 
|  | 85 | + | 
|  | 86 | + | 
|  | 87 | +		@Nested | 
|  | 88 | +		@DisplayName("With @TestBean in enclosing class and in @Nested class") | 
|  | 89 | +		public class TestBeanFieldInEnclosingClassTestCase { | 
|  | 90 | + | 
|  | 91 | +			@TestBean(name = "nestedField") | 
|  | 92 | +			String nestedField; | 
|  | 93 | + | 
|  | 94 | +			@TestBean(name = "methodRenamed2", methodName = "nestedField") | 
|  | 95 | +			String methodRenamed2; | 
|  | 96 | + | 
|  | 97 | + | 
|  | 98 | +			@Test | 
|  | 99 | +			void fieldHasOverride(ApplicationContext ctx) { | 
|  | 100 | +				assertThat(ctx.getBean("field")).as("applicationContext").isEqualTo("fieldOverride"); | 
|  | 101 | +				assertThat(field).as("injection point").isEqualTo("fieldOverride"); | 
|  | 102 | +			} | 
|  | 103 | + | 
|  | 104 | +			@Test | 
|  | 105 | +			void fieldWithMethodNameHasOverride(ApplicationContext ctx) { | 
|  | 106 | +				assertThat(ctx.getBean("methodRenamed1")).as("applicationContext").isEqualTo("fieldOverride"); | 
|  | 107 | +				assertThat(methodRenamed1).as("injection point").isEqualTo("fieldOverride"); | 
|  | 108 | +			} | 
|  | 109 | + | 
|  | 110 | +			@Test | 
|  | 111 | +			void nestedFieldHasOverride(ApplicationContext ctx) { | 
|  | 112 | +				assertThat(ctx.getBean("nestedField")).as("applicationContext").isEqualTo("nestedFieldOverride"); | 
|  | 113 | +				assertThat(nestedField).isEqualTo("nestedFieldOverride"); | 
|  | 114 | +			} | 
|  | 115 | + | 
|  | 116 | +			@Test | 
|  | 117 | +			void nestedFieldWithMethodNameHasOverride(ApplicationContext ctx) { | 
|  | 118 | +				assertThat(ctx.getBean("methodRenamed2")).as("applicationContext").isEqualTo("nestedFieldOverride"); | 
|  | 119 | +				assertThat(methodRenamed2).isEqualTo("nestedFieldOverride"); | 
|  | 120 | +			} | 
|  | 121 | + | 
|  | 122 | +			@Nested | 
|  | 123 | +			@DisplayName("With @TestBean in the enclosing classes") | 
|  | 124 | +			public class TestBeanFieldInEnclosingClassLevel2TestCase { | 
|  | 125 | + | 
|  | 126 | +				@Test | 
|  | 127 | +				void fieldHasOverride(ApplicationContext ctx) { | 
|  | 128 | +					assertThat(ctx.getBean("field")).as("applicationContext").isEqualTo("fieldOverride"); | 
|  | 129 | +					assertThat(field).as("injection point").isEqualTo("fieldOverride"); | 
|  | 130 | +				} | 
|  | 131 | + | 
|  | 132 | +				@Test | 
|  | 133 | +				void fieldWithMethodNameHasOverride(ApplicationContext ctx) { | 
|  | 134 | +					assertThat(ctx.getBean("methodRenamed1")).as("applicationContext").isEqualTo("fieldOverride"); | 
|  | 135 | +					assertThat(methodRenamed1).as("injection point").isEqualTo("fieldOverride"); | 
|  | 136 | +				} | 
|  | 137 | + | 
|  | 138 | +				@Test | 
|  | 139 | +				void nestedFieldHasOverride(ApplicationContext ctx) { | 
|  | 140 | +					assertThat(ctx.getBean("nestedField")).as("applicationContext").isEqualTo("nestedFieldOverride"); | 
|  | 141 | +					assertThat(nestedField).isEqualTo("nestedFieldOverride"); | 
|  | 142 | +				} | 
|  | 143 | + | 
|  | 144 | +				@Test | 
|  | 145 | +				void nestedFieldWithMethodNameHasOverride(ApplicationContext ctx) { | 
|  | 146 | +					assertThat(ctx.getBean("methodRenamed2")).as("applicationContext").isEqualTo("nestedFieldOverride"); | 
|  | 147 | +					assertThat(methodRenamed2).isEqualTo("nestedFieldOverride"); | 
|  | 148 | +				} | 
|  | 149 | +			} | 
|  | 150 | +		} | 
|  | 151 | + | 
|  | 152 | +		@Nested | 
|  | 153 | +		@DisplayName("With factory method in enclosing class") | 
|  | 154 | +		public class TestBeanFactoryMethodInEnclosingClassTestCase { | 
|  | 155 | + | 
|  | 156 | +			@TestBean(methodName = "nestedField", name = "nestedField") | 
|  | 157 | +			String nestedField; | 
|  | 158 | + | 
|  | 159 | +			@Test | 
|  | 160 | +			void nestedFieldHasOverride(ApplicationContext ctx) { | 
|  | 161 | +				assertThat(ctx.getBean("nestedField")).as("applicationContext").isEqualTo("nestedFieldOverride"); | 
|  | 162 | +				assertThat(nestedField).isEqualTo("nestedFieldOverride"); | 
|  | 163 | +			} | 
|  | 164 | + | 
|  | 165 | +			@Nested | 
|  | 166 | +			@DisplayName("With factory method in the enclosing class of the enclosing class") | 
|  | 167 | +			public class TestBeanFactoryMethodInEnclosingClassLevel2TestCase { | 
|  | 168 | + | 
|  | 169 | +				@TestBean(methodName = "nestedField", name = "nestedNestedField") | 
|  | 170 | +				String nestedNestedField; | 
|  | 171 | + | 
|  | 172 | +				@Test | 
|  | 173 | +				void nestedFieldHasOverride(ApplicationContext ctx) { | 
|  | 174 | +					assertThat(ctx.getBean("nestedNestedField")).as("applicationContext").isEqualTo("nestedFieldOverride"); | 
|  | 175 | +					assertThat(nestedNestedField).isEqualTo("nestedFieldOverride"); | 
|  | 176 | +				} | 
|  | 177 | +			} | 
|  | 178 | +		} | 
|  | 179 | +	} | 
|  | 180 | + | 
|  | 181 | +	@Configuration(proxyBeanMethods = false) | 
|  | 182 | +	static class Config { | 
|  | 183 | + | 
|  | 184 | +		@Bean("field") | 
|  | 185 | +		String bean1() { | 
|  | 186 | +			return "prod"; | 
|  | 187 | +		} | 
|  | 188 | + | 
|  | 189 | +		@Bean("nestedField") | 
|  | 190 | +		String bean2() { | 
|  | 191 | +			return "nestedProd"; | 
|  | 192 | +		} | 
|  | 193 | + | 
|  | 194 | +		@Bean("methodRenamed1") | 
|  | 195 | +		String bean3() { | 
|  | 196 | +			return "Prod"; | 
|  | 197 | +		} | 
|  | 198 | + | 
|  | 199 | +		@Bean("methodRenamed2") | 
|  | 200 | +		String bean4() { | 
|  | 201 | +			return "NestedProd"; | 
|  | 202 | +		} | 
|  | 203 | +	} | 
|  | 204 | + | 
|  | 205 | +} | 
0 commit comments