diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java index 1a3c426b5b..35cf78c1cb 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -26,9 +26,11 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.boot.test.mock.mockito.example.ExampleService; import org.springframework.boot.test.mock.mockito.example.FailingExampleService; +import org.springframework.boot.test.mock.mockito.example.RealExampleService; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import static org.assertj.core.api.Assertions.assertThat; @@ -84,6 +86,79 @@ public class MockitoPostProcessorTests { .isTrue(); } + @Test + public void canMockPrimaryBean() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + MockitoPostProcessor.register(context); + context.register(MockPrimaryBean.class); + context.refresh(); + assertThat(Mockito.mockingDetails(context.getBean(MockPrimaryBean.class).mock) + .isMock()).isTrue(); + assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isMock()) + .isTrue(); + assertThat(Mockito + .mockingDetails(context.getBean("examplePrimary", ExampleService.class)) + .isMock()).isTrue(); + assertThat(Mockito + .mockingDetails(context.getBean("exampleQualified", ExampleService.class)) + .isMock()).isFalse(); + } + + @Test + public void canMockQualifiedBeanWithPrimaryBeanPresent() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + MockitoPostProcessor.register(context); + context.register(MockQualifiedBean.class); + context.refresh(); + assertThat(Mockito.mockingDetails(context.getBean(MockQualifiedBean.class).mock) + .isMock()).isTrue(); + assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isMock()) + .isFalse(); + assertThat(Mockito + .mockingDetails(context.getBean("examplePrimary", ExampleService.class)) + .isMock()).isFalse(); + assertThat(Mockito + .mockingDetails(context.getBean("exampleQualified", ExampleService.class)) + .isMock()).isTrue(); + } + + @Test + public void canSpyPrimaryBean() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + MockitoPostProcessor.register(context); + context.register(SpyPrimaryBean.class); + context.refresh(); + assertThat( + Mockito.mockingDetails(context.getBean(SpyPrimaryBean.class).spy).isSpy()) + .isTrue(); + assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isSpy()) + .isTrue(); + assertThat(Mockito + .mockingDetails(context.getBean("examplePrimary", ExampleService.class)) + .isSpy()).isTrue(); + assertThat(Mockito + .mockingDetails(context.getBean("exampleQualified", ExampleService.class)) + .isSpy()).isFalse(); + } + + @Test + public void canSpyQualifiedBeanWithPrimaryBeanPresent() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + MockitoPostProcessor.register(context); + context.register(SpyQualifiedBean.class); + context.refresh(); + assertThat(Mockito.mockingDetails(context.getBean(SpyQualifiedBean.class).spy) + .isSpy()).isTrue(); + assertThat(Mockito.mockingDetails(context.getBean(ExampleService.class)).isSpy()) + .isFalse(); + assertThat(Mockito + .mockingDetails(context.getBean("examplePrimary", ExampleService.class)) + .isSpy()).isFalse(); + assertThat(Mockito + .mockingDetails(context.getBean("exampleQualified", ExampleService.class)) + .isSpy()).isTrue(); + } + @Configuration @MockBean(SomeInterface.class) static class MockedFactoryBean { @@ -137,6 +212,88 @@ public class MockitoPostProcessorTests { } + @Configuration + static class MockPrimaryBean { + + @MockBean(ExampleService.class) + private ExampleService mock; + + @Bean + @Qualifier("test") + public ExampleService exampleQualified() { + return new RealExampleService("qualified"); + } + + @Bean + @Primary + public ExampleService examplePrimary() { + return new RealExampleService("primary"); + } + + } + + @Configuration + static class MockQualifiedBean { + + @MockBean(ExampleService.class) + @Qualifier("test") + private ExampleService mock; + + @Bean + @Qualifier("test") + public ExampleService exampleQualified() { + return new RealExampleService("qualified"); + } + + @Bean + @Primary + public ExampleService examplePrimary() { + return new RealExampleService("primary"); + } + + } + + @Configuration + static class SpyPrimaryBean { + + @SpyBean(ExampleService.class) + private ExampleService spy; + + @Bean + @Qualifier("test") + public ExampleService exampleQualified() { + return new RealExampleService("qualified"); + } + + @Bean + @Primary + public ExampleService examplePrimary() { + return new RealExampleService("primary"); + } + + } + + @Configuration + static class SpyQualifiedBean { + + @SpyBean(ExampleService.class) + @Qualifier("test") + private ExampleService spy; + + @Bean + @Qualifier("test") + public ExampleService exampleQualified() { + return new RealExampleService("qualified"); + } + + @Bean + @Primary + public ExampleService examplePrimary() { + return new RealExampleService("primary"); + } + + } + static class TestFactoryBean implements FactoryBean { @Override diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java new file mode 100644 index 0000000000..d60f206a45 --- /dev/null +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 2012-2018 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 + * + * http://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.boot.test.mock.mockito; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.example.CustomQualifier; +import org.springframework.boot.test.mock.mockito.example.CustomQualifierExampleService; +import org.springframework.boot.test.mock.mockito.example.ExampleService; +import org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller; +import org.springframework.boot.test.mock.mockito.example.RealExampleService; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.verify; + +/** + * Test {@link SpyBean} on a test class field can be used to replace existing bean while + * preserving qualifiers. + * + * @author Andreas Neiser + */ +@RunWith(SpringRunner.class) +public class SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests { + + @SpyBean + @CustomQualifier + private ExampleService service; + + @Autowired + private ExampleServiceCaller caller; + + @Autowired + private ApplicationContext applicationContext; + + @Test + public void testMocking() throws Exception { + this.caller.sayGreeting(); + verify(this.service).greeting(); + } + + @Test + public void onlyQualifiedBeanIsReplaced() { + assertThat(this.applicationContext.getBean("service")).isSameAs(this.service); + ExampleService anotherService = this.applicationContext.getBean("anotherService", + ExampleService.class); + assertThat(anotherService.greeting()).isEqualTo("Another"); + } + + @Configuration + static class TestConfig { + + @Bean + public CustomQualifierExampleService service() { + return new CustomQualifierExampleService(); + } + + @Bean + public ExampleService anotherService() { + return new RealExampleService("Another"); + } + + @Bean + public ExampleServiceCaller controller(@CustomQualifier ExampleService service) { + return new ExampleServiceCaller(service); + } + + } + +}