52 changed files with 2970 additions and 75 deletions
@ -0,0 +1,143 @@
@@ -0,0 +1,143 @@
|
||||
/* |
||||
* Copyright 2002-2025 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; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.junit.platform.testkit.engine.EngineTestKit; |
||||
import org.junit.platform.testkit.engine.Events; |
||||
|
||||
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.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; |
||||
import static org.junit.platform.testkit.engine.EventConditions.event; |
||||
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; |
||||
import static org.junit.platform.testkit.engine.EventConditions.test; |
||||
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; |
||||
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; |
||||
|
||||
/** |
||||
* Integration tests for {@link BeanOverrideTestExecutionListener}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
class BeanOverrideTestExecutionListenerTests { |
||||
|
||||
@Test |
||||
void beanOverrideWithNoMatchingContextName() { |
||||
executeTests(BeanOverrideWithNoMatchingContextNameTestCase.class) |
||||
.assertThatEvents().haveExactly(1, event(test("test"), |
||||
finishedWithFailure( |
||||
instanceOf(IllegalStateException.class), |
||||
message(""" |
||||
Test class BeanOverrideWithNoMatchingContextNameTestCase declares @BeanOverride \ |
||||
fields [message, number], but no BeanOverrideHandler has been registered. \ |
||||
If you are using @ContextHierarchy, ensure that context names for bean overrides match \ |
||||
configured @ContextConfiguration names.""")))); |
||||
} |
||||
|
||||
@Test |
||||
void beanOverrideWithInvalidContextName() { |
||||
executeTests(BeanOverrideWithInvalidContextNameTestCase.class) |
||||
.assertThatEvents().haveExactly(1, event(test("test"), |
||||
finishedWithFailure( |
||||
instanceOf(IllegalStateException.class), |
||||
message(msg -> |
||||
msg.startsWith("No bean override instance found for BeanOverrideHandler") && |
||||
msg.contains("DummyBeanOverrideHandler") && |
||||
msg.contains("BeanOverrideWithInvalidContextNameTestCase.message2") && |
||||
msg.contains("contextName = 'BOGUS'") && |
||||
msg.endsWith(""" |
||||
If you are using @ContextHierarchy, ensure that context names for bean overrides match \ |
||||
configured @ContextConfiguration names."""))))); |
||||
} |
||||
|
||||
|
||||
private static Events executeTests(Class<?> testClass) { |
||||
return EngineTestKit.engine("junit-jupiter") |
||||
.selectors(selectClass(testClass)) |
||||
.execute() |
||||
.testEvents() |
||||
.assertStatistics(stats -> stats.started(1).failed(1)); |
||||
} |
||||
|
||||
|
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
static class BeanOverrideWithNoMatchingContextNameTestCase { |
||||
|
||||
@DummyBean(contextName = "BOGUS") |
||||
String message; |
||||
|
||||
@DummyBean(contextName = "BOGUS") |
||||
Integer number; |
||||
|
||||
@Test |
||||
void test() { |
||||
// no-op
|
||||
} |
||||
} |
||||
|
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
static class BeanOverrideWithInvalidContextNameTestCase { |
||||
|
||||
@DummyBean(contextName = "child") |
||||
String message1; |
||||
|
||||
@DummyBean(contextName = "BOGUS") |
||||
String message2; |
||||
|
||||
@Test |
||||
void test() { |
||||
// no-op
|
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
String message() { |
||||
return "Message 1"; |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
String message() { |
||||
return "Message 2"; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,109 @@
@@ -0,0 +1,109 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.convention.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
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.convention.TestBean; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByNameInChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByNameInChildContextHierarchyTests.Config2; |
||||
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.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Verifies that {@link TestBean @TestBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only overridden "by name" in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class TestBeanByNameInChildContextHierarchyTests { |
||||
|
||||
@TestBean(name = "service", contextName = "child") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
static ExampleService service() { |
||||
return () -> "@TestBean 2"; |
||||
} |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
ExampleService serviceInParent = context.getParent().getBean(ExampleService.class); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("@TestBean 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say @TestBean 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 1"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 2"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.convention.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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.convention.TestBean; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByNameInParentAndChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByNameInParentAndChildContextHierarchyTests.Config2; |
||||
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.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Verifies that {@link TestBean @TestBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* identical beans are overridden "by name" in the parent and in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class TestBeanByNameInParentAndChildContextHierarchyTests { |
||||
|
||||
@TestBean(name = "service", contextName = "parent") |
||||
ExampleService serviceInParent; |
||||
|
||||
@TestBean(name = "service", contextName = "child") |
||||
ExampleService serviceInChild; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
static ExampleService serviceInParent() { |
||||
return () -> "@TestBean 1"; |
||||
} |
||||
|
||||
static ExampleService serviceInChild() { |
||||
return () -> "@TestBean 2"; |
||||
} |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertThat(serviceInParent.greeting()).isEqualTo("@TestBean 1"); |
||||
assertThat(serviceInChild.greeting()).isEqualTo("@TestBean 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(serviceInChild); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say @TestBean 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say @TestBean 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 1"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 2"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,101 @@
@@ -0,0 +1,101 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.convention.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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.convention.TestBean; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByNameInParentContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByNameInParentContextHierarchyTests.Config2; |
||||
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.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Verifies that {@link TestBean @TestBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only overridden "by name" in the parent. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class) |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class TestBeanByNameInParentContextHierarchyTests { |
||||
|
||||
@TestBean(name = "service", contextName = "parent") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
static ExampleService service() { |
||||
return () -> "@TestBean 1"; |
||||
} |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertThat(service.greeting()).isEqualTo("@TestBean 1"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(service); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say @TestBean 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say @TestBean 1"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 1"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,109 @@
@@ -0,0 +1,109 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.convention.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
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.convention.TestBean; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByTypeInChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByTypeInChildContextHierarchyTests.Config2; |
||||
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.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Verifies that {@link TestBean @TestBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only overridden "by type" in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class TestBeanByTypeInChildContextHierarchyTests { |
||||
|
||||
@TestBean(contextName = "child") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
static ExampleService service() { |
||||
return () -> "@TestBean 2"; |
||||
} |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
ExampleService serviceInParent = context.getParent().getBean(ExampleService.class); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("@TestBean 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say @TestBean 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 1"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 2"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.convention.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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.convention.TestBean; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByTypeInParentAndChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByTypeInParentAndChildContextHierarchyTests.Config2; |
||||
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.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Verifies that {@link TestBean @TestBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* identical beans are overridden "by type" in the parent and in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class TestBeanByTypeInParentAndChildContextHierarchyTests { |
||||
|
||||
@TestBean(contextName = "parent") |
||||
ExampleService serviceInParent; |
||||
|
||||
@TestBean(contextName = "child") |
||||
ExampleService serviceInChild; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
static ExampleService serviceInParent() { |
||||
return () -> "@TestBean 1"; |
||||
} |
||||
|
||||
static ExampleService serviceInChild() { |
||||
return () -> "@TestBean 2"; |
||||
} |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertThat(serviceInParent.greeting()).isEqualTo("@TestBean 1"); |
||||
assertThat(serviceInChild.greeting()).isEqualTo("@TestBean 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(serviceInChild); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say @TestBean 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say @TestBean 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 1"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 2"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,101 @@
@@ -0,0 +1,101 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.convention.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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.convention.TestBean; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByTypeInParentContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.convention.hierarchies.TestBeanByTypeInParentContextHierarchyTests.Config2; |
||||
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.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Verifies that {@link TestBean @TestBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only overridden "by type" in the parent. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class) |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class TestBeanByTypeInParentContextHierarchyTests { |
||||
|
||||
@TestBean(contextName = "parent") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
static ExampleService service() { |
||||
return () -> "@TestBean 1"; |
||||
} |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertThat(service.greeting()).isEqualTo("@TestBean 1"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(service); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say @TestBean 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say @TestBean 1"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return () -> "Service 1"; |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
class BarService { |
||||
|
||||
String bar() { |
||||
return "bar"; |
||||
} |
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import jakarta.annotation.PostConstruct; |
||||
|
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
@Configuration |
||||
class ErrorIfContextReloadedConfig { |
||||
|
||||
private static boolean loaded = false; |
||||
|
||||
|
||||
@PostConstruct |
||||
public void postConstruct() { |
||||
if (loaded) { |
||||
throw new RuntimeException("Context loaded multiple times"); |
||||
} |
||||
loaded = true; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
class FooService { |
||||
|
||||
String foo() { |
||||
return "foo"; |
||||
} |
||||
} |
||||
7
spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndContextHierarchyParentIntegrationTests.java → spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/hierarchies/MockitoBeanAndContextHierarchyParentIntegrationTests.java
7
spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndContextHierarchyParentIntegrationTests.java → spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/hierarchies/MockitoBeanAndContextHierarchyParentIntegrationTests.java
@ -0,0 +1,111 @@
@@ -0,0 +1,111 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByNameInChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByNameInChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsNotMock; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoBean @MockitoBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only mocked "by name" in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoBeanByNameInChildContextHierarchyTests { |
||||
|
||||
@MockitoBean(name = "service", contextName = "child") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
ExampleService serviceInParent = context.getParent().getBean(ExampleService.class); |
||||
|
||||
assertIsNotMock(serviceInParent); |
||||
|
||||
when(service.greeting()).thenReturn("Mock 2"); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("Mock 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Mock 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByNameInParentAndChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByNameInParentAndChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoBean @MockitoBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* identical beans are mocked "by name" in the parent and in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoBeanByNameInParentAndChildContextHierarchyTests { |
||||
|
||||
@MockitoBean(name = "service", contextName = "parent") |
||||
ExampleService serviceInParent; |
||||
|
||||
@MockitoBean(name = "service", contextName = "child") |
||||
ExampleService serviceInChild; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
when(serviceInParent.greeting()).thenReturn("Mock 1"); |
||||
when(serviceInChild.greeting()).thenReturn("Mock 2"); |
||||
|
||||
assertThat(serviceInParent.greeting()).isEqualTo("Mock 1"); |
||||
assertThat(serviceInChild.greeting()).isEqualTo("Mock 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(serviceInChild); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Mock 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Mock 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByNameInParentContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByNameInParentContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoBean @MockitoBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only mocked "by name" in the parent. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class) |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoBeanByNameInParentContextHierarchyTests { |
||||
|
||||
@MockitoBean(name = "service", contextName = "parent") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
when(service.greeting()).thenReturn("Mock 1"); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("Mock 1"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(service); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Mock 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Mock 1"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,111 @@
@@ -0,0 +1,111 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByTypeInChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByTypeInChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsNotMock; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoBean @MockitoBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only mocked "by type" in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoBeanByTypeInChildContextHierarchyTests { |
||||
|
||||
@MockitoBean(contextName = "child") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
ExampleService serviceInParent = context.getParent().getBean(ExampleService.class); |
||||
|
||||
assertIsNotMock(serviceInParent); |
||||
|
||||
when(service.greeting()).thenReturn("Mock 2"); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("Mock 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Mock 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByTypeInParentAndChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByTypeInParentAndChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoBean @MockitoBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* identical beans are mocked "by type" in the parent and in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoBeanByTypeInParentAndChildContextHierarchyTests { |
||||
|
||||
@MockitoBean(contextName = "parent") |
||||
ExampleService serviceInParent; |
||||
|
||||
@MockitoBean(contextName = "child") |
||||
ExampleService serviceInChild; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
when(serviceInParent.greeting()).thenReturn("Mock 1"); |
||||
when(serviceInChild.greeting()).thenReturn("Mock 2"); |
||||
|
||||
assertThat(serviceInParent.greeting()).isEqualTo("Mock 1"); |
||||
assertThat(serviceInChild.greeting()).isEqualTo("Mock 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(serviceInChild); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Mock 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Mock 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByTypeInParentContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoBeanByTypeInParentContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoBean @MockitoBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only mocked "by type" in the parent. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class) |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoBeanByTypeInParentContextHierarchyTests { |
||||
|
||||
@MockitoBean(contextName = "parent") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
when(service.greeting()).thenReturn("Mock 1"); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("Mock 1"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(service); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Mock 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Mock 1"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
17
spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanAndContextHierarchyChildIntegrationTests.java → spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/hierarchies/MockitoSpyBeanAndContextHierarchyChildIntegrationTests.java
17
spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanAndContextHierarchyChildIntegrationTests.java → spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/hierarchies/MockitoSpyBeanAndContextHierarchyChildIntegrationTests.java
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByNameInChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByNameInChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsNotSpy; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoSpyBean @MockitoSpyBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only spied on "by name" in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoSpyBeanByNameInChildContextHierarchyTests { |
||||
|
||||
@MockitoSpyBean(name = "service", contextName = "child") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
ExampleService serviceInParent = context.getParent().getBean(ExampleService.class); |
||||
|
||||
assertIsNotSpy(serviceInParent); |
||||
assertIsSpy(service); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("Service 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Service 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByNameInParentAndChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByNameInParentAndChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoSpyBean @MockitoSpyBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* identical beans are spied on "by name" in the parent and in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoSpyBeanByNameInParentAndChildContextHierarchyTests { |
||||
|
||||
@MockitoSpyBean(name = "service", contextName = "parent") |
||||
ExampleService serviceInParent; |
||||
|
||||
@MockitoSpyBean(name = "service", contextName = "child") |
||||
ExampleService serviceInChild; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertIsSpy(serviceInParent); |
||||
assertIsSpy(serviceInChild); |
||||
|
||||
assertThat(serviceInParent.greeting()).isEqualTo("Service 1"); |
||||
assertThat(serviceInChild.greeting()).isEqualTo("Service 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(serviceInChild); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Service 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; |
||||
|
||||
/** |
||||
* This is effectively a one-to-one copy of |
||||
* {@link MockitoSpyBeanByNameInParentAndChildContextHierarchyTests}, except |
||||
* that this test class uses different names for the context hierarchy levels: |
||||
* level-1 and level-2 instead of parent and child. |
||||
* |
||||
* <p>If the context cache is broken, either this test class or |
||||
* {@code MockitoSpyBeanByNameInParentAndChildContextHierarchyTests} will fail |
||||
* when run within the same test suite. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
* @see MockitoSpyBeanByNameInParentAndChildContextHierarchyTests |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = MockitoSpyBeanByNameInParentAndChildContextHierarchyTests.Config1.class, name = "level-1"), |
||||
@ContextConfiguration(classes = MockitoSpyBeanByNameInParentAndChildContextHierarchyTests.Config2.class, name = "level-2") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoSpyBeanByNameInParentAndChildContextHierarchyV2Tests { |
||||
|
||||
@MockitoSpyBean(name = "service", contextName = "level-1") |
||||
ExampleService serviceInParent; |
||||
|
||||
@MockitoSpyBean(name = "service", contextName = "level-2") |
||||
ExampleService serviceInChild; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertIsSpy(serviceInParent); |
||||
assertIsSpy(serviceInChild); |
||||
|
||||
assertThat(serviceInParent.greeting()).isEqualTo("Service 1"); |
||||
assertThat(serviceInChild.greeting()).isEqualTo("Service 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(serviceInChild); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Service 2"); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByNameInParentContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByNameInParentContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoSpyBean @MockitoSpyBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only spied on "by name" in the parent. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class) |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoSpyBeanByNameInParentContextHierarchyTests { |
||||
|
||||
@MockitoSpyBean(name = "service", contextName = "parent") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertIsSpy(service); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("Service 1"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(service); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Service 1"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByTypeInChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByTypeInChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsNotSpy; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoSpyBean @MockitoSpyBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only spied on "by type" in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoSpyBeanByTypeInChildContextHierarchyTests { |
||||
|
||||
@MockitoSpyBean(contextName = "child") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
ExampleService serviceInParent = context.getParent().getBean(ExampleService.class); |
||||
|
||||
assertIsNotSpy(serviceInParent); |
||||
assertIsSpy(service); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("Service 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Service 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByTypeInParentAndChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByTypeInParentAndChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoSpyBean @MockitoSpyBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* identical beans are spied on "by type" in the parent and in the child. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoSpyBeanByTypeInParentAndChildContextHierarchyTests { |
||||
|
||||
@MockitoSpyBean(contextName = "parent") |
||||
ExampleService serviceInParent; |
||||
|
||||
@MockitoSpyBean(contextName = "child") |
||||
ExampleService serviceInChild; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertIsSpy(serviceInParent); |
||||
assertIsSpy(serviceInChild); |
||||
|
||||
assertThat(serviceInParent.greeting()).isEqualTo("Service 1"); |
||||
assertThat(serviceInChild.greeting()).isEqualTo("Service 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(serviceInChild); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Service 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByTypeInParentContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeanByTypeInParentContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoSpyBean @MockitoSpyBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* a bean is only spied on "by type" in the parent. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class) |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class MockitoSpyBeanByTypeInParentContextHierarchyTests { |
||||
|
||||
@MockitoSpyBean(contextName = "parent") |
||||
ExampleService service; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test() { |
||||
assertIsSpy(service); |
||||
|
||||
assertThat(service.greeting()).isEqualTo("Service 1"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(service); |
||||
assertThat(serviceCaller2.getService()).isSameAs(service); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Service 1"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
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 org.springframework.test.context.bean.override.example.RealExampleService; |
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeansByTypeInParentAndChildContextHierarchyTests.Config1; |
||||
import org.springframework.test.context.bean.override.mockito.hierarchies.MockitoSpyBeansByTypeInParentAndChildContextHierarchyTests.Config2; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; |
||||
|
||||
/** |
||||
* Verifies that {@link MockitoSpyBean @MockitoSpyBean} can be used within a |
||||
* {@link ContextHierarchy @ContextHierarchy} with named context levels, when |
||||
* identical beans are spied on "by type" in the parent and in the child and |
||||
* configured via class-level {@code @MockitoSpyBean} declarations. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = Config1.class, name = "parent"), |
||||
@ContextConfiguration(classes = Config2.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
@MockitoSpyBean(types = ExampleService.class, contextName = "parent") |
||||
@MockitoSpyBean(types = ExampleService.class, contextName = "child") |
||||
class MockitoSpyBeansByTypeInParentAndChildContextHierarchyTests { |
||||
|
||||
@Autowired |
||||
ExampleService serviceInChild; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller1; |
||||
|
||||
@Autowired |
||||
ExampleServiceCaller serviceCaller2; |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
ExampleService serviceInParent = context.getParent().getBean(ExampleService.class); |
||||
|
||||
assertIsSpy(serviceInParent); |
||||
assertIsSpy(serviceInChild); |
||||
|
||||
assertThat(serviceInParent.greeting()).isEqualTo("Service 1"); |
||||
assertThat(serviceInChild.greeting()).isEqualTo("Service 2"); |
||||
assertThat(serviceCaller1.getService()).isSameAs(serviceInParent); |
||||
assertThat(serviceCaller2.getService()).isSameAs(serviceInChild); |
||||
assertThat(serviceCaller1.sayGreeting()).isEqualTo("I say Service 1"); |
||||
assertThat(serviceCaller2.sayGreeting()).isEqualTo("I say Service 2"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
static class Config1 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 1"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller1(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class Config2 { |
||||
|
||||
@Bean |
||||
ExampleService service() { |
||||
return new RealExampleService("Service 2"); |
||||
} |
||||
|
||||
@Bean |
||||
ExampleServiceCaller serviceCaller2(ExampleService service) { |
||||
return new ExampleServiceCaller(service); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.ApplicationContext; |
||||
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.mockito.MockitoBean; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.BDDMockito.given; |
||||
|
||||
/** |
||||
* If the {@link ApplicationContext} for {@link ErrorIfContextReloadedConfig} is |
||||
* loaded twice (i.e., not properly cached), either this test class or |
||||
* {@link ReusedParentConfigV2Tests} will fail when both test classes are run |
||||
* within the same test suite. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = ErrorIfContextReloadedConfig.class), |
||||
@ContextConfiguration(classes = FooService.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class ReusedParentConfigV1Tests { |
||||
|
||||
@Autowired |
||||
ErrorIfContextReloadedConfig sharedConfig; |
||||
|
||||
@MockitoBean(contextName = "child") |
||||
FooService fooService; |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
assertThat(context.getParent().getBeanNamesForType(FooService.class)).isEmpty(); |
||||
assertThat(context.getBeanNamesForType(FooService.class)).hasSize(1); |
||||
|
||||
given(fooService.foo()).willReturn("mock"); |
||||
assertThat(fooService.foo()).isEqualTo("mock"); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* Copyright 2002-2025 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.hierarchies; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.ApplicationContext; |
||||
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.mockito.MockitoBean; |
||||
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.BDDMockito.given; |
||||
|
||||
/** |
||||
* If the {@link ApplicationContext} for {@link ErrorIfContextReloadedConfig} is |
||||
* loaded twice (i.e., not properly cached), either this test class or |
||||
* {@link ReusedParentConfigV1Tests} will fail when both test classes are run |
||||
* within the same test suite. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2.6 |
||||
*/ |
||||
@ExtendWith(SpringExtension.class) |
||||
@ContextHierarchy({ |
||||
@ContextConfiguration(classes = ErrorIfContextReloadedConfig.class), |
||||
@ContextConfiguration(classes = BarService.class, name = "child") |
||||
}) |
||||
@DisabledInAotMode("@ContextHierarchy is not supported in AOT") |
||||
class ReusedParentConfigV2Tests { |
||||
|
||||
@Autowired |
||||
ErrorIfContextReloadedConfig sharedConfig; |
||||
|
||||
@MockitoBean(contextName = "child") |
||||
BarService barService; |
||||
|
||||
|
||||
@Test |
||||
void test(ApplicationContext context) { |
||||
assertThat(context.getParent().getBeanNamesForType(BarService.class)).isEmpty(); |
||||
assertThat(context.getBeanNamesForType(BarService.class)).hasSize(1); |
||||
|
||||
given(barService.bar()).willReturn("mock"); |
||||
assertThat(barService.bar()).isEqualTo("mock"); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue