Commit 34070e5a authored by Phillip Webb's avatar Phillip Webb

Add Support for Mockito spies

Add a @SpyBean annotation that can be used to create spies.

Fixes gh-5538
parent 1d4f38e4
/*
* Copyright 2012-2016 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.springframework.util.ObjectUtils;
/**
* Base class for {@link MockDefinition} and {@link SpyDefinition}.
*
* @author Phillip Webb
* @see DefinitionsParser
*/
abstract class Definition {
private static final int MULTIPLIER = 31;
private final String name;
private final MockReset reset;
Definition(String name, MockReset reset) {
this.name = name;
this.reset = (reset != null ? reset : MockReset.AFTER);
}
/**
* Return the name for bean.
* @return the name or {@code null}
*/
public String getName() {
return this.name;
}
/**
* Return the mock reset mode.
* @return the reset mode
*/
public MockReset getReset() {
return this.reset;
}
@Override
public int hashCode() {
int result = 1;
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.name);
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.reset);
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !getClass().isAssignableFrom(obj.getClass())) {
return false;
}
Definition other = (Definition) obj;
boolean result = true;
result &= ObjectUtils.nullSafeEquals(this.name, other.name);
result &= ObjectUtils.nullSafeEquals(this.reset, other.reset);
return result;
}
}
...@@ -33,24 +33,25 @@ import org.springframework.util.ReflectionUtils.FieldCallback; ...@@ -33,24 +33,25 @@ import org.springframework.util.ReflectionUtils.FieldCallback;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Parser to create {@link MockDefinition} from {@link MockBean @MockBean} annotations * Parser to create {@link MockDefinition} and {@link SpyDefinition} instances from
* declared on or in a class. * {@link MockBean @MockBean} and {@link SpyBean @SpyBean} annotations declared on or in a
* class.
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
class MockDefinitionsParser { class DefinitionsParser {
private final Set<MockDefinition> definitions; private final Set<Definition> definitions;
private final Map<MockDefinition, Field> fields; private final Map<Definition, Field> definitionFields;
MockDefinitionsParser() { DefinitionsParser() {
this(Collections.<MockDefinition>emptySet()); this(Collections.<Definition>emptySet());
} }
MockDefinitionsParser(Collection<? extends MockDefinition> existing) { DefinitionsParser(Collection<? extends Definition> existing) {
this.definitions = new LinkedHashSet<MockDefinition>(); this.definitions = new LinkedHashSet<Definition>();
this.fields = new LinkedHashMap<MockDefinition, Field>(); this.definitionFields = new LinkedHashMap<Definition, Field>();
if (existing != null) { if (existing != null) {
this.definitions.addAll(existing); this.definitions.addAll(existing);
} }
...@@ -72,12 +73,16 @@ class MockDefinitionsParser { ...@@ -72,12 +73,16 @@ class MockDefinitionsParser {
private void parseElement(AnnotatedElement element) { private void parseElement(AnnotatedElement element) {
for (MockBean annotation : AnnotationUtils.getRepeatableAnnotations(element, for (MockBean annotation : AnnotationUtils.getRepeatableAnnotations(element,
MockBean.class, MockBeans.class)) { MockBean.class, MockBeans.class)) {
parseAnnotation(annotation, element); parseMockBeanAnnotation(annotation, element);
}
for (SpyBean annotation : AnnotationUtils.getRepeatableAnnotations(element,
SpyBean.class, SpyBeans.class)) {
parseSpyBeanAnnotation(annotation, element);
} }
} }
private void parseAnnotation(MockBean annotation, AnnotatedElement element) { private void parseMockBeanAnnotation(MockBean annotation, AnnotatedElement element) {
Set<Class<?>> classesToMock = getOrDeduceClassesToMock(annotation, element); Set<Class<?>> classesToMock = getOrDeduceClasses(element, annotation.value());
Assert.state(!classesToMock.isEmpty(), Assert.state(!classesToMock.isEmpty(),
"Unable to deduce class to mock from " + element); "Unable to deduce class to mock from " + element);
if (StringUtils.hasLength(annotation.name())) { if (StringUtils.hasLength(annotation.name())) {
...@@ -88,30 +93,50 @@ class MockDefinitionsParser { ...@@ -88,30 +93,50 @@ class MockDefinitionsParser {
MockDefinition definition = new MockDefinition(annotation.name(), classToMock, MockDefinition definition = new MockDefinition(annotation.name(), classToMock,
annotation.extraInterfaces(), annotation.answer(), annotation.extraInterfaces(), annotation.answer(),
annotation.serializable(), annotation.reset()); annotation.serializable(), annotation.reset());
boolean isNewDefinition = this.definitions.add(definition); addDefinition(element, definition, "mock");
Assert.state(isNewDefinition, "Duplicate mock definition " + definition); }
if (element instanceof Field) { }
this.fields.put(definition, (Field) element);
} private void parseSpyBeanAnnotation(SpyBean annotation, AnnotatedElement element) {
Set<Class<?>> classesToSpy = getOrDeduceClasses(element, annotation.value());
Assert.state(!classesToSpy.isEmpty(),
"Unable to deduce class to spy from " + element);
if (StringUtils.hasLength(annotation.name())) {
Assert.state(classesToSpy.size() == 1,
"The name attribute can only be used when spying a single class");
}
for (Class<?> classToSpy : classesToSpy) {
SpyDefinition definition = new SpyDefinition(annotation.name(), classToSpy,
annotation.reset());
addDefinition(element, definition, "spy");
}
}
private void addDefinition(AnnotatedElement element, Definition definition,
String type) {
boolean isNewDefinition = this.definitions.add(definition);
Assert.state(isNewDefinition, "Duplicate " + type + " definition " + definition);
if (element instanceof Field) {
Field field = (Field) element;
this.definitionFields.put(definition, field);
} }
} }
private Set<Class<?>> getOrDeduceClassesToMock(MockBean annotation, private Set<Class<?>> getOrDeduceClasses(AnnotatedElement element, Class<?>[] value) {
AnnotatedElement element) {
Set<Class<?>> classes = new LinkedHashSet<Class<?>>(); Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
classes.addAll(Arrays.asList(annotation.value())); classes.addAll(Arrays.asList(value));
if (classes.isEmpty() && element instanceof Field) { if (classes.isEmpty() && element instanceof Field) {
classes.add(((Field) element).getType()); classes.add(((Field) element).getType());
} }
return classes; return classes;
} }
public Set<MockDefinition> getDefinitions() { public Set<Definition> getDefinitions() {
return Collections.unmodifiableSet(this.definitions); return Collections.unmodifiableSet(this.definitions);
} }
public Field getField(MockDefinition definition) { public Field getField(Definition definition) {
return this.fields.get(definition); return this.definitionFields.get(definition);
} }
} }
...@@ -82,9 +82,9 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -82,9 +82,9 @@ import org.springframework.test.context.junit4.SpringRunner;
public @interface MockBean { public @interface MockBean {
/** /**
* The name of the bean that should be registered with the application context. If not * The name of the bean to register or replace. If not specified the name will either
* specified the name will either be generated or, if the mock replaces an existing * be generated or, if the mock replaces an existing bean, the existing name will be
* bean, the existing name will be used. * used.
* @return the name of the bean * @return the name of the bean
*/ */
String name() default ""; String name() default "";
......
...@@ -35,12 +35,10 @@ import org.springframework.util.StringUtils; ...@@ -35,12 +35,10 @@ import org.springframework.util.StringUtils;
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
class MockDefinition { class MockDefinition extends Definition {
private static final int MULTIPLIER = 31; private static final int MULTIPLIER = 31;
private final String name;
private final Class<?> classToMock; private final Class<?> classToMock;
private final Set<Class<?>> extraInterfaces; private final Set<Class<?>> extraInterfaces;
...@@ -49,21 +47,18 @@ class MockDefinition { ...@@ -49,21 +47,18 @@ class MockDefinition {
private final boolean serializable; private final boolean serializable;
private final MockReset reset;
MockDefinition(Class<?> classToMock) { MockDefinition(Class<?> classToMock) {
this(null, classToMock, null, null, false, null); this(null, classToMock, null, null, false, null);
} }
MockDefinition(String name, Class<?> classToMock, Class<?>[] extraInterfaces, MockDefinition(String name, Class<?> classToMock, Class<?>[] extraInterfaces,
Answers answer, boolean serializable, MockReset reset) { Answers answer, boolean serializable, MockReset reset) {
super(name, reset);
Assert.notNull(classToMock, "ClassToMock must not be null"); Assert.notNull(classToMock, "ClassToMock must not be null");
this.name = name;
this.classToMock = classToMock; this.classToMock = classToMock;
this.extraInterfaces = asClassSet(extraInterfaces); this.extraInterfaces = asClassSet(extraInterfaces);
this.answer = (answer != null ? answer : Answers.RETURNS_DEFAULTS); this.answer = (answer != null ? answer : Answers.RETURNS_DEFAULTS);
this.serializable = serializable; this.serializable = serializable;
this.reset = (reset != null ? reset : MockReset.AFTER);
} }
private Set<Class<?>> asClassSet(Class<?>[] classes) { private Set<Class<?>> asClassSet(Class<?>[] classes) {
...@@ -74,14 +69,6 @@ class MockDefinition { ...@@ -74,14 +69,6 @@ class MockDefinition {
return Collections.unmodifiableSet(classSet); return Collections.unmodifiableSet(classSet);
} }
/**
* Return the name for bean.
* @return the name or {@code null}
*/
public String getName() {
return this.name;
}
/** /**
* Return the classes that should be mocked. * Return the classes that should be mocked.
* @return the class to mock; never {@code null} * @return the class to mock; never {@code null}
...@@ -114,23 +101,13 @@ class MockDefinition { ...@@ -114,23 +101,13 @@ class MockDefinition {
return this.serializable; return this.serializable;
} }
/**
* Return the mock reset mode.
* @return the reset mode
*/
public MockReset getReset() {
return this.reset;
}
@Override @Override
public int hashCode() { public int hashCode() {
int result = 1; int result = super.hashCode();
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.name);
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.classToMock); result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.classToMock);
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.extraInterfaces); result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.extraInterfaces);
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.answer); result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.answer);
result = MULTIPLIER * result + (this.serializable ? 1231 : 1237); result = MULTIPLIER * result + (this.serializable ? 1231 : 1237);
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.reset);
return result; return result;
} }
...@@ -143,32 +120,30 @@ class MockDefinition { ...@@ -143,32 +120,30 @@ class MockDefinition {
return false; return false;
} }
MockDefinition other = (MockDefinition) obj; MockDefinition other = (MockDefinition) obj;
boolean result = true; boolean result = super.equals(obj);
result &= ObjectUtils.nullSafeEquals(this.name, other.name);
result &= ObjectUtils.nullSafeEquals(this.classToMock, other.classToMock); result &= ObjectUtils.nullSafeEquals(this.classToMock, other.classToMock);
result &= ObjectUtils.nullSafeEquals(this.extraInterfaces, other.extraInterfaces); result &= ObjectUtils.nullSafeEquals(this.extraInterfaces, other.extraInterfaces);
result &= ObjectUtils.nullSafeEquals(this.answer, other.answer); result &= ObjectUtils.nullSafeEquals(this.answer, other.answer);
result &= this.serializable == other.serializable; result &= this.serializable == other.serializable;
result &= ObjectUtils.nullSafeEquals(this.reset, other.reset);
return result; return result;
} }
@Override @Override
public String toString() { public String toString() {
return new ToStringCreator(this).append("name", this.name) return new ToStringCreator(this).append("name", getName())
.append("classToMock", this.classToMock) .append("classToMock", this.classToMock)
.append("extraInterfaces", this.extraInterfaces) .append("extraInterfaces", this.extraInterfaces)
.append("answer", this.answer).append("serializable", this.serializable) .append("answer", this.answer).append("serializable", this.serializable)
.append("reset", this.reset).toString(); .append("reset", getReset()).toString();
} }
public <T> T createMock() { public <T> T createMock() {
return createMock(this.name); return createMock(getName());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T createMock(String name) { public <T> T createMock(String name) {
MockSettings settings = MockReset.withSettings(this.reset); MockSettings settings = MockReset.withSettings(getReset());
if (StringUtils.hasLength(name)) { if (StringUtils.hasLength(name)) {
settings.name(name); settings.name(name);
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.test.mock.mockito; package org.springframework.boot.test.mock.mockito;
import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
...@@ -30,10 +31,10 @@ import org.springframework.test.context.MergedContextConfiguration; ...@@ -30,10 +31,10 @@ import org.springframework.test.context.MergedContextConfiguration;
*/ */
class MockitoContextCustomizer implements ContextCustomizer { class MockitoContextCustomizer implements ContextCustomizer {
private final Set<MockDefinition> definitions; private final Set<Definition> definitions;
MockitoContextCustomizer(Set<MockDefinition> definitions) { MockitoContextCustomizer(Set<? extends Definition> definitions) {
this.definitions = definitions; this.definitions = new LinkedHashSet<Definition>(definitions);
} }
@Override @Override
......
...@@ -34,7 +34,7 @@ class MockitoContextCustomizerFactory implements ContextCustomizerFactory { ...@@ -34,7 +34,7 @@ class MockitoContextCustomizerFactory implements ContextCustomizerFactory {
List<ContextConfigurationAttributes> configAttributes) { List<ContextConfigurationAttributes> configAttributes) {
// We gather the explicit mock definitions here since they form part of the // We gather the explicit mock definitions here since they form part of the
// MergedContextConfiguration key. Different mocks need to have a different key // MergedContextConfiguration key. Different mocks need to have a different key
MockDefinitionsParser parser = new MockDefinitionsParser(); DefinitionsParser parser = new DefinitionsParser();
parser.parse(testClass); parser.parse(testClass);
return new MockitoContextCustomizer(parser.getDefinitions()); return new MockitoContextCustomizer(parser.getDefinitions());
} }
......
...@@ -38,10 +38,11 @@ import org.springframework.util.ReflectionUtils.FieldCallback; ...@@ -38,10 +38,11 @@ import org.springframework.util.ReflectionUtils.FieldCallback;
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
class MockitoInitializeTestExecutionListener extends AbstractTestExecutionListener { class MockitoTestExecutionListener extends AbstractTestExecutionListener {
@Override @Override
public void prepareTestInstance(TestContext testContext) throws Exception { public void prepareTestInstance(TestContext testContext) throws Exception {
System.out.println("Prepare");
if (hasMockitoAnnotations(testContext)) { if (hasMockitoAnnotations(testContext)) {
MockitoAnnotations.initMocks(testContext.getTestInstance()); MockitoAnnotations.initMocks(testContext.getTestInstance());
} }
...@@ -55,18 +56,18 @@ class MockitoInitializeTestExecutionListener extends AbstractTestExecutionListen ...@@ -55,18 +56,18 @@ class MockitoInitializeTestExecutionListener extends AbstractTestExecutionListen
} }
private void injectFields(TestContext testContext) { private void injectFields(TestContext testContext) {
MockDefinitionsParser parser = new MockDefinitionsParser(); DefinitionsParser parser = new DefinitionsParser();
parser.parse(testContext.getTestClass()); parser.parse(testContext.getTestClass());
if (!parser.getDefinitions().isEmpty()) { if (!parser.getDefinitions().isEmpty()) {
injectFields(testContext, parser); injectFields(testContext, parser);
} }
} }
private void injectFields(TestContext testContext, MockDefinitionsParser parser) { private void injectFields(TestContext testContext, DefinitionsParser parser) {
ApplicationContext applicationContext = testContext.getApplicationContext(); ApplicationContext applicationContext = testContext.getApplicationContext();
MockitoPostProcessor postProcessor = applicationContext MockitoPostProcessor postProcessor = applicationContext
.getBean(MockitoPostProcessor.class); .getBean(MockitoPostProcessor.class);
for (MockDefinition definition : parser.getDefinitions()) { for (Definition definition : parser.getDefinitions()) {
Field field = parser.getField(definition); Field field = parser.getField(definition);
if (field != null) { if (field != null) {
postProcessor.inject(field, testContext.getTestInstance(), definition); postProcessor.inject(field, testContext.getTestInstance(), definition);
......
/*
* Copyright 2012-2016 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 java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AliasFor;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Annotation that can be used to appy Mockto spies to a Spring
* {@link ApplicationContext}. Can be used as a class level annotation or on fields in
* either {@code @Configuration} classes, or test classes that are
* {@link RunWith @RunWith} the {@link SpringRunner}.
* <p>
* Spies can be applied by type or by {@link #name() bean name}. All beans in the context
* of the same type will be wrapped with the spy, if no existing bean is defined a new one
* will be added.
* <p>
* When {@code @SpyBean} is used on a field, as well as being registered in the
* application context, the spy will also be injected into the field. Typical usage might
* be: <pre class="code">
* &#064;RunWith(SpringRunner.class)
* public class ExampleTests {
*
* &#064;SpyBean
* private ExampleService service;
*
* &#064;Autowired
* private UserOfService userOfService;
*
* &#064;Test
* public void testUserOfService() {
* String actual = this.userOfService.makeUse();
* assertEquals("Was: Hello", actual);
* verify(this.service).greet();
* }
*
* &#064;Configuration
* &#064;Import(UserOfService.class) // A &#064;Component injected with ExampleService
* static class Config {
* }
*
*
* }
* </pre>
* <p>
* This annotation is {@code @Repeatable} and may be specified multiple times when working
* with Java 8 or contained within an {@link SpyBeans @SpyBeans} annotation.
*
* @author Phillip Webb
* @since 1.4.0
* @see MockitoPostProcessor
*/
@Target({ ElementType.TYPE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(SpyBeans.class)
public @interface SpyBean {
/**
* The name of the bean to spy. If not specified the name will either be generated or,
* if the spy is for an existing bean, the existing name will be used.
* @return the name of the bean
*/
String name() default "";
/**
* The classes to spy. This is an alias of {@link #classes()} which can be used for
* brevity if no other attributes are defined. See {@link #classes()} for details.
* @return the classes to mock
*/
@AliasFor("classes")
Class<?>[] value() default {};
/**
* The classes to spy. Each class specified here will result in a spy being applied.
* Classes can be omitted when the annotation is used on a field.
* <p>
* When {@code @MockBean} also defines a {@code name} this attribute can only contain
* a single value.
* <p>
* If this is the only attribute specified consider using the {@code value} alias
* instead.
* @return the classes to mock
*/
@AliasFor("value")
Class<?>[] classes() default {};
/**
* The reset mode to apply to the spied bean. The default is {@link MockReset#AFTER}
* meaning that spies are automatically reset after each test method is invoked.
* @return the reset mode
*/
MockReset reset() default MockReset.AFTER;
}
/*
* Copyright 2012-2016 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 java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Container annotation that aggregates several {@link SpyBean} annotations.
* <p>
* Can be used natively, declaring several nested {@link SpyBean} annotations. Can also be
* used in conjunction with Java 8's support for <em>repeatable annotations</em>, where
* {@link SpyBean} can simply be declared several times on the same
* {@linkplain ElementType#TYPE type}, implicitly generating this container annotation.
*
* @author Phillip Webb
* @since 1.4.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface SpyBeans {
/**
* Return the contained {@link SpyBean} annotations.
* @return the spy beans
*/
SpyBean[] value();
}
/*
* Copyright 2012-2016 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.mockito.MockSettings;
import org.mockito.Mockito;
import org.mockito.internal.util.MockUtil;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* A complete definition that can be used to create a Mockito spy.
*
* @author Phillip Webb
*/
class SpyDefinition extends Definition {
private MockUtil mockUtil = new MockUtil();
private static final int MULTIPLIER = 31;
private final Class<?> classToSpy;
SpyDefinition(String name, Class<?> classToSpy, MockReset reset) {
super(name, reset);
Assert.notNull(classToSpy, "ClassToSpy must not be null");
this.classToSpy = classToSpy;
}
public Class<?> getClassToSpy() {
return this.classToSpy;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.classToSpy);
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != getClass()) {
return false;
}
SpyDefinition other = (SpyDefinition) obj;
boolean result = super.equals(obj);
result &= ObjectUtils.nullSafeEquals(this.classToSpy, other.classToSpy);
return result;
}
@Override
public String toString() {
return new ToStringCreator(this).append("name", getName())
.append("classToSpy", this.classToSpy).append("reset", getReset())
.toString();
}
public <T> T createSpy(Object instance) {
return createSpy(getName(), instance);
}
@SuppressWarnings("unchecked")
public <T> T createSpy(String name, Object instance) {
Assert.notNull(instance, "Instance must not be null");
Assert.isInstanceOf(this.classToSpy, instance);
if (this.mockUtil.isSpy(instance)) {
return (T) instance;
}
MockSettings settings = MockReset.withSettings(getReset());
if (StringUtils.hasLength(name)) {
settings.name(name);
}
settings.spiedInstance(instance);
settings.defaultAnswer(Mockito.CALLS_REAL_METHODS);
return (T) Mockito.mock(instance.getClass(), settings);
}
}
...@@ -7,5 +7,5 @@ org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory ...@@ -7,5 +7,5 @@ org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory
# Test Execution Listeners # Test Execution Listeners
org.springframework.test.context.TestExecutionListener=\ org.springframework.test.context.TestExecutionListener=\
org.springframework.boot.test.mock.mockito.MockitoInitializeTestExecutionListener,\ org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener,\
org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener
...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given; ...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given;
* @author Phillip Webb * @author Phillip Webb
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class OnConfigurationClassForExistingBeanIntegrationTests { public class MockBeanOnConfigurationClassForExistingBeanIntegrationTests {
@Autowired @Autowired
private ExampleServiceCaller caller; private ExampleServiceCaller caller;
......
...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given; ...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given;
* @author Phillip Webb * @author Phillip Webb
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class OnConfigurationClassForNewBeanIntegrationTests { public class MockBeanOnConfigurationClassForNewBeanIntegrationTests {
@Autowired @Autowired
private ExampleServiceCaller caller; private ExampleServiceCaller caller;
......
...@@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.given; ...@@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.given;
* @author Phillip Webb * @author Phillip Webb
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class OnConfigurationFieldForExistingBeanIntegrationTests { public class MockBeanOnConfigurationFieldForExistingBeanIntegrationTests {
@Autowired @Autowired
private Config config; private Config config;
......
...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given; ...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given;
* @author Phillip Webb * @author Phillip Webb
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class OnConfigurationFieldForNewBeanIntegrationTests { public class MockBeanOnConfigurationFieldForNewBeanIntegrationTests {
@Autowired @Autowired
private Config config; private Config config;
......
...@@ -21,8 +21,8 @@ import org.junit.runner.RunWith; ...@@ -21,8 +21,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.OnContextHierarchyIntegrationTests.ChildConfig; import org.springframework.boot.test.mock.mockito.MockBeanOnContextHierarchyIntegrationTests.ChildConfig;
import org.springframework.boot.test.mock.mockito.OnContextHierarchyIntegrationTests.ParentConfig; import org.springframework.boot.test.mock.mockito.MockBeanOnContextHierarchyIntegrationTests.ParentConfig;
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.ExampleServiceCaller; import org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
...@@ -42,7 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -42,7 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class), @ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class),
@ContextConfiguration(classes = ChildConfig.class) }) @ContextConfiguration(classes = ChildConfig.class) })
public class OnContextHierarchyIntegrationTests { public class MockBeanOnContextHierarchyIntegrationTests {
@Autowired @Autowired
private ChildConfig childConfig; private ChildConfig childConfig;
......
...@@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.given; ...@@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.given;
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@MockBean(ExampleService.class) @MockBean(ExampleService.class)
public class OnTestClassForExistingBeanIntegrationTests { public class MockBeanOnTestClassForExistingBeanIntegrationTests {
@Autowired @Autowired
private ExampleServiceCaller caller; private ExampleServiceCaller caller;
......
...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given; ...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given;
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@MockBean(ExampleService.class) @MockBean(ExampleService.class)
public class OnTestClassForNewBeanIntegrationTests { public class MockBeanOnTestClassForNewBeanIntegrationTests {
@Autowired @Autowired
private ExampleServiceCaller caller; private ExampleServiceCaller caller;
......
...@@ -31,15 +31,15 @@ import static org.mockito.BDDMockito.given; ...@@ -31,15 +31,15 @@ import static org.mockito.BDDMockito.given;
/** /**
* Test {@link MockBean} on a test class field can be used to replace existing beans when * Test {@link MockBean} on a test class field can be used to replace existing beans when
* the context is cached. This test is identical to * the context is cached. This test is identical to
* {@link OnTestFieldForExistingBeanCacheIntegrationTests} so one of them should trigger * {@link MockBeanOnTestFieldForExistingBeanIntegrationTests} so one of them should
* application context caching. * trigger application context caching.
* *
* @author Phillip Webb * @author Phillip Webb
* @see OnTestFieldForExistingBeanIntegrationTests * @see MockBeanOnTestFieldForExistingBeanIntegrationTests
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = OnTestFieldForExistingBeanConfig.class) @ContextConfiguration(classes = MockBeanOnTestFieldForExistingBeanConfig.class)
public class OnTestFieldForExistingBeanCacheIntegrationTests { public class MockBeanOnTestFieldForExistingBeanCacheIntegrationTests {
@MockBean @MockBean
private ExampleService exampleService; private ExampleService exampleService;
......
...@@ -21,14 +21,14 @@ import org.springframework.context.annotation.Configuration; ...@@ -21,14 +21,14 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
/** /**
* Config for {@link OnTestFieldForExistingBeanIntegrationTests} and * Config for {@link MockBeanOnTestFieldForExistingBeanIntegrationTests} and
* {@link OnTestFieldForExistingBeanCacheIntegrationTests}. Extracted to a shared config * {@link MockBeanOnTestFieldForExistingBeanCacheIntegrationTests}. Extracted to a shared config
* to trigger caching. * to trigger caching.
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
@Configuration @Configuration
@Import(ExampleServiceCaller.class) @Import(ExampleServiceCaller.class)
public class OnTestFieldForExistingBeanConfig { public class MockBeanOnTestFieldForExistingBeanConfig {
} }
...@@ -32,11 +32,11 @@ import static org.mockito.BDDMockito.given; ...@@ -32,11 +32,11 @@ import static org.mockito.BDDMockito.given;
* Test {@link MockBean} on a test class field can be used to replace existing beans. * Test {@link MockBean} on a test class field can be used to replace existing beans.
* *
* @author Phillip Webb * @author Phillip Webb
* @see OnTestFieldForExistingBeanCacheIntegrationTests * @see MockBeanOnTestFieldForExistingBeanCacheIntegrationTests
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = OnTestFieldForExistingBeanConfig.class) @ContextConfiguration(classes = MockBeanOnTestFieldForExistingBeanConfig.class)
public class OnTestFieldForExistingBeanIntegrationTests { public class MockBeanOnTestFieldForExistingBeanIntegrationTests {
@MockBean @MockBean
private ExampleService exampleService; private ExampleService exampleService;
......
...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given; ...@@ -36,7 +36,7 @@ import static org.mockito.BDDMockito.given;
* @author Phillip Webb * @author Phillip Webb
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class OnTestFieldForNewBeanIntegrationTests { public class MockBeanOnTestFieldForNewBeanIntegrationTests {
@MockBean @MockBean
private ExampleService exampleService; private ExampleService exampleService;
......
...@@ -37,13 +37,13 @@ import static org.mockito.Mockito.mock; ...@@ -37,13 +37,13 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link MockitoInitializeTestExecutionListener}. * Tests for {@link MockitoTestExecutionListener}.
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
public class MockitoInitializeTestExecutionListenerTests { public class MockitoInitializeTestExecutionListenerTests {
private MockitoInitializeTestExecutionListener listener = new MockitoInitializeTestExecutionListener(); private MockitoTestExecutionListener listener = new MockitoTestExecutionListener();
@Mock @Mock
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
......
...@@ -27,8 +27,7 @@ import org.springframework.context.annotation.Bean; ...@@ -27,8 +27,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/** /**
* Test for {@link MockitoPostProcessor}. See also the integration tests in the * Test for {@link MockitoPostProcessor}. See also the integration tests.
* {@code runner} package.
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
......
/*
* Copyright 2012-2016 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.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
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 configuration class can be used to spy existing beans.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
public class SpyBeanOnConfigurationClassForExistingBeanIntegrationTests {
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
}
@Configuration
@SpyBean(SimpleExampleService.class)
@Import({ ExampleServiceCaller.class, SimpleExampleService.class })
static class Config {
}
}
/*
* Copyright 2012-2016 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.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
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 configuration class can be used to inject new spy instances.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
public class SpyBeanOnConfigurationClassForNewBeanIntegrationTests {
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
}
@Configuration
@SpyBean(SimpleExampleService.class)
@Import(ExampleServiceCaller.class)
static class Config {
}
}
/*
* Copyright 2012-2016 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.ExampleService;
import org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
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 field on a {@code @Configuration} class can be used to
* replace existing beans.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
public class SpyBeanOnConfigurationFieldForExistingBeanIntegrationTests {
@Autowired
private Config config;
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.config.exampleService).greeting();
}
@Configuration
@Import({ ExampleServiceCaller.class, SimpleExampleService.class })
static class Config {
@SpyBean
private ExampleService exampleService;
}
}
/*
* Copyright 2012-2016 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.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
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 field on a {@code @Configuration} class can be used to inject
* new spy instances.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
public class SpyBeanOnConfigurationFieldForNewBeanIntegrationTests {
@Autowired
private Config config;
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.config.exampleService).greeting();
}
@Configuration
@Import(ExampleServiceCaller.class)
static class Config {
@SpyBean
private SimpleExampleService exampleService;
}
}
/*
* Copyright 2012-2016 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.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBeanOnContextHierarchyIntegrationTests.ChildConfig;
import org.springframework.boot.test.mock.mockito.SpyBeanOnContextHierarchyIntegrationTests.ParentConfig;
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.SimpleExampleService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test {@link SpyBean} can be used with a {@link ContextHierarchy}.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@ContextHierarchy({ @ContextConfiguration(classes = ParentConfig.class),
@ContextConfiguration(classes = ChildConfig.class) })
public class SpyBeanOnContextHierarchyIntegrationTests {
@Autowired
private ChildConfig childConfig;
@Test
public void testSpying() throws Exception {
ApplicationContext context = this.childConfig.getContext();
ApplicationContext parentContext = context.getParent();
assertThat(parentContext.getBeanNamesForType(ExampleService.class)).hasSize(1);
assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class))
.hasSize(0);
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(0);
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
assertThat(context.getBean(ExampleService.class)).isNotNull();
assertThat(context.getBean(ExampleServiceCaller.class)).isNotNull();
}
@Configuration
@SpyBean(SimpleExampleService.class)
static class ParentConfig {
}
@Configuration
@SpyBean(ExampleServiceCaller.class)
static class ChildConfig implements ApplicationContextAware {
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
public ApplicationContext getContext() {
return this.context;
}
}
}
/*
* Copyright 2012-2016 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.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
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 can be used to replace existing beans.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@SpyBean(SimpleExampleService.class)
public class SpyBeanOnTestClassForExistingBeanIntegrationTests {
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
}
@Configuration
@Import({ ExampleServiceCaller.class, SimpleExampleService.class })
static class Config {
}
}
/*
* Copyright 2012-2016 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.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
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 can be used to inject new mock instances.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@SpyBean(SimpleExampleService.class)
public class SpyBeanOnTestClassForNewBeanIntegrationTests {
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
}
@Configuration
@Import(ExampleServiceCaller.class)
static class Config {
}
}
/*
* Copyright 2012-2016 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.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.test.context.ContextConfiguration;
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 beans when
* the context is cached. This test is identical to
* {@link SpyBeanOnTestFieldForExistingBeanIntegrationTests} so one of them should trigger
* application context caching.
*
* @author Phillip Webb
* @see SpyBeanOnTestFieldForExistingBeanIntegrationTests
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = SpyBeanOnTestFieldForExistingBeanConfig.class)
public class SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests {
@SpyBean
private SimpleExampleService exampleService;
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
}
}
/*
* Copyright 2012-2016 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.springframework.boot.test.mock.mockito.example.ExampleServiceCaller;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Config for {@link SpyBeanOnTestFieldForExistingBeanIntegrationTests} and
* {@link SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests}. Extracted to a shared
* config to trigger caching.
*
* @author Phillip Webb
*/
@Configuration
@Import(ExampleServiceCaller.class)
public class SpyBeanOnTestFieldForExistingBeanConfig {
}
/*
* Copyright 2012-2016 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.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.test.context.ContextConfiguration;
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 beans.
*
* @author Phillip Webb
* @see SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MockBeanOnTestFieldForExistingBeanConfig.class)
public class SpyBeanOnTestFieldForExistingBeanIntegrationTests {
@SpyBean
private SimpleExampleService exampleService;
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
}
}
/*
* Copyright 2012-2016 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.ExampleService;
import org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller;
import org.springframework.boot.test.mock.mockito.example.SimpleExampleService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
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 inject new mock instances.
*
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
public class SpyBeanOnTestFieldForNewBeanIntegrationTests {
@SpyBean
private ExampleService exampleService;
@Autowired
private ExampleServiceCaller caller;
@Test
public void testSpying() throws Exception {
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
verify(this.caller.getService()).greeting();
}
@Configuration
@Import({ ExampleServiceCaller.class, SimpleExampleService.class })
static class Config {
}
}
/*
* Copyright 2012-2016 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.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Answers;
import org.mockito.internal.util.MockUtil;
import org.mockito.mock.MockCreationSettings;
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 static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SpyDefinition}.
*
* @author Phillip Webb
*/
public class SpyDefinitionTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void ClassToSpyMustNotBeNull() throws Exception {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("ClassToSpy must not be null");
new SpyDefinition(null, null, null);
}
@Test
public void createWithDefaults() throws Exception {
SpyDefinition definition = new SpyDefinition(null, RealExampleService.class,
null);
assertThat(definition.getName()).isNull();
assertThat(definition.getClassToSpy()).isEqualTo(RealExampleService.class);
assertThat(definition.getReset()).isEqualTo(MockReset.AFTER);
}
@Test
public void createExplicit() throws Exception {
SpyDefinition definition = new SpyDefinition("name", RealExampleService.class,
MockReset.BEFORE);
assertThat(definition.getName()).isEqualTo("name");
assertThat(definition.getClassToSpy()).isEqualTo(RealExampleService.class);
assertThat(definition.getReset()).isEqualTo(MockReset.BEFORE);
}
@Test
public void createSpy() throws Exception {
SpyDefinition definition = new SpyDefinition("name", RealExampleService.class,
MockReset.BEFORE);
RealExampleService spy = definition.createSpy(new RealExampleService("hello"));
MockCreationSettings<?> settings = new MockUtil().getMockSettings(spy);
assertThat(spy).isInstanceOf(ExampleService.class);
assertThat(settings.getMockName().toString()).isEqualTo("name");
assertThat(settings.getDefaultAnswer())
.isEqualTo(Answers.CALLS_REAL_METHODS.get());
assertThat(MockReset.get(spy)).isEqualTo(MockReset.BEFORE);
}
@Test
public void createSpyWhenNullInstanceShouldThrowException() throws Exception {
SpyDefinition definition = new SpyDefinition("name", RealExampleService.class,
MockReset.BEFORE);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Instance must not be null");
definition.createSpy(null);
}
@Test
public void createSpyWhenWrongInstanceShouldThrowException() throws Exception {
SpyDefinition definition = new SpyDefinition("name", RealExampleService.class,
MockReset.BEFORE);
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("must be an instance of");
definition.createSpy(new ExampleServiceCaller(null));
}
@Test
public void createSpyTwice() throws Exception {
SpyDefinition definition = new SpyDefinition("name", RealExampleService.class,
MockReset.BEFORE);
Object instance = new RealExampleService("hello");
instance = definition.createSpy(instance);
instance = definition.createSpy(instance);
}
}
/*
* Copyright 2012-2016 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.example;
/**
* Example service implementation for spy tests.
*
* @author Phillip Webb
*/
public class RealExampleService implements ExampleService {
private final String greeting;
public RealExampleService(String greeting) {
this.greeting = greeting;
}
@Override
public String greeting() {
return this.greeting;
}
}
/*
* Copyright 2012-2016 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.example;
/**
* Example service implementation for spy tests.
*
* @author Phillip Webb
*/
public class SimpleExampleService extends RealExampleService {
public SimpleExampleService() {
super("simple");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment