Skip to content

Commit 615cf63

Browse files
committed
Make MockBean resolve right type for abstract test class
Fixes gh-20916
1 parent 58974ab commit 615cf63

File tree

3 files changed

+122
-11
lines changed

3 files changed

+122
-11
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/DefinitionsParser.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,20 @@ class DefinitionsParser {
6060
}
6161

6262
void parse(Class<?> source) {
63-
parseElement(source);
64-
ReflectionUtils.doWithFields(source, this::parseElement);
63+
parseElement(source, null);
64+
ReflectionUtils.doWithFields(source, (element) -> parseElement(element, source));
6565
}
6666

67-
private void parseElement(AnnotatedElement element) {
67+
private void parseElement(AnnotatedElement element, Class<?> source) {
6868
MergedAnnotations annotations = MergedAnnotations.from(element, SearchStrategy.SUPERCLASS);
6969
annotations.stream(MockBean.class).map(MergedAnnotation::synthesize)
70-
.forEach((annotation) -> parseMockBeanAnnotation(annotation, element));
70+
.forEach((annotation) -> parseMockBeanAnnotation(annotation, element, source));
7171
annotations.stream(SpyBean.class).map(MergedAnnotation::synthesize)
72-
.forEach((annotation) -> parseSpyBeanAnnotation(annotation, element));
72+
.forEach((annotation) -> parseSpyBeanAnnotation(annotation, element, source));
7373
}
7474

75-
private void parseMockBeanAnnotation(MockBean annotation, AnnotatedElement element) {
76-
Set<ResolvableType> typesToMock = getOrDeduceTypes(element, annotation.value());
75+
private void parseMockBeanAnnotation(MockBean annotation, AnnotatedElement element, Class<?> source) {
76+
Set<ResolvableType> typesToMock = getOrDeduceTypes(element, annotation.value(), source);
7777
Assert.state(!typesToMock.isEmpty(), () -> "Unable to deduce type to mock from " + element);
7878
if (StringUtils.hasLength(annotation.name())) {
7979
Assert.state(typesToMock.size() == 1, "The name attribute can only be used when mocking a single class");
@@ -86,8 +86,8 @@ private void parseMockBeanAnnotation(MockBean annotation, AnnotatedElement eleme
8686
}
8787
}
8888

89-
private void parseSpyBeanAnnotation(SpyBean annotation, AnnotatedElement element) {
90-
Set<ResolvableType> typesToSpy = getOrDeduceTypes(element, annotation.value());
89+
private void parseSpyBeanAnnotation(SpyBean annotation, AnnotatedElement element, Class<?> source) {
90+
Set<ResolvableType> typesToSpy = getOrDeduceTypes(element, annotation.value(), source);
9191
Assert.state(!typesToSpy.isEmpty(), () -> "Unable to deduce type to spy from " + element);
9292
if (StringUtils.hasLength(annotation.name())) {
9393
Assert.state(typesToSpy.size() == 1, "The name attribute can only be used when spying a single class");
@@ -108,13 +108,13 @@ private void addDefinition(AnnotatedElement element, Definition definition, Stri
108108
}
109109
}
110110

111-
private Set<ResolvableType> getOrDeduceTypes(AnnotatedElement element, Class<?>[] value) {
111+
private Set<ResolvableType> getOrDeduceTypes(AnnotatedElement element, Class<?>[] value, Class<?> source) {
112112
Set<ResolvableType> types = new LinkedHashSet<>();
113113
for (Class<?> clazz : value) {
114114
types.add(ResolvableType.forClass(clazz));
115115
}
116116
if (types.isEmpty() && element instanceof Field) {
117-
types.add(ResolvableType.forField((Field) element));
117+
types.add(ResolvableType.forField((Field) element, source));
118118
}
119119
return types;
120120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2020 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.boot.test.mock.mockito;
18+
19+
/**
20+
* Concrete implementation of {@link AbstractMockBeanOnGenericTests}.
21+
*
22+
* @author Madhura Bhave
23+
*/
24+
class AbstractMockBeanOnGenericExtensionTests extends
25+
AbstractMockBeanOnGenericTests<AbstractMockBeanOnGenericTests.ThingImpl, AbstractMockBeanOnGenericTests.SomethingImpl> {
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2012-2020 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.boot.test.mock.mockito;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link MockBean} with abstract class and generics.
30+
*
31+
* @author Madhura Bhave
32+
*/
33+
@SpringBootTest(classes = AbstractMockBeanOnGenericTests.TestConfiguration.class)
34+
abstract class AbstractMockBeanOnGenericTests<T extends AbstractMockBeanOnGenericTests.Thing<U>, U extends AbstractMockBeanOnGenericTests.Something> {
35+
36+
@Autowired
37+
private T thing;
38+
39+
@MockBean
40+
private U something;
41+
42+
@Test
43+
void mockBeanShouldResolveConcreteType() {
44+
assertThat(this.something).isInstanceOf(SomethingImpl.class);
45+
}
46+
47+
abstract static class Thing<T extends AbstractMockBeanOnGenericTests.Something> {
48+
49+
@Autowired
50+
private T something;
51+
52+
T getSomething() {
53+
return this.something;
54+
}
55+
56+
void setSomething(T something) {
57+
this.something = something;
58+
}
59+
60+
}
61+
62+
static class SomethingImpl extends Something {
63+
64+
}
65+
66+
static class ThingImpl extends Thing<SomethingImpl> {
67+
68+
}
69+
70+
static class Something {
71+
72+
}
73+
74+
@Configuration
75+
static class TestConfiguration {
76+
77+
@Bean
78+
ThingImpl thing() {
79+
return new ThingImpl();
80+
}
81+
82+
}
83+
84+
}

0 commit comments

Comments
 (0)