Skip to content

Commit b9a6ba1

Browse files
committed
Fix package tangle in bean override tests
This commit fixes a package tangle between the root bean override package and its sub-packages. This was vastly improved with the introduction of `@DummyBean` but we still had a few tests that were at the wrong place.
1 parent 96302a7 commit b9a6ba1

File tree

4 files changed

+263
-168
lines changed

4 files changed

+263
-168
lines changed

spring-test/src/test/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizerEqualityTests.java

-168
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2002-2024 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;
18+
19+
import java.util.Collections;
20+
21+
import org.springframework.lang.Nullable;
22+
import org.springframework.test.context.ContextCustomizer;
23+
24+
/**
25+
* Test utilities for {@link BeanOverrideContextCustomizer} that are public so
26+
* that specific bean override implementations can use them.
27+
*
28+
* @author Stephane Nicoll
29+
*/
30+
public abstract class BeanOverrideContextCustomizerTestUtils {
31+
32+
33+
private static final BeanOverrideContextCustomizerFactory factory = new BeanOverrideContextCustomizerFactory();
34+
35+
/**
36+
* Create a {@link ContextCustomizer} for the given {@code testClass}. Return
37+
* a customizer to handle any use of {@link BeanOverride} or {@code null} if
38+
* the test class does not use them.
39+
* @param testClass a test class to introspect
40+
* @return a context customizer for bean override support, or null
41+
*/
42+
@Nullable
43+
public static ContextCustomizer createContextCustomizer(Class<?> testClass) {
44+
return factory.createContextCustomizer(testClass, Collections.emptyList());
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2002-2024 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.Test;
20+
21+
import org.springframework.test.context.ContextCustomizer;
22+
import org.springframework.test.context.bean.override.BeanOverrideContextCustomizerTestUtils;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests that validate the behavior of {@link TestBean} with the TCF context cache.
28+
*
29+
* @author Stephane Nicoll
30+
*/
31+
class TestBeanContextCustomizerEqualityTests {
32+
33+
@Test
34+
void contextCustomizerWithSameOverrideInDifferentTestClassesIsEqual() {
35+
assertThat(createContextCustomizer(Case1.class)).isEqualTo(createContextCustomizer(Case2.class));
36+
}
37+
38+
@Test
39+
void contextCustomizerWithDifferentMethodsIsNotEqual() {
40+
assertThat(createContextCustomizer(Case1.class)).isNotEqualTo(createContextCustomizer(Case3.class));
41+
}
42+
43+
@Test
44+
void contextCustomizerWithByNameVsByTypeLookupIsNotEqual() {
45+
assertThat(createContextCustomizer(Case4.class)).isNotEqualTo(createContextCustomizer(Case5.class));
46+
}
47+
48+
49+
private ContextCustomizer createContextCustomizer(Class<?> testClass) {
50+
ContextCustomizer customizer = BeanOverrideContextCustomizerTestUtils.createContextCustomizer(testClass);
51+
assertThat(customizer).isNotNull();
52+
return customizer;
53+
}
54+
55+
interface DescriptionProvider {
56+
57+
static String createDescription() {
58+
return "override";
59+
}
60+
61+
}
62+
63+
static class Case1 implements DescriptionProvider {
64+
65+
@TestBean(methodName = "createDescription")
66+
private String description;
67+
68+
}
69+
70+
static class Case2 implements DescriptionProvider {
71+
72+
@TestBean(methodName = "createDescription")
73+
private String description;
74+
75+
}
76+
77+
static class Case3 implements DescriptionProvider {
78+
79+
@TestBean(methodName = "createDescription")
80+
private String description;
81+
82+
static String createDescription() {
83+
return "another value";
84+
}
85+
}
86+
87+
static class Case4 {
88+
89+
@TestBean
90+
private String description;
91+
92+
private static String description() {
93+
return "overridden";
94+
}
95+
}
96+
97+
static class Case5 {
98+
99+
@TestBean(name = "descriptionBean")
100+
private String description;
101+
102+
private static String description() {
103+
return "overridden";
104+
}
105+
}
106+
107+
}

0 commit comments

Comments
 (0)