Skip to content

Commit c0ad9c0

Browse files
committed
Verify @⁠MockitoBean & @⁠MockitoSpyBean can be used with @⁠ContextHierarchy
See gh-33742
1 parent 2aa3f40 commit c0ad9c0

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.example;
18+
19+
/**
20+
* Example bean for mocking tests that call the {@link ExampleService}.
21+
*
22+
* @author Phillip Webb
23+
* @since 6.2
24+
*/
25+
public class ExampleServiceCaller {
26+
27+
private final ExampleService service;
28+
29+
public ExampleServiceCaller(ExampleService service) {
30+
this.service = service;
31+
}
32+
33+
public ExampleService getService() {
34+
return this.service;
35+
}
36+
37+
public String sayGreeting() {
38+
return "I say " + this.service.greeting();
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.test.context.ContextHierarchy;
25+
import org.springframework.test.context.bean.override.example.ExampleService;
26+
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
27+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.BDDMockito.given;
31+
32+
/**
33+
* Tests which verify that {@link MockitoBean @MockitoBean} can be used within a
34+
* {@link ContextHierarchy @ContextHierarchy}.
35+
*
36+
* @author Sam Brannen
37+
* @author Phillip Webb
38+
* @since 6.2
39+
* @see MockitoSpyBeanAndContextHierarchyChildTests
40+
*/
41+
@SpringJUnitConfig
42+
public class MockitoBeanAndContextHierarchyParentTests {
43+
44+
@MockitoBean
45+
ExampleService service;
46+
47+
@Autowired
48+
ApplicationContext context;
49+
50+
@BeforeEach
51+
void configureServiceMock() {
52+
given(service.greeting()).willReturn("mock");
53+
}
54+
55+
@Test
56+
void test() {
57+
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1);
58+
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).isEmpty();
59+
60+
assertThat(service.greeting()).isEqualTo("mock");
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.test.context.ContextConfiguration;
26+
import org.springframework.test.context.ContextHierarchy;
27+
import org.springframework.test.context.aot.DisabledInAotMode;
28+
import org.springframework.test.context.bean.override.example.ExampleService;
29+
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests which verify that {@link MockitoBean @MockitoBean} and
35+
* {@link MockitoSpyBean @MockitoSpyBean} can be used within a
36+
* {@link ContextHierarchy @ContextHierarchy}.
37+
*
38+
* @author Sam Brannen
39+
* @author Phillip Webb
40+
* @since 6.2
41+
* @see MockitoBeanAndContextHierarchyParentTests
42+
*/
43+
@ContextHierarchy(@ContextConfiguration)
44+
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
45+
public class MockitoSpyBeanAndContextHierarchyChildTests extends MockitoBeanAndContextHierarchyParentTests {
46+
47+
@MockitoSpyBean
48+
ExampleServiceCaller serviceCaller;
49+
50+
@Autowired
51+
ApplicationContext context;
52+
53+
54+
@Test
55+
@Override
56+
void test() {
57+
assertThat(context).as("child ApplicationContext").isNotNull();
58+
assertThat(context.getParent()).as("parent ApplicationContext").isNotNull();
59+
assertThat(context.getParent().getParent()).as("grandparent ApplicationContext").isNull();
60+
61+
ApplicationContext parentContext = context.getParent();
62+
assertThat(parentContext.getBeanNamesForType(ExampleService.class)).hasSize(1);
63+
assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class)).isEmpty();
64+
65+
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1);
66+
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
67+
68+
assertThat(service.greeting()).isEqualTo("mock");
69+
assertThat(serviceCaller.sayGreeting()).isEqualTo("I say mock");
70+
}
71+
72+
73+
@Configuration(proxyBeanMethods = false)
74+
static class ChildConfig {
75+
76+
@Bean
77+
ExampleServiceCaller serviceCaller(ExampleService service) {
78+
return new ExampleServiceCaller(service);
79+
}
80+
}
81+
82+
}

0 commit comments

Comments
 (0)