-
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.
Integration test Bean Override support for multiple candidate beans
This commit introduces integration tests which verify that Bean Overrides (for example, @MockitoBean and @MockitoSpyBean) can select a single candidate bean to override when there are multiple candidates that match the required type. To "select" the desired bean, these tests rely on one of the following. - explicit bean name in the bean override annotation - explicit @Qualifier on the bean override field - explicit @Primary on one of the candidate beans However, the @Primary tests are currently @Disabled until @Primary is honored in the Spring TestContext Framework. See gh-33742
- Loading branch information
Showing
6 changed files
with
532 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
.../integration/MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.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,88 @@ | ||
/* | ||
* 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.integration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockingDetails; | ||
import org.mockito.mock.MockCreationSettings; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.bean.override.example.ExampleGenericServiceCaller; | ||
import org.springframework.test.context.bean.override.example.IntegerExampleGenericService; | ||
import org.springframework.test.context.bean.override.example.StringExampleGenericService; | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.Mockito.mockingDetails; | ||
|
||
/** | ||
* Tests that {@link MockitoBean @MockitoBean} can be used to mock a bean when | ||
* there are multiple candidates and an explicit bean name is supplied to select | ||
* one of the candidates. | ||
* | ||
* @author Sam Brannen | ||
* @author Phillip Webb | ||
* @since 6.2 | ||
* @see MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests | ||
* @see MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests | ||
*/ | ||
@SpringJUnitConfig | ||
class MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests { | ||
|
||
@MockitoBean("stringService") | ||
StringExampleGenericService mock; | ||
|
||
@Autowired | ||
ExampleGenericServiceCaller caller; | ||
|
||
|
||
@Test | ||
void test() { | ||
MockingDetails mockingDetails = mockingDetails(mock); | ||
MockCreationSettings<?> mockSettings = mockingDetails.getMockCreationSettings(); | ||
assertThat(mockingDetails.isMock()).as("is mock").isTrue(); | ||
assertThat(mockSettings.getMockName()).as("mock name").hasToString("stringService"); | ||
|
||
given(mock.greeting()).willReturn("mocked"); | ||
assertThat(caller.sayGreeting()).isEqualTo("I say mocked 123"); | ||
then(mock).should().greeting(); | ||
} | ||
|
||
|
||
@Configuration(proxyBeanMethods = false) | ||
@Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class }) | ||
static class Config { | ||
|
||
@Bean | ||
StringExampleGenericService one() { | ||
return new StringExampleGenericService("one"); | ||
} | ||
|
||
@Bean | ||
// "stringService" matches the constructor argument name in ExampleGenericServiceCaller | ||
StringExampleGenericService stringService() { | ||
return new StringExampleGenericService("two"); | ||
} | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
...integration/MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.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,90 @@ | ||
/* | ||
* 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.integration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockingDetails; | ||
import org.mockito.mock.MockCreationSettings; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.bean.override.example.ExampleGenericServiceCaller; | ||
import org.springframework.test.context.bean.override.example.IntegerExampleGenericService; | ||
import org.springframework.test.context.bean.override.example.StringExampleGenericService; | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.Mockito.mockingDetails; | ||
|
||
/** | ||
* Tests that {@link MockitoBean @MockitoBean} can be used to mock a bean when | ||
* there are multiple candidates and a {@link Qualifier @Qualifier} is supplied | ||
* to select one of the candidates. | ||
* | ||
* @author Sam Brannen | ||
* @author Phillip Webb | ||
* @since 6.2 | ||
* @see MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests | ||
* @see MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests | ||
*/ | ||
@SpringJUnitConfig | ||
class MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests { | ||
|
||
@Qualifier("stringService") | ||
@MockitoBean | ||
StringExampleGenericService mock; | ||
|
||
@Autowired | ||
ExampleGenericServiceCaller caller; | ||
|
||
|
||
@Test | ||
void test() { | ||
MockingDetails mockingDetails = mockingDetails(mock); | ||
MockCreationSettings<?> mockSettings = mockingDetails.getMockCreationSettings(); | ||
assertThat(mockingDetails.isMock()).as("is mock").isTrue(); | ||
assertThat(mockSettings.getMockName()).as("mock name").hasToString("stringService"); | ||
|
||
given(mock.greeting()).willReturn("mocked"); | ||
assertThat(caller.sayGreeting()).isEqualTo("I say mocked 123"); | ||
then(mock).should().greeting(); | ||
} | ||
|
||
|
||
@Configuration(proxyBeanMethods = false) | ||
@Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class }) | ||
static class Config { | ||
|
||
@Bean | ||
StringExampleGenericService one() { | ||
return new StringExampleGenericService("one"); | ||
} | ||
|
||
@Bean | ||
// "stringService" matches the constructor argument name in ExampleGenericServiceCaller | ||
StringExampleGenericService stringService() { | ||
return new StringExampleGenericService("two"); | ||
} | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...ockito/integration/MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.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,91 @@ | ||
/* | ||
* 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.integration; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.MockingDetails; | ||
import org.mockito.mock.MockCreationSettings; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.test.context.bean.override.example.ExampleGenericServiceCaller; | ||
import org.springframework.test.context.bean.override.example.IntegerExampleGenericService; | ||
import org.springframework.test.context.bean.override.example.StringExampleGenericService; | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.Mockito.mockingDetails; | ||
|
||
/** | ||
* Tests that {@link MockitoBean @MockitoBean} can be used to mock a bean when | ||
* there are multiple candidates and one is primary. | ||
* | ||
* @author Sam Brannen | ||
* @author Phillip Webb | ||
* @since 6.2 | ||
* @see MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests | ||
* @see MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests | ||
*/ | ||
@Disabled("Disabled until @Primary is supported for BeanOverrideStrategy.REPLACE_OR_CREATE") | ||
@ExtendWith(SpringExtension.class) | ||
class MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests { | ||
|
||
@MockitoBean | ||
StringExampleGenericService mock; | ||
|
||
@Autowired | ||
ExampleGenericServiceCaller caller; | ||
|
||
|
||
@Test | ||
void test() { | ||
MockingDetails mockingDetails = mockingDetails(mock); | ||
MockCreationSettings<?> mockSettings = mockingDetails.getMockCreationSettings(); | ||
assertThat(mockingDetails.isMock()).as("is mock").isTrue(); | ||
assertThat(mockSettings.getMockName()).as("mock name").hasToString("two"); | ||
|
||
given(mock.greeting()).willReturn("mocked"); | ||
assertThat(caller.sayGreeting()).isEqualTo("I say mocked 123"); | ||
then(mock).should().greeting(); | ||
} | ||
|
||
|
||
@Configuration(proxyBeanMethods = false) | ||
@Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class }) | ||
static class Config { | ||
|
||
@Bean | ||
StringExampleGenericService one() { | ||
return new StringExampleGenericService("one"); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
StringExampleGenericService two() { | ||
return new StringExampleGenericService("two"); | ||
} | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
...tegration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.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,86 @@ | ||
/* | ||
* 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.integration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockingDetails; | ||
import org.mockito.mock.MockCreationSettings; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.bean.override.example.ExampleGenericServiceCaller; | ||
import org.springframework.test.context.bean.override.example.IntegerExampleGenericService; | ||
import org.springframework.test.context.bean.override.example.StringExampleGenericService; | ||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.Mockito.mockingDetails; | ||
|
||
/** | ||
* Tests that {@link MockitoSpyBean @MockitoSpyBean} can be used to spy on a bean | ||
* when there are multiple candidates and an explicit bean name is supplied to | ||
* select one of the candidates. | ||
* | ||
* @author Sam Brannen | ||
* @author Phillip Webb | ||
* @since 6.2 | ||
* @see MockitoSpyBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests | ||
* @see MockitoSpyBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests | ||
*/ | ||
@SpringJUnitConfig | ||
class MockitoSpyBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests { | ||
|
||
@MockitoSpyBean("stringService") | ||
StringExampleGenericService spy; | ||
|
||
@Autowired | ||
ExampleGenericServiceCaller caller; | ||
|
||
|
||
@Test | ||
void test() { | ||
MockingDetails mockingDetails = mockingDetails(spy); | ||
MockCreationSettings<?> mockSettings = mockingDetails.getMockCreationSettings(); | ||
assertThat(mockingDetails.isSpy()).as("is spy").isTrue(); | ||
assertThat(mockSettings.getMockName()).hasToString("stringService"); | ||
|
||
assertThat(caller.sayGreeting()).isEqualTo("I say two 123"); | ||
then(spy).should().greeting(); | ||
} | ||
|
||
|
||
@Configuration(proxyBeanMethods = false) | ||
@Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class }) | ||
static class Config { | ||
|
||
@Bean | ||
StringExampleGenericService one() { | ||
return new StringExampleGenericService("one"); | ||
} | ||
|
||
@Bean | ||
// "stringService" matches the constructor argument name in ExampleGenericServiceCaller | ||
StringExampleGenericService stringService() { | ||
return new StringExampleGenericService("two"); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.