Test @MockBean/@SpyBean with @Primary

Add additional tests to ensure that `@MockBean` and `@SpyBean`
work consistently when combined with `@Primary`.

See gh-11077
pull/14206/merge
Andreas Neiser 6 years ago committed by Phillip Webb
parent c777614d8f
commit a5b3a2646b

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.test.mock.mockito.example.ExampleService; 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.FailingExampleService;
import org.springframework.boot.test.mock.mockito.example.RealExampleService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -84,6 +86,79 @@ public class MockitoPostProcessorTests {
.isTrue(); .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 @Configuration
@MockBean(SomeInterface.class) @MockBean(SomeInterface.class)
static class MockedFactoryBean { 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<Object> { static class TestFactoryBean implements FactoryBean<Object> {
@Override @Override

@ -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);
}
}
}
Loading…
Cancel
Save