diff --git a/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/SpringBootBeanDefinitionOverrideExceptionIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/SpringBootBeanDefinitionOverrideExceptionIntegrationTest.java new file mode 100644 index 0000000000..17787a86a8 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/SpringBootBeanDefinitionOverrideExceptionIntegrationTest.java @@ -0,0 +1,27 @@ +package com.baeldung.beandefinitionoverrideexception; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {TestConfiguration1.class, TestConfiguration2.class}, properties = {"spring.main.allow-bean-definition-overriding=true"}) +public class SpringBootBeanDefinitionOverrideExceptionIntegrationTest { + + @Autowired + private ApplicationContext applicationContext; + + @Test + public void whenBeanOverridingAllowed_thenTestBean2OverridesTestBean1() { + Object testBean = applicationContext.getBean("testBean"); + + assertThat(testBean.getClass()).isEqualTo(TestConfiguration2.TestBean2.class); + } +} diff --git a/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration1.java b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration1.java new file mode 100644 index 0000000000..d22c4388ae --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration1.java @@ -0,0 +1,26 @@ +package com.baeldung.beandefinitionoverrideexception; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan() +public class TestConfiguration1 { + + class TestBean1 { + + private String name; + + public String getName() { + return name; + } + + } + + @Bean + public TestBean1 testBean(){ + return new TestBean1(); + } + +} diff --git a/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration2.java b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration2.java new file mode 100644 index 0000000000..1a96240002 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/beandefinitionoverrideexception/TestConfiguration2.java @@ -0,0 +1,25 @@ +package com.baeldung.beandefinitionoverrideexception; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +@Configuration +public class TestConfiguration2 { + + class TestBean2 { + + private String name; + + public String getName() { + return name; + } + + } + + @Bean + public TestBean2 testBean(){ + return new TestBean2(); + } + +}