Skip to content

Commit 92bbaa2

Browse files
committed
Improve test coverage for @⁠MockitoSpyBean use cases
1 parent 837579c commit 92bbaa2

File tree

4 files changed

+186
-23
lines changed

4 files changed

+186
-23
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* @author Sam Brannen
3434
* @since 6.2.1
3535
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34025">gh-34025</a>
36+
* @see MockitoSpyBeanDuplicateTypeIntegrationTests
37+
* @see MockitoSpyBeanDuplicateTypeAndNameIntegrationTests
3638
*/
3739
@SpringJUnitConfig
3840
public class MockitoBeanDuplicateTypeIntegrationTests {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.MockingDetails;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.test.context.bean.override.example.ExampleService;
28+
import org.springframework.test.context.bean.override.example.RealExampleService;
29+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Mockito.mockingDetails;
33+
34+
/**
35+
* Integration tests for duplicate {@link MockitoSpyBean @MockitoSpyBean}
36+
* declarations for the same target bean, selected by-name.
37+
*
38+
* @author Sam Brannen
39+
* @since 6.2.1
40+
* @see MockitoBeanDuplicateTypeIntegrationTests
41+
* @see MockitoSpyBeanDuplicateTypeIntegrationTests
42+
*/
43+
@SpringJUnitConfig
44+
public class MockitoSpyBeanDuplicateTypeAndNameIntegrationTests {
45+
46+
@MockitoSpyBean("exampleService1")
47+
ExampleService service1;
48+
49+
@MockitoSpyBean("exampleService1")
50+
ExampleService service2;
51+
52+
@Autowired
53+
ExampleService exampleService2;
54+
55+
@Autowired
56+
List<ExampleService> services;
57+
58+
59+
@Test
60+
void duplicateMocksShouldHaveBeenCreated() {
61+
assertThat(service1).isSameAs(service2);
62+
assertThat(services).containsExactly(service1, exampleService2);
63+
64+
MockingDetails mockingDetails1 = mockingDetails(service1);
65+
MockingDetails mockingDetails2 = mockingDetails(exampleService2);
66+
assertThat(mockingDetails1.isSpy()).as("isSpy(service1)").isTrue();
67+
assertThat(mockingDetails2.isSpy()).as("isSpy(exampleService2)").isFalse();
68+
}
69+
70+
71+
@Configuration(proxyBeanMethods = false)
72+
static class Config {
73+
74+
@Bean
75+
ExampleService exampleService1() {
76+
return new RealExampleService("@Bean 1");
77+
}
78+
79+
@Bean
80+
ExampleService exampleService2() {
81+
return new RealExampleService("@Bean 2");
82+
}
83+
}
84+
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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;
18+
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.MockingDetails;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.test.context.bean.override.example.ExampleService;
28+
import org.springframework.test.context.bean.override.example.RealExampleService;
29+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Mockito.mockingDetails;
33+
34+
/**
35+
* Integration tests for duplicate {@link MockitoSpyBean @MockitoSpyBean}
36+
* declarations for the same target bean, selected by-type.
37+
*
38+
* @author Sam Brannen
39+
* @since 6.2.1
40+
* @see MockitoBeanDuplicateTypeIntegrationTests
41+
* @see MockitoSpyBeanDuplicateTypeAndNameIntegrationTests
42+
*/
43+
@SpringJUnitConfig
44+
public class MockitoSpyBeanDuplicateTypeIntegrationTests {
45+
46+
@MockitoSpyBean
47+
ExampleService service1;
48+
49+
@MockitoSpyBean
50+
ExampleService service2;
51+
52+
@Autowired
53+
List<ExampleService> services;
54+
55+
56+
@Test
57+
void test() {
58+
assertThat(service1).isSameAs(service2);
59+
assertThat(services).containsExactly(service1);
60+
61+
MockingDetails mockingDetails = mockingDetails(service1);
62+
assertThat(mockingDetails.isSpy()).as("isSpy(field1)").isTrue();
63+
}
64+
65+
66+
@Configuration(proxyBeanMethods = false)
67+
static class Config {
68+
69+
@Bean
70+
ExampleService exampleService() {
71+
return new RealExampleService("@Bean");
72+
}
73+
}
74+
75+
}

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

+24-23
Original file line numberDiff line numberDiff line change
@@ -35,80 +35,81 @@
3535
* Integration tests for {@link MockitoSpyBean} that use by-name lookup.
3636
*
3737
* @author Simon Baslé
38+
* @author Sam Brannen
3839
* @since 6.2
3940
*/
4041
@SpringJUnitConfig(Config.class)
4142
public class MockitoSpyBeanForByNameLookupIntegrationTests {
4243

43-
@MockitoSpyBean("field")
44+
@MockitoSpyBean("field1")
4445
ExampleService field;
4546

46-
@MockitoSpyBean("nestedField")
47-
ExampleService nestedField;
48-
49-
@MockitoSpyBean("field")
47+
@MockitoSpyBean("field1")
5048
ExampleService renamed1;
5149

52-
@MockitoSpyBean("nestedField")
53-
ExampleService renamed2;
54-
5550

5651
@Test
5752
void fieldHasOverride(ApplicationContext ctx) {
58-
assertThat(ctx.getBean("field"))
53+
assertThat(ctx.getBean("field1"))
5954
.isInstanceOf(ExampleService.class)
6055
.satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue())
61-
.isSameAs(this.field);
56+
.isSameAs(field);
6257

63-
assertThat(this.field.greeting()).isEqualTo("Hello Field");
58+
assertThat(field.greeting()).isEqualTo("bean1");
6459
}
6560

6661
@Test
6762
void renamedFieldHasOverride(ApplicationContext ctx) {
68-
assertThat(ctx.getBean("field"))
63+
assertThat(ctx.getBean("field1"))
6964
.isInstanceOf(ExampleService.class)
7065
.satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue())
71-
.isSameAs(this.renamed1);
66+
.isSameAs(renamed1);
7267

73-
assertThat(this.renamed1.greeting()).isEqualTo("Hello Field");
68+
assertThat(renamed1.greeting()).isEqualTo("bean1");
7469
}
7570

7671
@Nested
77-
@DisplayName("With @MockitoSpyBean in enclosing class")
72+
@DisplayName("With @MockitoSpyBean in enclosing class and in @Nested class")
7873
public class MockitoSpyBeanNestedTests {
7974

75+
@MockitoSpyBean("field2")
76+
ExampleService nestedField;
77+
78+
@MockitoSpyBean("field2")
79+
ExampleService renamed2;
80+
8081
@Test
8182
void fieldHasOverride(ApplicationContext ctx) {
82-
assertThat(ctx.getBean("nestedField"))
83+
assertThat(ctx.getBean("field2"))
8384
.isInstanceOf(ExampleService.class)
8485
.satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue())
8586
.isSameAs(nestedField);
8687

87-
assertThat(nestedField.greeting()).isEqualTo("Hello Nested Field");
88+
assertThat(nestedField.greeting()).isEqualTo("bean2");
8889
}
8990

9091
@Test
9192
void renamedFieldHasOverride(ApplicationContext ctx) {
92-
assertThat(ctx.getBean("nestedField"))
93+
assertThat(ctx.getBean("field2"))
9394
.isInstanceOf(ExampleService.class)
9495
.satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue())
9596
.isSameAs(renamed2);
9697

97-
assertThat(renamed2.greeting()).isEqualTo("Hello Nested Field");
98+
assertThat(renamed2.greeting()).isEqualTo("bean2");
9899
}
99100
}
100101

101102
@Configuration(proxyBeanMethods = false)
102103
static class Config {
103104

104-
@Bean("field")
105+
@Bean("field1")
105106
ExampleService bean1() {
106-
return new RealExampleService("Hello Field");
107+
return new RealExampleService("bean1");
107108
}
108109

109-
@Bean("nestedField")
110+
@Bean("field2")
110111
ExampleService bean2() {
111-
return new RealExampleService("Hello Nested Field");
112+
return new RealExampleService("bean2");
112113
}
113114
}
114115

0 commit comments

Comments
 (0)