Skip to content

Commit ba8024d

Browse files
committed
Verify @⁠MockitoBean can be used with generics and parameterized types
See gh-33742
1 parent efda3f0 commit ba8024d

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.mockito.integration;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
23+
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.Something;
24+
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.Thing;
25+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
26+
27+
/**
28+
* Abstract base class for tests for {@link MockitoBean @MockitoBean} with generics.
29+
*
30+
* @param <T> type of thing
31+
* @param <S> type of something
32+
* @author Madhura Bhave
33+
* @author Sam Brannen
34+
* @since 6.2
35+
* @see MockitoBeanAndGenericsIntegrationTests
36+
*/
37+
@SpringJUnitConfig
38+
abstract class AbstractMockitoBeanAndGenericsIntegrationTests<T extends Thing<S>, S extends Something> {
39+
40+
@Autowired
41+
T thing;
42+
43+
@MockitoBean
44+
S something;
45+
46+
47+
static class Something {
48+
String speak() {
49+
return "Hi";
50+
}
51+
}
52+
53+
static class SomethingImpl extends Something {
54+
}
55+
56+
abstract static class Thing<S extends Something> {
57+
58+
@Autowired
59+
private S something;
60+
61+
S getSomething() {
62+
return this.something;
63+
}
64+
}
65+
66+
static class ThingImpl extends Thing<SomethingImpl> {
67+
}
68+
69+
@Configuration(proxyBeanMethods = false)
70+
static class Config {
71+
72+
@Bean
73+
ThingImpl thing() {
74+
return new ThingImpl();
75+
}
76+
}
77+
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-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.mockito.integration;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.SomethingImpl;
22+
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.ThingImpl;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.mockito.BDDMockito.when;
26+
27+
/**
28+
* Concrete implementation of {@link AbstractMockitoBeanAndGenericsIntegrationTests}.
29+
*
30+
* @author Madhura Bhave
31+
* @author Sam Brannen
32+
* @since 6.2
33+
*/
34+
class MockitoBeanAndGenericsIntegrationTests extends AbstractMockitoBeanAndGenericsIntegrationTests<ThingImpl, SomethingImpl> {
35+
36+
@Test
37+
void mockitoBeanShouldResolveConcreteType() {
38+
assertThat(something).isExactlyInstanceOf(SomethingImpl.class);
39+
40+
when(something.speak()).thenReturn("Hola");
41+
assertThat(thing.getSomething().speak()).isEqualTo("Hola");
42+
}
43+
44+
}

0 commit comments

Comments
 (0)