pull/6919/merge
Phillip Webb 8 years ago
parent 6ec1ac0aa0
commit 04448d6bd9

@ -38,7 +38,8 @@ abstract class Definition {
private final boolean proxyTargetAware; private final boolean proxyTargetAware;
Definition(AnnotatedElement element, String name, MockReset reset, boolean proxyTargetAware) { Definition(AnnotatedElement element, String name, MockReset reset,
boolean proxyTargetAware) {
this.element = element; this.element = element;
this.name = name; this.name = name;
this.reset = (reset != null ? reset : MockReset.AFTER); this.reset = (reset != null ? reset : MockReset.AFTER);

@ -113,8 +113,7 @@ class DefinitionsParser {
} }
} }
private void addDefinition(Definition definition, private void addDefinition(Definition definition, String type) {
String type) {
boolean isNewDefinition = this.definitions.add(definition); boolean isNewDefinition = this.definitions.add(definition);
Assert.state(isNewDefinition, "Duplicate " + type + " definition " + definition); Assert.state(isNewDefinition, "Duplicate " + type + " definition " + definition);
AnnotatedElement element = definition.getElement(); AnnotatedElement element = definition.getElement();

@ -66,8 +66,7 @@ import org.springframework.test.context.junit4.SpringRunner;
* *
* *
* } * }
* </pre> * </pre> If there is more than one bean of the requested type, qualifier metadata must be
* If there is more than one bean of the requested type, qualifier metadata must be
* specified at field level: <pre class="code"> * specified at field level: <pre class="code">
* &#064;RunWith(SpringRunner.class) * &#064;RunWith(SpringRunner.class)
* public class ExampleTests { * public class ExampleTests {

@ -50,8 +50,9 @@ class MockDefinition extends Definition {
private final boolean serializable; private final boolean serializable;
MockDefinition(AnnotatedElement element, String name, ResolvableType typeToMock, Class<?>[] extraInterfaces, MockDefinition(AnnotatedElement element, String name, ResolvableType typeToMock,
Answers answer, boolean serializable, MockReset reset) { Class<?>[] extraInterfaces, Answers answer, boolean serializable,
MockReset reset) {
super(element, name, reset, false); super(element, name, reset, false);
Assert.notNull(typeToMock, "TypeToMock must not be null"); Assert.notNull(typeToMock, "TypeToMock must not be null");
this.typeToMock = typeToMock; this.typeToMock = typeToMock;

@ -66,8 +66,7 @@ import org.springframework.test.context.junit4.SpringRunner;
* *
* *
* } * }
* </pre> * </pre> If there is more than one bean of the requested type, qualifier metadata must be
* If there is more than one bean of the requested type, qualifier metadata must be
* specified at field level: <pre class="code"> * specified at field level: <pre class="code">
* &#064;RunWith(SpringRunner.class) * &#064;RunWith(SpringRunner.class)
* public class ExampleTests { * public class ExampleTests {

@ -82,8 +82,7 @@ public class DefinitionsParserTests {
this.parser.parse(MockBeanOnClassAndField.class); this.parser.parse(MockBeanOnClassAndField.class);
assertThat(getDefinitions()).hasSize(2); assertThat(getDefinitions()).hasSize(2);
MockDefinition classDefinition = getMockDefinition(0); MockDefinition classDefinition = getMockDefinition(0);
assertThat(classDefinition.getElement()) assertThat(classDefinition.getElement()).isEqualTo(MockBeanOnClassAndField.class);
.isEqualTo(MockBeanOnClassAndField.class);
assertThat(classDefinition.getTypeToMock().resolve()) assertThat(classDefinition.getTypeToMock().resolve())
.isEqualTo(ExampleService.class); .isEqualTo(ExampleService.class);
MockDefinition fieldDefinition = getMockDefinition(1); MockDefinition fieldDefinition = getMockDefinition(1);
@ -161,8 +160,7 @@ public class DefinitionsParserTests {
this.parser.parse(SpyBeanOnClassAndField.class); this.parser.parse(SpyBeanOnClassAndField.class);
assertThat(getDefinitions()).hasSize(2); assertThat(getDefinitions()).hasSize(2);
SpyDefinition classDefinition = getSpyDefinition(0); SpyDefinition classDefinition = getSpyDefinition(0);
assertThat(classDefinition.getElement()) assertThat(classDefinition.getElement()).isEqualTo(SpyBeanOnClassAndField.class);
.isEqualTo(SpyBeanOnClassAndField.class);
assertThat(classDefinition.getTypeToSpy().resolve()) assertThat(classDefinition.getTypeToSpy().resolve())
.isEqualTo(RealExampleService.class); .isEqualTo(RealExampleService.class);
SpyDefinition fieldDefinition = getSpyDefinition(1); SpyDefinition fieldDefinition = getSpyDefinition(1);

@ -34,8 +34,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Test {@link MockBean} on a test class field can be used to replace existing bean * Test {@link MockBean} on a test class field can be used to replace existing bean while
* while preserving qualifiers. * preserving qualifiers.
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class MockBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests { public class MockBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
@ -59,8 +59,8 @@ public class MockBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
@Test @Test
public void onlyQualifiedBeanIsReplaced() { public void onlyQualifiedBeanIsReplaced() {
assertThat(this.applicationContext.getBean("service")).isSameAs(this.service); assertThat(this.applicationContext.getBean("service")).isSameAs(this.service);
ExampleService anotherService = this.applicationContext.getBean( ExampleService anotherService = this.applicationContext.getBean("anotherService",
"anotherService", ExampleService.class); ExampleService.class);
assertThat(anotherService.greeting()).isEqualTo("Another"); assertThat(anotherService.greeting()).isEqualTo("Another");
} }

@ -65,8 +65,7 @@ public class MockDefinitionTests {
@Test @Test
public void createExplicit() throws Exception { public void createExplicit() throws Exception {
MockDefinition definition = new MockDefinition(getClass(), "name", MockDefinition definition = new MockDefinition(getClass(), "name",
EXAMPLE_SERVICE_TYPE, EXAMPLE_SERVICE_TYPE, new Class<?>[] { ExampleExtraInterface.class },
new Class<?>[] { ExampleExtraInterface.class },
Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE); Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE);
assertThat(definition.getElement()).isEqualTo(getClass()); assertThat(definition.getElement()).isEqualTo(getClass());
assertThat(definition.getName()).isEqualTo("name"); assertThat(definition.getName()).isEqualTo("name");
@ -81,8 +80,7 @@ public class MockDefinitionTests {
@Test @Test
public void createMock() throws Exception { public void createMock() throws Exception {
MockDefinition definition = new MockDefinition(null, "name", MockDefinition definition = new MockDefinition(null, "name", EXAMPLE_SERVICE_TYPE,
EXAMPLE_SERVICE_TYPE,
new Class<?>[] { ExampleExtraInterface.class }, new Class<?>[] { ExampleExtraInterface.class },
Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE); Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE);
ExampleService mock = definition.createMock(); ExampleService mock = definition.createMock();

@ -53,7 +53,8 @@ public class MockitoContextCustomizerTests {
} }
private MockDefinition createTestMockDefinition(Class<?> typeToMock) { private MockDefinition createTestMockDefinition(Class<?> typeToMock) {
return new MockDefinition(null, null, ResolvableType.forClass(typeToMock), null, null, false, null); return new MockDefinition(null, null, ResolvableType.forClass(typeToMock), null,
null, false, null);
} }
} }

@ -52,7 +52,8 @@ public class SpyDefinitionTests {
@Test @Test
public void createWithDefaults() throws Exception { public void createWithDefaults() throws Exception {
SpyDefinition definition = new SpyDefinition(null, null, REAL_SERVICE_TYPE, null, true); SpyDefinition definition = new SpyDefinition(null, null, REAL_SERVICE_TYPE, null,
true);
assertThat(definition.getElement()).isNull(); assertThat(definition.getElement()).isNull();
assertThat(definition.getName()).isNull(); assertThat(definition.getName()).isNull();
assertThat(definition.getTypeToSpy()).isEqualTo(REAL_SERVICE_TYPE); assertThat(definition.getTypeToSpy()).isEqualTo(REAL_SERVICE_TYPE);

Loading…
Cancel
Save