-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify @MockitoBean & @MockitoSpyBean can be used with @ContextHie…
…rarchy See gh-33742
- Loading branch information
Showing
3 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...est/java/org/springframework/test/context/bean/override/example/ExampleServiceCaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.bean.override.example; | ||
|
||
/** | ||
* Example bean for mocking tests that call the {@link ExampleService}. | ||
* | ||
* @author Phillip Webb | ||
* @since 6.2 | ||
*/ | ||
public class ExampleServiceCaller { | ||
|
||
private final ExampleService service; | ||
|
||
public ExampleServiceCaller(ExampleService service) { | ||
this.service = service; | ||
} | ||
|
||
public ExampleService getService() { | ||
return this.service; | ||
} | ||
|
||
public String sayGreeting() { | ||
return "I say " + this.service.greeting(); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...amework/test/context/bean/override/mockito/MockitoBeanAndContextHierarchyParentTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.bean.override.mockito; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.test.context.ContextHierarchy; | ||
import org.springframework.test.context.bean.override.example.ExampleService; | ||
import org.springframework.test.context.bean.override.example.ExampleServiceCaller; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
/** | ||
* Tests which verify that {@link MockitoBean @MockitoBean} can be used within a | ||
* {@link ContextHierarchy @ContextHierarchy}. | ||
* | ||
* @author Sam Brannen | ||
* @author Phillip Webb | ||
* @since 6.2 | ||
* @see MockitoSpyBeanAndContextHierarchyChildTests | ||
*/ | ||
@SpringJUnitConfig | ||
public class MockitoBeanAndContextHierarchyParentTests { | ||
|
||
@MockitoBean | ||
ExampleService service; | ||
|
||
@Autowired | ||
ApplicationContext context; | ||
|
||
@BeforeEach | ||
void configureServiceMock() { | ||
given(service.greeting()).willReturn("mock"); | ||
} | ||
|
||
@Test | ||
void test() { | ||
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1); | ||
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).isEmpty(); | ||
|
||
assertThat(service.greeting()).isEqualTo("mock"); | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...ework/test/context/bean/override/mockito/MockitoSpyBeanAndContextHierarchyChildTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2012-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.bean.override.mockito; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.ContextHierarchy; | ||
import org.springframework.test.context.aot.DisabledInAotMode; | ||
import org.springframework.test.context.bean.override.example.ExampleService; | ||
import org.springframework.test.context.bean.override.example.ExampleServiceCaller; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests which verify that {@link MockitoBean @MockitoBean} and | ||
* {@link MockitoSpyBean @MockitoSpyBean} can be used within a | ||
* {@link ContextHierarchy @ContextHierarchy}. | ||
* | ||
* @author Sam Brannen | ||
* @author Phillip Webb | ||
* @since 6.2 | ||
* @see MockitoBeanAndContextHierarchyParentTests | ||
*/ | ||
@ContextHierarchy(@ContextConfiguration) | ||
@DisabledInAotMode // @ContextHierarchy is not supported in AOT. | ||
public class MockitoSpyBeanAndContextHierarchyChildTests extends MockitoBeanAndContextHierarchyParentTests { | ||
|
||
@MockitoSpyBean | ||
ExampleServiceCaller serviceCaller; | ||
|
||
@Autowired | ||
ApplicationContext context; | ||
|
||
|
||
@Test | ||
@Override | ||
void test() { | ||
assertThat(context).as("child ApplicationContext").isNotNull(); | ||
assertThat(context.getParent()).as("parent ApplicationContext").isNotNull(); | ||
assertThat(context.getParent().getParent()).as("grandparent ApplicationContext").isNull(); | ||
|
||
ApplicationContext parentContext = context.getParent(); | ||
assertThat(parentContext.getBeanNamesForType(ExampleService.class)).hasSize(1); | ||
assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class)).isEmpty(); | ||
|
||
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1); | ||
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1); | ||
|
||
assertThat(service.greeting()).isEqualTo("mock"); | ||
assertThat(serviceCaller.sayGreeting()).isEqualTo("I say mock"); | ||
} | ||
|
||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class ChildConfig { | ||
|
||
@Bean | ||
ExampleServiceCaller serviceCaller(ExampleService service) { | ||
return new ExampleServiceCaller(service); | ||
} | ||
} | ||
|
||
} |