Remove most deprecated APIs that were due for removal in 4.0
Support for rest controller, controler, and servlet endpoints has been kept for now. Issue: 45600
This commit is contained in:
committed by
Phillip Webb
parent
bf385649ec
commit
a78797a2ba
@@ -110,26 +110,10 @@ import org.springframework.util.CollectionUtils;
|
||||
*/
|
||||
public abstract class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<C>> {
|
||||
|
||||
private static final Class<?>[] NO_ADDITIONAL_CONTEXT_INTERFACES = {};
|
||||
|
||||
private final RunnerConfiguration<C> runnerConfiguration;
|
||||
|
||||
private final Function<RunnerConfiguration<C>, SELF> instanceFactory;
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractApplicationContextRunner} instance.
|
||||
* @param contextFactory the factory used to create the actual context
|
||||
* @param instanceFactory the factory used to create new instance of the runner
|
||||
* @since 2.6.0
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of
|
||||
* {@link #AbstractApplicationContextRunner(Function, Supplier, Class...)}
|
||||
*/
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
protected AbstractApplicationContextRunner(Supplier<C> contextFactory,
|
||||
Function<RunnerConfiguration<C>, SELF> instanceFactory) {
|
||||
this(instanceFactory, contextFactory, NO_ADDITIONAL_CONTEXT_INTERFACES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractApplicationContextRunner} instance.
|
||||
* @param instanceFactory the factory used to create new instance of the runner
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
abstract class Definition {
|
||||
|
||||
private static final int MULTIPLIER = 31;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final MockReset reset;
|
||||
|
||||
private final boolean proxyTargetAware;
|
||||
|
||||
private final QualifierDefinition qualifier;
|
||||
|
||||
Definition(String name, MockReset reset, boolean proxyTargetAware, QualifierDefinition qualifier) {
|
||||
this.name = name;
|
||||
this.reset = (reset != null) ? reset : MockReset.AFTER;
|
||||
this.proxyTargetAware = proxyTargetAware;
|
||||
this.qualifier = qualifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name for bean.
|
||||
* @return the name or {@code null}
|
||||
*/
|
||||
String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the mock reset mode.
|
||||
* @return the reset mode
|
||||
*/
|
||||
MockReset getReset() {
|
||||
return this.reset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if AOP advised beans should be proxy target aware.
|
||||
* @return if proxy target aware
|
||||
*/
|
||||
boolean isProxyTargetAware() {
|
||||
return this.proxyTargetAware;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the qualifier or {@code null}.
|
||||
* @return the qualifier
|
||||
*/
|
||||
QualifierDefinition getQualifier() {
|
||||
return this.qualifier;
|
||||
}
|
||||
|
||||
@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 = result && ObjectUtils.nullSafeEquals(this.name, other.name);
|
||||
result = result && ObjectUtils.nullSafeEquals(this.reset, other.reset);
|
||||
result = result && ObjectUtils.nullSafeEquals(this.proxyTargetAware, other.proxyTargetAware);
|
||||
result = result && ObjectUtils.nullSafeEquals(this.qualifier, other.qualifier);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 1;
|
||||
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.name);
|
||||
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.reset);
|
||||
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.proxyTargetAware);
|
||||
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.qualifier);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parser to create {@link MockDefinition} and {@link SpyDefinition} instances from
|
||||
* {@link MockBean @MockBean} and {@link SpyBean @SpyBean} annotations declared on or in a
|
||||
* class.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class DefinitionsParser {
|
||||
|
||||
private final Set<Definition> definitions;
|
||||
|
||||
private final Map<Definition, Field> definitionFields;
|
||||
|
||||
DefinitionsParser() {
|
||||
this(Collections.emptySet());
|
||||
}
|
||||
|
||||
DefinitionsParser(Collection<? extends Definition> existing) {
|
||||
this.definitions = new LinkedHashSet<>();
|
||||
this.definitionFields = new LinkedHashMap<>();
|
||||
if (existing != null) {
|
||||
this.definitions.addAll(existing);
|
||||
}
|
||||
}
|
||||
|
||||
void parse(Class<?> source) {
|
||||
parseElement(source, null);
|
||||
ReflectionUtils.doWithFields(source, (element) -> parseElement(element, source));
|
||||
}
|
||||
|
||||
private void parseElement(AnnotatedElement element, Class<?> source) {
|
||||
MergedAnnotations annotations = MergedAnnotations.from(element, SearchStrategy.SUPERCLASS);
|
||||
annotations.stream(MockBean.class)
|
||||
.map(MergedAnnotation::synthesize)
|
||||
.forEach((annotation) -> parseMockBeanAnnotation(annotation, element, source));
|
||||
annotations.stream(SpyBean.class)
|
||||
.map(MergedAnnotation::synthesize)
|
||||
.forEach((annotation) -> parseSpyBeanAnnotation(annotation, element, source));
|
||||
}
|
||||
|
||||
private void parseMockBeanAnnotation(MockBean annotation, AnnotatedElement element, Class<?> source) {
|
||||
Set<ResolvableType> typesToMock = getOrDeduceTypes(element, annotation.value(), source);
|
||||
Assert.state(!typesToMock.isEmpty(), () -> "Unable to deduce type to mock from " + element);
|
||||
if (StringUtils.hasLength(annotation.name())) {
|
||||
Assert.state(typesToMock.size() == 1, "The name attribute can only be used when mocking a single class");
|
||||
}
|
||||
for (ResolvableType typeToMock : typesToMock) {
|
||||
MockDefinition definition = new MockDefinition(annotation.name(), typeToMock, annotation.extraInterfaces(),
|
||||
annotation.answer(), annotation.serializable(), annotation.reset(),
|
||||
QualifierDefinition.forElement(element));
|
||||
addDefinition(element, definition, "mock");
|
||||
}
|
||||
}
|
||||
|
||||
private void parseSpyBeanAnnotation(SpyBean annotation, AnnotatedElement element, Class<?> source) {
|
||||
Set<ResolvableType> typesToSpy = getOrDeduceTypes(element, annotation.value(), source);
|
||||
Assert.state(!typesToSpy.isEmpty(), () -> "Unable to deduce type to spy from " + element);
|
||||
if (StringUtils.hasLength(annotation.name())) {
|
||||
Assert.state(typesToSpy.size() == 1, "The name attribute can only be used when spying a single class");
|
||||
}
|
||||
for (ResolvableType typeToSpy : typesToSpy) {
|
||||
SpyDefinition definition = new SpyDefinition(annotation.name(), typeToSpy, annotation.reset(),
|
||||
annotation.proxyTargetAware(), QualifierDefinition.forElement(element));
|
||||
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) {
|
||||
this.definitionFields.put(definition, field);
|
||||
}
|
||||
}
|
||||
|
||||
private Set<ResolvableType> getOrDeduceTypes(AnnotatedElement element, Class<?>[] value, Class<?> source) {
|
||||
Set<ResolvableType> types = new LinkedHashSet<>();
|
||||
for (Class<?> type : value) {
|
||||
types.add(ResolvableType.forClass(type));
|
||||
}
|
||||
if (types.isEmpty() && element instanceof Field field) {
|
||||
types.add((field.getGenericType() instanceof TypeVariable) ? ResolvableType.forField(field, source)
|
||||
: ResolvableType.forField(field));
|
||||
}
|
||||
return types;
|
||||
}
|
||||
|
||||
Set<Definition> getDefinitions() {
|
||||
return Collections.unmodifiableSet(this.definitions);
|
||||
}
|
||||
|
||||
Field getField(Definition definition) {
|
||||
return this.definitionFields.get(definition);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.mockito.Answers;
|
||||
import org.mockito.MockSettings;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* Annotation that can be used to add mocks 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>
|
||||
* Mocks can be registered by type or by {@link #name() bean name}. When registered by
|
||||
* type, any existing single bean of a matching type (including subclasses) in the context
|
||||
* will be replaced by the mock. When registered by name, an existing bean can be
|
||||
* specifically targeted for replacement by a mock. In either case, if no existing bean is
|
||||
* defined a new one will be added. Dependencies that are known to the application context
|
||||
* but are not beans (such as those
|
||||
* {@link org.springframework.beans.factory.config.ConfigurableListableBeanFactory#registerResolvableDependency(Class, Object)
|
||||
* registered directly}) will not be found and a mocked bean will be added to the context
|
||||
* alongside the existing dependency.
|
||||
* <p>
|
||||
* When {@code @MockBean} is used on a field, as well as being registered in the
|
||||
* application context, the mock will also be injected into the field. Typical usage might
|
||||
* be: <pre class="code">
|
||||
* @RunWith(SpringRunner.class)
|
||||
* public class ExampleTests {
|
||||
*
|
||||
* @MockBean
|
||||
* private ExampleService service;
|
||||
*
|
||||
* @Autowired
|
||||
* private UserOfService userOfService;
|
||||
*
|
||||
* @Test
|
||||
* public void testUserOfService() {
|
||||
* given(this.service.greet()).willReturn("Hello");
|
||||
* String actual = this.userOfService.makeUse();
|
||||
* assertEquals("Was: Hello", actual);
|
||||
* }
|
||||
*
|
||||
* @Configuration
|
||||
* @Import(UserOfService.class) // A @Component injected with ExampleService
|
||||
* static class Config {
|
||||
* }
|
||||
*
|
||||
*
|
||||
* }
|
||||
* </pre> If there is more than one bean of the requested type, qualifier metadata must be
|
||||
* specified at field level: <pre class="code">
|
||||
* @RunWith(SpringRunner.class)
|
||||
* public class ExampleTests {
|
||||
*
|
||||
* @MockBean
|
||||
* @Qualifier("example")
|
||||
* private ExampleService service;
|
||||
*
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
* <p>
|
||||
* This annotation is {@code @Repeatable} and may be specified multiple times when working
|
||||
* with Java 8 or contained within an {@link MockBeans @MockBeans} annotation.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
* @see MockitoPostProcessor
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of
|
||||
* {@link org.springframework.test.context.bean.override.mockito.MockitoBean}
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@Target({ ElementType.TYPE, ElementType.FIELD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Repeatable(MockBeans.class)
|
||||
public @interface MockBean {
|
||||
|
||||
/**
|
||||
* The name of the bean to register or replace. If not specified the name will either
|
||||
* be generated or, if the mock replaces an existing bean, the existing name will be
|
||||
* used.
|
||||
* @return the name of the bean
|
||||
*/
|
||||
String name() default "";
|
||||
|
||||
/**
|
||||
* The classes to mock. 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 mock. Each class specified here will result in a mock being created
|
||||
* and registered with the application context. 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 specified attribute consider using the {@code value} alias
|
||||
* instead.
|
||||
* @return the classes to mock
|
||||
*/
|
||||
@AliasFor("value")
|
||||
Class<?>[] classes() default {};
|
||||
|
||||
/**
|
||||
* Any extra interfaces that should also be declared on the mock. See
|
||||
* {@link MockSettings#extraInterfaces(Class...)} for details.
|
||||
* @return any extra interfaces
|
||||
*/
|
||||
Class<?>[] extraInterfaces() default {};
|
||||
|
||||
/**
|
||||
* The {@link Answers} type to use on the mock.
|
||||
* @return the answer type
|
||||
*/
|
||||
Answers answer() default Answers.RETURNS_DEFAULTS;
|
||||
|
||||
/**
|
||||
* If the generated mock is serializable. See {@link MockSettings#serializable()} for
|
||||
* details.
|
||||
* @return if the mock is serializable
|
||||
*/
|
||||
boolean serializable() default false;
|
||||
|
||||
/**
|
||||
* The reset mode to apply to the mock bean. The default is {@link MockReset#AFTER}
|
||||
* meaning that mocks are automatically reset after each test method is invoked.
|
||||
* @return the reset mode
|
||||
*/
|
||||
MockReset reset() default MockReset.AFTER;
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 MockBean @MockBean} annotations.
|
||||
* <p>
|
||||
* Can be used natively, declaring several nested {@link MockBean @MockBean} annotations.
|
||||
* Can also be used in conjunction with Java 8's support for <em>repeatable
|
||||
* annotations</em>, where {@link MockBean @MockBean} 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
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of
|
||||
* {@link org.springframework.test.context.bean.override.mockito.MockitoBean}
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
public @interface MockBeans {
|
||||
|
||||
/**
|
||||
* Return the contained {@link MockBean @MockBean} annotations.
|
||||
* @return the mock beans
|
||||
*/
|
||||
MockBean[] value();
|
||||
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.MockSettings;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* A complete definition that can be used to create a Mockito mock.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockDefinition extends Definition {
|
||||
|
||||
private static final int MULTIPLIER = 31;
|
||||
|
||||
private final ResolvableType typeToMock;
|
||||
|
||||
private final Set<Class<?>> extraInterfaces;
|
||||
|
||||
private final Answers answer;
|
||||
|
||||
private final boolean serializable;
|
||||
|
||||
MockDefinition(String name, ResolvableType typeToMock, Class<?>[] extraInterfaces, Answers answer,
|
||||
boolean serializable, MockReset reset, QualifierDefinition qualifier) {
|
||||
super(name, reset, false, qualifier);
|
||||
Assert.notNull(typeToMock, "'typeToMock' must not be null");
|
||||
this.typeToMock = typeToMock;
|
||||
this.extraInterfaces = asClassSet(extraInterfaces);
|
||||
this.answer = (answer != null) ? answer : Answers.RETURNS_DEFAULTS;
|
||||
this.serializable = serializable;
|
||||
}
|
||||
|
||||
private Set<Class<?>> asClassSet(Class<?>[] classes) {
|
||||
Set<Class<?>> classSet = new LinkedHashSet<>();
|
||||
if (classes != null) {
|
||||
classSet.addAll(Arrays.asList(classes));
|
||||
}
|
||||
return Collections.unmodifiableSet(classSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the type that should be mocked.
|
||||
* @return the type to mock; never {@code null}
|
||||
*/
|
||||
ResolvableType getTypeToMock() {
|
||||
return this.typeToMock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the extra interfaces.
|
||||
* @return the extra interfaces or an empty set
|
||||
*/
|
||||
Set<Class<?>> getExtraInterfaces() {
|
||||
return this.extraInterfaces;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the answers mode.
|
||||
* @return the answers mode; never {@code null}
|
||||
*/
|
||||
Answers getAnswer() {
|
||||
return this.answer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if the mock is serializable.
|
||||
* @return if the mock is serializable
|
||||
*/
|
||||
boolean isSerializable() {
|
||||
return this.serializable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || obj.getClass() != getClass()) {
|
||||
return false;
|
||||
}
|
||||
MockDefinition other = (MockDefinition) obj;
|
||||
boolean result = super.equals(obj);
|
||||
result = result && ObjectUtils.nullSafeEquals(this.typeToMock, other.typeToMock);
|
||||
result = result && ObjectUtils.nullSafeEquals(this.extraInterfaces, other.extraInterfaces);
|
||||
result = result && ObjectUtils.nullSafeEquals(this.answer, other.answer);
|
||||
result = result && this.serializable == other.serializable;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = super.hashCode();
|
||||
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.typeToMock);
|
||||
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.extraInterfaces);
|
||||
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.answer);
|
||||
result = MULTIPLIER * result + Boolean.hashCode(this.serializable);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("name", getName())
|
||||
.append("typeToMock", this.typeToMock)
|
||||
.append("extraInterfaces", this.extraInterfaces)
|
||||
.append("answer", this.answer)
|
||||
.append("serializable", this.serializable)
|
||||
.append("reset", getReset())
|
||||
.toString();
|
||||
}
|
||||
|
||||
<T> T createMock() {
|
||||
return createMock(getName());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<T> T createMock(String name) {
|
||||
MockSettings settings = MockReset.withSettings(getReset());
|
||||
if (StringUtils.hasLength(name)) {
|
||||
settings.name(name);
|
||||
}
|
||||
if (!this.extraInterfaces.isEmpty()) {
|
||||
settings.extraInterfaces(ClassUtils.toClassArray(this.extraInterfaces));
|
||||
}
|
||||
settings.defaultAnswer(this.answer);
|
||||
if (this.serializable) {
|
||||
settings.serializable();
|
||||
}
|
||||
return (T) mock(this.typeToMock.resolve(), settings);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.util.List;
|
||||
|
||||
import org.mockito.MockSettings;
|
||||
import org.mockito.MockingDetails;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.listeners.InvocationListener;
|
||||
import org.mockito.listeners.MethodInvocationReport;
|
||||
import org.mockito.mock.MockCreationSettings;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Reset strategy used on a mock bean. Usually applied to a mock through the
|
||||
* {@link MockBean @MockBean} annotation but can also be directly applied to any mock in
|
||||
* the {@code ApplicationContext} using the static methods.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
* @see ResetMocksTestExecutionListener
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of
|
||||
* {@link org.springframework.test.context.bean.override.mockito.MockReset}
|
||||
*/
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public enum MockReset {
|
||||
|
||||
/**
|
||||
* Reset the mock before the test method runs.
|
||||
*/
|
||||
BEFORE,
|
||||
|
||||
/**
|
||||
* Reset the mock after the test method runs.
|
||||
*/
|
||||
AFTER,
|
||||
|
||||
/**
|
||||
* Don't reset the mock.
|
||||
*/
|
||||
NONE;
|
||||
|
||||
/**
|
||||
* Create {@link MockSettings settings} to be used with mocks where reset should occur
|
||||
* before each test method runs.
|
||||
* @return mock settings
|
||||
*/
|
||||
public static MockSettings before() {
|
||||
return withSettings(BEFORE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link MockSettings settings} to be used with mocks where reset should occur
|
||||
* after each test method runs.
|
||||
* @return mock settings
|
||||
*/
|
||||
public static MockSettings after() {
|
||||
return withSettings(AFTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create {@link MockSettings settings} to be used with mocks where a specific reset
|
||||
* should occur.
|
||||
* @param reset the reset type
|
||||
* @return mock settings
|
||||
*/
|
||||
public static MockSettings withSettings(MockReset reset) {
|
||||
return apply(reset, Mockito.withSettings());
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply {@link MockReset} to existing {@link MockSettings settings}.
|
||||
* @param reset the reset type
|
||||
* @param settings the settings
|
||||
* @return the configured settings
|
||||
*/
|
||||
public static MockSettings apply(MockReset reset, MockSettings settings) {
|
||||
Assert.notNull(settings, "'settings' must not be null");
|
||||
if (reset != null && reset != NONE) {
|
||||
settings.invocationListeners(new ResetInvocationListener(reset));
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link MockReset} associated with the given mock.
|
||||
* @param mock the source mock
|
||||
* @return the reset type (never {@code null})
|
||||
*/
|
||||
static MockReset get(Object mock) {
|
||||
MockReset reset = MockReset.NONE;
|
||||
MockingDetails mockingDetails = Mockito.mockingDetails(mock);
|
||||
if (mockingDetails.isMock()) {
|
||||
MockCreationSettings<?> settings = mockingDetails.getMockCreationSettings();
|
||||
List<InvocationListener> listeners = settings.getInvocationListeners();
|
||||
for (Object listener : listeners) {
|
||||
if (listener instanceof ResetInvocationListener resetInvocationListener) {
|
||||
reset = resetInvocationListener.getReset();
|
||||
}
|
||||
}
|
||||
}
|
||||
return reset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dummy {@link InvocationListener} used to hold the {@link MockReset} value.
|
||||
*/
|
||||
private static class ResetInvocationListener implements InvocationListener {
|
||||
|
||||
private final MockReset reset;
|
||||
|
||||
ResetInvocationListener(MockReset reset) {
|
||||
this.reset = reset;
|
||||
}
|
||||
|
||||
MockReset getReset() {
|
||||
return this.reset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reportInvocation(MethodInvocationReport methodInvocationReport) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Beans created using Mockito.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
class MockitoBeans implements Iterable<Object> {
|
||||
|
||||
private final List<Object> beans = new ArrayList<>();
|
||||
|
||||
void add(Object bean) {
|
||||
this.beans.add(bean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Object> iterator() {
|
||||
return this.beans.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
/**
|
||||
* A {@link ContextCustomizer} to add Mockito support.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockitoContextCustomizer implements ContextCustomizer {
|
||||
|
||||
private final Set<Definition> definitions;
|
||||
|
||||
MockitoContextCustomizer(Set<? extends Definition> definitions) {
|
||||
this.definitions = new LinkedHashSet<>(definitions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customizeContext(ConfigurableApplicationContext context,
|
||||
MergedContextConfiguration mergedContextConfiguration) {
|
||||
if (context instanceof BeanDefinitionRegistry registry) {
|
||||
MockitoPostProcessor.register(registry, this.definitions);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || obj.getClass() != getClass()) {
|
||||
return false;
|
||||
}
|
||||
MockitoContextCustomizer other = (MockitoContextCustomizer) obj;
|
||||
return this.definitions.equals(other.definitions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.definitions.hashCode();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.List;
|
||||
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.ContextCustomizerFactory;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
|
||||
/**
|
||||
* A {@link ContextCustomizerFactory} to add Mockito support.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockitoContextCustomizerFactory implements ContextCustomizerFactory {
|
||||
|
||||
@Override
|
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
||||
List<ContextConfigurationAttributes> configAttributes) {
|
||||
// We gather the explicit mock definitions here since they form part of the
|
||||
// MergedContextConfiguration key. Different mocks need to have a different key.
|
||||
DefinitionsParser parser = new DefinitionsParser();
|
||||
parseDefinitions(testClass, parser);
|
||||
return new MockitoContextCustomizer(parser.getDefinitions());
|
||||
}
|
||||
|
||||
private void parseDefinitions(Class<?> testClass, DefinitionsParser parser) {
|
||||
parser.parse(testClass);
|
||||
if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) {
|
||||
parseDefinitions(testClass.getEnclosingClass(), parser);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,494 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.aop.scope.ScopedProxyUtils;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.PropertyValues;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
||||
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
|
||||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
|
||||
import org.springframework.core.Conventions;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.PriorityOrdered;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link BeanFactoryPostProcessor} used to register and inject
|
||||
* {@link MockBean @MockBeans} with the {@link ApplicationContext}. An initial set of
|
||||
* definitions can be passed to the processor with additional definitions being
|
||||
* automatically created from {@code @Configuration} classes that use
|
||||
* {@link MockBean @MockBean}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
* @author Andreas Neiser
|
||||
* @since 1.4.0
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of Spring Framework's
|
||||
* {@link MockitoBean} and {@link MockitoSpyBean} support
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0")
|
||||
public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor, BeanClassLoaderAware,
|
||||
BeanFactoryAware, BeanFactoryPostProcessor, Ordered {
|
||||
|
||||
private static final String BEAN_NAME = MockitoPostProcessor.class.getName();
|
||||
|
||||
private static final String CONFIGURATION_CLASS_ATTRIBUTE = Conventions
|
||||
.getQualifiedAttributeName(ConfigurationClassPostProcessor.class, "configurationClass");
|
||||
|
||||
private static final BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();
|
||||
|
||||
private final Set<Definition> definitions;
|
||||
|
||||
private ClassLoader classLoader;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
private final MockitoBeans mockitoBeans = new MockitoBeans();
|
||||
|
||||
private final Map<Definition, String> beanNameRegistry = new HashMap<>();
|
||||
|
||||
private final Map<Field, String> fieldRegistry = new HashMap<>();
|
||||
|
||||
private final Map<String, SpyDefinition> spies = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Create a new {@link MockitoPostProcessor} instance with the given initial
|
||||
* definitions.
|
||||
* @param definitions the initial definitions
|
||||
*/
|
||||
public MockitoPostProcessor(Set<Definition> definitions) {
|
||||
this.definitions = definitions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory,
|
||||
"'beanFactory' must be a ConfigurableListableBeanFactory");
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
Assert.isTrue(beanFactory instanceof BeanDefinitionRegistry, "'beanFactory' must be a BeanDefinitionRegistry");
|
||||
postProcessBeanFactory(beanFactory, (BeanDefinitionRegistry) beanFactory);
|
||||
}
|
||||
|
||||
private void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry) {
|
||||
beanFactory.registerSingleton(MockitoBeans.class.getName(), this.mockitoBeans);
|
||||
DefinitionsParser parser = new DefinitionsParser(this.definitions);
|
||||
for (Class<?> configurationClass : getConfigurationClasses(beanFactory)) {
|
||||
parser.parse(configurationClass);
|
||||
}
|
||||
Set<Definition> definitions = parser.getDefinitions();
|
||||
for (Definition definition : definitions) {
|
||||
Field field = parser.getField(definition);
|
||||
register(beanFactory, registry, definition, field);
|
||||
}
|
||||
}
|
||||
|
||||
private Set<Class<?>> getConfigurationClasses(ConfigurableListableBeanFactory beanFactory) {
|
||||
Set<Class<?>> configurationClasses = new LinkedHashSet<>();
|
||||
for (BeanDefinition beanDefinition : getConfigurationBeanDefinitions(beanFactory).values()) {
|
||||
configurationClasses.add(ClassUtils.resolveClassName(beanDefinition.getBeanClassName(), this.classLoader));
|
||||
}
|
||||
return configurationClasses;
|
||||
}
|
||||
|
||||
private Map<String, BeanDefinition> getConfigurationBeanDefinitions(ConfigurableListableBeanFactory beanFactory) {
|
||||
Map<String, BeanDefinition> definitions = new LinkedHashMap<>();
|
||||
for (String beanName : beanFactory.getBeanDefinitionNames()) {
|
||||
BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
|
||||
if (definition.getAttribute(CONFIGURATION_CLASS_ATTRIBUTE) != null) {
|
||||
definitions.put(beanName, definition);
|
||||
}
|
||||
}
|
||||
return definitions;
|
||||
}
|
||||
|
||||
private void register(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry,
|
||||
Definition definition, Field field) {
|
||||
if (definition instanceof MockDefinition mockDefinition) {
|
||||
registerMock(beanFactory, registry, mockDefinition, field);
|
||||
}
|
||||
else if (definition instanceof SpyDefinition spyDefinition) {
|
||||
registerSpy(beanFactory, registry, spyDefinition, field);
|
||||
}
|
||||
}
|
||||
|
||||
private void registerMock(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry,
|
||||
MockDefinition definition, Field field) {
|
||||
RootBeanDefinition beanDefinition = createBeanDefinition(definition);
|
||||
String beanName = getBeanName(beanFactory, registry, definition, beanDefinition);
|
||||
String transformedBeanName = BeanFactoryUtils.transformedBeanName(beanName);
|
||||
if (registry.containsBeanDefinition(transformedBeanName)) {
|
||||
BeanDefinition existing = registry.getBeanDefinition(transformedBeanName);
|
||||
copyBeanDefinitionDetails(existing, beanDefinition);
|
||||
registry.removeBeanDefinition(transformedBeanName);
|
||||
}
|
||||
registry.registerBeanDefinition(transformedBeanName, beanDefinition);
|
||||
Object mock = definition.createMock(beanName + " bean");
|
||||
beanFactory.registerSingleton(transformedBeanName, mock);
|
||||
this.mockitoBeans.add(mock);
|
||||
this.beanNameRegistry.put(definition, beanName);
|
||||
if (field != null) {
|
||||
this.fieldRegistry.put(field, beanName);
|
||||
}
|
||||
}
|
||||
|
||||
private RootBeanDefinition createBeanDefinition(MockDefinition mockDefinition) {
|
||||
RootBeanDefinition definition = new RootBeanDefinition(mockDefinition.getTypeToMock().resolve());
|
||||
definition.setTargetType(mockDefinition.getTypeToMock());
|
||||
if (mockDefinition.getQualifier() != null) {
|
||||
mockDefinition.getQualifier().applyTo(definition);
|
||||
}
|
||||
return definition;
|
||||
}
|
||||
|
||||
private String getBeanName(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry,
|
||||
MockDefinition mockDefinition, RootBeanDefinition beanDefinition) {
|
||||
if (StringUtils.hasLength(mockDefinition.getName())) {
|
||||
return mockDefinition.getName();
|
||||
}
|
||||
Set<String> existingBeans = getExistingBeans(beanFactory, mockDefinition.getTypeToMock(),
|
||||
mockDefinition.getQualifier());
|
||||
if (existingBeans.isEmpty()) {
|
||||
return MockitoPostProcessor.beanNameGenerator.generateBeanName(beanDefinition, registry);
|
||||
}
|
||||
if (existingBeans.size() == 1) {
|
||||
return existingBeans.iterator().next();
|
||||
}
|
||||
String primaryCandidate = determinePrimaryCandidate(registry, existingBeans, mockDefinition.getTypeToMock());
|
||||
if (primaryCandidate != null) {
|
||||
return primaryCandidate;
|
||||
}
|
||||
throw new IllegalStateException("Unable to register mock bean " + mockDefinition.getTypeToMock()
|
||||
+ " expected a single matching bean to replace but found " + existingBeans);
|
||||
}
|
||||
|
||||
private void copyBeanDefinitionDetails(BeanDefinition from, RootBeanDefinition to) {
|
||||
to.setPrimary(from.isPrimary());
|
||||
}
|
||||
|
||||
private void registerSpy(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry,
|
||||
SpyDefinition spyDefinition, Field field) {
|
||||
Set<String> existingBeans = getExistingBeans(beanFactory, spyDefinition.getTypeToSpy(),
|
||||
spyDefinition.getQualifier());
|
||||
if (ObjectUtils.isEmpty(existingBeans)) {
|
||||
createSpy(registry, spyDefinition, field);
|
||||
}
|
||||
else {
|
||||
registerSpies(registry, spyDefinition, field, existingBeans);
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType type,
|
||||
QualifierDefinition qualifier) {
|
||||
Set<String> candidates = new TreeSet<>();
|
||||
for (String candidate : getExistingBeans(beanFactory, type)) {
|
||||
if (qualifier == null || qualifier.matches(beanFactory, candidate)) {
|
||||
candidates.add(candidate);
|
||||
}
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType resolvableType) {
|
||||
Set<String> beans = new LinkedHashSet<>(
|
||||
Arrays.asList(beanFactory.getBeanNamesForType(resolvableType, true, false)));
|
||||
Class<?> type = resolvableType.resolve(Object.class);
|
||||
for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class, true, false)) {
|
||||
beanName = BeanFactoryUtils.transformedBeanName(beanName);
|
||||
Class<?> producedType = beanFactory.getType(beanName, false);
|
||||
if (type.equals(producedType)) {
|
||||
beans.add(beanName);
|
||||
}
|
||||
}
|
||||
beans.removeIf(this::isScopedTarget);
|
||||
return beans;
|
||||
}
|
||||
|
||||
private boolean isScopedTarget(String beanName) {
|
||||
try {
|
||||
return ScopedProxyUtils.isScopedTarget(beanName);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void createSpy(BeanDefinitionRegistry registry, SpyDefinition spyDefinition, Field field) {
|
||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(spyDefinition.getTypeToSpy().resolve());
|
||||
String beanName = MockitoPostProcessor.beanNameGenerator.generateBeanName(beanDefinition, registry);
|
||||
registry.registerBeanDefinition(beanName, beanDefinition);
|
||||
registerSpy(spyDefinition, field, beanName);
|
||||
}
|
||||
|
||||
private void registerSpies(BeanDefinitionRegistry registry, SpyDefinition spyDefinition, Field field,
|
||||
Collection<String> existingBeans) {
|
||||
try {
|
||||
String beanName = determineBeanName(existingBeans, spyDefinition, registry);
|
||||
registerSpy(spyDefinition, field, beanName);
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
throw new IllegalStateException("Unable to register spy bean " + spyDefinition.getTypeToSpy(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private String determineBeanName(Collection<String> existingBeans, SpyDefinition definition,
|
||||
BeanDefinitionRegistry registry) {
|
||||
if (StringUtils.hasText(definition.getName())) {
|
||||
return definition.getName();
|
||||
}
|
||||
if (existingBeans.size() == 1) {
|
||||
return existingBeans.iterator().next();
|
||||
}
|
||||
return determinePrimaryCandidate(registry, existingBeans, definition.getTypeToSpy());
|
||||
}
|
||||
|
||||
private String determinePrimaryCandidate(BeanDefinitionRegistry registry, Collection<String> candidateBeanNames,
|
||||
ResolvableType type) {
|
||||
String primaryBeanName = null;
|
||||
for (String candidateBeanName : candidateBeanNames) {
|
||||
BeanDefinition beanDefinition = registry.getBeanDefinition(candidateBeanName);
|
||||
if (beanDefinition.isPrimary()) {
|
||||
if (primaryBeanName != null) {
|
||||
throw new NoUniqueBeanDefinitionException(type.resolve(), candidateBeanNames.size(),
|
||||
"more than one 'primary' bean found among candidates: "
|
||||
+ Collections.singletonList(candidateBeanNames));
|
||||
}
|
||||
primaryBeanName = candidateBeanName;
|
||||
}
|
||||
}
|
||||
return primaryBeanName;
|
||||
}
|
||||
|
||||
private void registerSpy(SpyDefinition definition, Field field, String beanName) {
|
||||
this.spies.put(beanName, definition);
|
||||
this.beanNameRegistry.put(definition, beanName);
|
||||
if (field != null) {
|
||||
this.fieldRegistry.put(field, beanName);
|
||||
}
|
||||
}
|
||||
|
||||
protected final Object createSpyIfNecessary(Object bean, String beanName) throws BeansException {
|
||||
SpyDefinition definition = this.spies.get(beanName);
|
||||
if (definition != null) {
|
||||
bean = definition.createSpy(beanName, bean);
|
||||
this.mockitoBeans.add(bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
|
||||
throws BeansException {
|
||||
ReflectionUtils.doWithFields(bean.getClass(), (field) -> postProcessField(bean, field));
|
||||
return pvs;
|
||||
}
|
||||
|
||||
private void postProcessField(Object bean, Field field) {
|
||||
String beanName = this.fieldRegistry.get(field);
|
||||
if (StringUtils.hasText(beanName)) {
|
||||
inject(field, bean, beanName);
|
||||
}
|
||||
}
|
||||
|
||||
void inject(Field field, Object target, Definition definition) {
|
||||
String beanName = this.beanNameRegistry.get(definition);
|
||||
Assert.state(StringUtils.hasLength(beanName), () -> "No bean found for definition " + definition);
|
||||
inject(field, target, beanName);
|
||||
}
|
||||
|
||||
private void inject(Field field, Object target, String beanName) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Object existingValue = ReflectionUtils.getField(field, target);
|
||||
Object bean = this.beanFactory.getBean(beanName, field.getType());
|
||||
if (existingValue == bean) {
|
||||
return;
|
||||
}
|
||||
Assert.state(existingValue == null, () -> "The existing value '" + existingValue + "' of field '" + field
|
||||
+ "' is not the same as the new value '" + bean + "'");
|
||||
ReflectionUtils.setField(field, target, bean);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new BeanCreationException("Could not inject field: " + field, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.LOWEST_PRECEDENCE - 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the processor with a {@link BeanDefinitionRegistry}. Not required when
|
||||
* using the {@link SpringRunner} as registration is automatic.
|
||||
* @param registry the bean definition registry
|
||||
*/
|
||||
public static void register(BeanDefinitionRegistry registry) {
|
||||
register(registry, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the processor with a {@link BeanDefinitionRegistry}. Not required when
|
||||
* using the {@link SpringRunner} as registration is automatic.
|
||||
* @param registry the bean definition registry
|
||||
* @param definitions the initial mock/spy definitions
|
||||
*/
|
||||
public static void register(BeanDefinitionRegistry registry, Set<Definition> definitions) {
|
||||
register(registry, MockitoPostProcessor.class, definitions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the processor with a {@link BeanDefinitionRegistry}. Not required when
|
||||
* using the {@link SpringRunner} as registration is automatic.
|
||||
* @param registry the bean definition registry
|
||||
* @param postProcessor the post processor class to register
|
||||
* @param definitions the initial mock/spy definitions
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void register(BeanDefinitionRegistry registry, Class<? extends MockitoPostProcessor> postProcessor,
|
||||
Set<Definition> definitions) {
|
||||
SpyPostProcessor.register(registry);
|
||||
BeanDefinition definition = getOrAddBeanDefinition(registry, postProcessor);
|
||||
ValueHolder constructorArg = definition.getConstructorArgumentValues().getIndexedArgumentValue(0, Set.class);
|
||||
Set<Definition> existing = (Set<Definition>) constructorArg.getValue();
|
||||
if (definitions != null) {
|
||||
existing.addAll(definitions);
|
||||
}
|
||||
}
|
||||
|
||||
private static BeanDefinition getOrAddBeanDefinition(BeanDefinitionRegistry registry,
|
||||
Class<? extends MockitoPostProcessor> postProcessor) {
|
||||
if (!registry.containsBeanDefinition(BEAN_NAME)) {
|
||||
RootBeanDefinition definition = new RootBeanDefinition(postProcessor);
|
||||
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
|
||||
constructorArguments.addIndexedArgumentValue(0, new LinkedHashSet<>());
|
||||
registry.registerBeanDefinition(BEAN_NAME, definition);
|
||||
return definition;
|
||||
}
|
||||
return registry.getBeanDefinition(BEAN_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link BeanPostProcessor} to handle {@link SpyBean} definitions. Registered as a
|
||||
* separate processor so that it can be ordered above AOP post processors.
|
||||
*/
|
||||
static class SpyPostProcessor implements SmartInstantiationAwareBeanPostProcessor, PriorityOrdered {
|
||||
|
||||
private static final String BEAN_NAME = SpyPostProcessor.class.getName();
|
||||
|
||||
private final Map<String, Object> earlySpyReferences = new ConcurrentHashMap<>(16);
|
||||
|
||||
private final MockitoPostProcessor mockitoPostProcessor;
|
||||
|
||||
SpyPostProcessor(MockitoPostProcessor mockitoPostProcessor) {
|
||||
this.mockitoPostProcessor = mockitoPostProcessor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.HIGHEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof FactoryBean) {
|
||||
return bean;
|
||||
}
|
||||
this.earlySpyReferences.put(getCacheKey(bean, beanName), bean);
|
||||
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof FactoryBean) {
|
||||
return bean;
|
||||
}
|
||||
if (this.earlySpyReferences.remove(getCacheKey(bean, beanName)) != bean) {
|
||||
return this.mockitoPostProcessor.createSpyIfNecessary(bean, beanName);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
private String getCacheKey(Object bean, String beanName) {
|
||||
return StringUtils.hasLength(beanName) ? beanName : bean.getClass().getName();
|
||||
}
|
||||
|
||||
static void register(BeanDefinitionRegistry registry) {
|
||||
if (!registry.containsBeanDefinition(BEAN_NAME)) {
|
||||
RootBeanDefinition definition = new RootBeanDefinition(SpyPostProcessor.class);
|
||||
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
|
||||
constructorArguments.addIndexedArgumentValue(0,
|
||||
new RuntimeBeanReference(MockitoPostProcessor.BEAN_NAME));
|
||||
registry.registerBeanDefinition(BEAN_NAME, definition);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,177 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.ReflectionUtils.FieldCallback;
|
||||
|
||||
/**
|
||||
* {@link TestExecutionListener} to enable {@link MockBean @MockBean} and
|
||||
* {@link SpyBean @SpyBean} support. Also triggers
|
||||
* {@link MockitoAnnotations#openMocks(Object)} when any Mockito annotations used,
|
||||
* primarily to allow {@link Captor @Captor} annotations.
|
||||
* <p>
|
||||
* To use the automatic reset support of {@code @MockBean} and {@code @SpyBean}, configure
|
||||
* {@link ResetMocksTestExecutionListener} as well.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Moritz Halbritter
|
||||
* @since 1.4.2
|
||||
* @see ResetMocksTestExecutionListener
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of Spring Framework's support for
|
||||
* {@link MockitoBean} and {@link MockitoSpyBean}.
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class MockitoTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
private static final String MOCKS_ATTRIBUTE_NAME = MockitoTestExecutionListener.class.getName() + ".mocks";
|
||||
|
||||
@Override
|
||||
public final int getOrder() {
|
||||
return 1950;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareTestInstance(TestContext testContext) throws Exception {
|
||||
closeMocks(testContext);
|
||||
initMocks(testContext);
|
||||
injectFields(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
if (Boolean.TRUE.equals(
|
||||
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
|
||||
closeMocks(testContext);
|
||||
initMocks(testContext);
|
||||
reinjectFields(testContext);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
closeMocks(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestClass(TestContext testContext) throws Exception {
|
||||
closeMocks(testContext);
|
||||
}
|
||||
|
||||
private void initMocks(TestContext testContext) {
|
||||
if (hasMockitoAnnotations(testContext)) {
|
||||
testContext.setAttribute(MOCKS_ATTRIBUTE_NAME, MockitoAnnotations.openMocks(testContext.getTestInstance()));
|
||||
}
|
||||
}
|
||||
|
||||
private void closeMocks(TestContext testContext) throws Exception {
|
||||
Object mocks = testContext.getAttribute(MOCKS_ATTRIBUTE_NAME);
|
||||
if (mocks instanceof AutoCloseable closeable) {
|
||||
closeable.close();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasMockitoAnnotations(TestContext testContext) {
|
||||
MockitoAnnotationCollection collector = new MockitoAnnotationCollection();
|
||||
ReflectionUtils.doWithFields(testContext.getTestClass(), collector);
|
||||
return collector.hasAnnotations();
|
||||
}
|
||||
|
||||
private void injectFields(TestContext testContext) {
|
||||
postProcessFields(testContext, (mockitoField, postProcessor) -> postProcessor.inject(mockitoField.field,
|
||||
mockitoField.target, mockitoField.definition));
|
||||
}
|
||||
|
||||
private void reinjectFields(final TestContext testContext) {
|
||||
postProcessFields(testContext, (mockitoField, postProcessor) -> {
|
||||
ReflectionUtils.makeAccessible(mockitoField.field);
|
||||
ReflectionUtils.setField(mockitoField.field, testContext.getTestInstance(), null);
|
||||
postProcessor.inject(mockitoField.field, mockitoField.target, mockitoField.definition);
|
||||
});
|
||||
}
|
||||
|
||||
private void postProcessFields(TestContext testContext, BiConsumer<MockitoField, MockitoPostProcessor> consumer) {
|
||||
DefinitionsParser parser = new DefinitionsParser();
|
||||
parser.parse(testContext.getTestClass());
|
||||
if (!parser.getDefinitions().isEmpty()) {
|
||||
MockitoPostProcessor postProcessor = testContext.getApplicationContext()
|
||||
.getBean(MockitoPostProcessor.class);
|
||||
for (Definition definition : parser.getDefinitions()) {
|
||||
Field field = parser.getField(definition);
|
||||
if (field != null) {
|
||||
consumer.accept(new MockitoField(field, testContext.getTestInstance(), definition), postProcessor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link FieldCallback} to collect Mockito annotations.
|
||||
*/
|
||||
private static final class MockitoAnnotationCollection implements FieldCallback {
|
||||
|
||||
private final Set<Annotation> annotations = new LinkedHashSet<>();
|
||||
|
||||
@Override
|
||||
public void doWith(Field field) throws IllegalArgumentException {
|
||||
for (Annotation annotation : field.getDeclaredAnnotations()) {
|
||||
if (annotation.annotationType().getName().startsWith("org.mockito")) {
|
||||
this.annotations.add(annotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean hasAnnotations() {
|
||||
return !this.annotations.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final class MockitoField {
|
||||
|
||||
private final Field field;
|
||||
|
||||
private final Object target;
|
||||
|
||||
private final Definition definition;
|
||||
|
||||
private MockitoField(Field field, Object instance, Definition definition) {
|
||||
this.field = field;
|
||||
this.target = instance;
|
||||
this.definition = definition;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
|
||||
/**
|
||||
* Definition of a Spring {@link Qualifier @Qualifier}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @see Definition
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class QualifierDefinition {
|
||||
|
||||
private final Field field;
|
||||
|
||||
private final DependencyDescriptor descriptor;
|
||||
|
||||
private final Set<Annotation> annotations;
|
||||
|
||||
QualifierDefinition(Field field, Set<Annotation> annotations) {
|
||||
// We can't use the field or descriptor as part of the context key
|
||||
// but we can assume that if two fields have the same qualifiers then
|
||||
// it's safe for Spring to use either for qualifier logic
|
||||
this.field = field;
|
||||
this.descriptor = new DependencyDescriptor(field, true);
|
||||
this.annotations = annotations;
|
||||
}
|
||||
|
||||
boolean matches(ConfigurableListableBeanFactory beanFactory, String beanName) {
|
||||
return beanFactory.isAutowireCandidate(beanName, this.descriptor);
|
||||
}
|
||||
|
||||
void applyTo(RootBeanDefinition definition) {
|
||||
definition.setQualifiedElement(this.field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null || !getClass().isAssignableFrom(obj.getClass())) {
|
||||
return false;
|
||||
}
|
||||
QualifierDefinition other = (QualifierDefinition) obj;
|
||||
return this.annotations.equals(other.annotations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.annotations.hashCode();
|
||||
}
|
||||
|
||||
static QualifierDefinition forElement(AnnotatedElement element) {
|
||||
if (element instanceof Field field) {
|
||||
Set<Annotation> annotations = getQualifierAnnotations(field);
|
||||
if (!annotations.isEmpty()) {
|
||||
return new QualifierDefinition(field, annotations);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Set<Annotation> getQualifierAnnotations(Field field) {
|
||||
// Assume that any annotations other than @MockBean/@SpyBean are qualifiers
|
||||
Annotation[] candidates = field.getDeclaredAnnotations();
|
||||
Set<Annotation> annotations = new HashSet<>(candidates.length);
|
||||
for (Annotation candidate : candidates) {
|
||||
if (!isMockOrSpyAnnotation(candidate.annotationType())) {
|
||||
annotations.add(candidate);
|
||||
}
|
||||
}
|
||||
return annotations;
|
||||
}
|
||||
|
||||
private static boolean isMockOrSpyAnnotation(Class<? extends Annotation> type) {
|
||||
if (type.equals(MockBean.class) || type.equals(SpyBean.class)) {
|
||||
return true;
|
||||
}
|
||||
MergedAnnotations metaAnnotations = MergedAnnotations.from(type);
|
||||
return metaAnnotations.isPresent(MockBean.class) || metaAnnotations.isPresent(SpyBean.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.NativeDetector;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* {@link TestExecutionListener} to reset any mock beans that have been marked with a
|
||||
* {@link MockReset}. Typically used alongside {@link MockitoTestExecutionListener}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
* @see MockitoTestExecutionListener
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of
|
||||
* {@link org.springframework.test.context.bean.override.mockito.MockitoResetTestExecutionListener}
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class ResetMocksTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
private static final boolean MOCKITO_IS_PRESENT = ClassUtils.isPresent("org.mockito.MockSettings",
|
||||
ResetMocksTestExecutionListener.class.getClassLoader());
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.LOWEST_PRECEDENCE - 100;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
if (MOCKITO_IS_PRESENT && !NativeDetector.inNativeImage()) {
|
||||
resetMocks(testContext.getApplicationContext(), MockReset.BEFORE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
if (MOCKITO_IS_PRESENT && !NativeDetector.inNativeImage()) {
|
||||
resetMocks(testContext.getApplicationContext(), MockReset.AFTER);
|
||||
}
|
||||
}
|
||||
|
||||
private void resetMocks(ApplicationContext applicationContext, MockReset reset) {
|
||||
if (applicationContext instanceof ConfigurableApplicationContext configurableContext) {
|
||||
resetMocks(configurableContext, reset);
|
||||
}
|
||||
}
|
||||
|
||||
private void resetMocks(ConfigurableApplicationContext applicationContext, MockReset reset) {
|
||||
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
|
||||
String[] names = beanFactory.getBeanDefinitionNames();
|
||||
Set<String> instantiatedSingletons = new HashSet<>(Arrays.asList(beanFactory.getSingletonNames()));
|
||||
for (String name : names) {
|
||||
BeanDefinition definition = beanFactory.getBeanDefinition(name);
|
||||
if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
|
||||
Object bean = getBean(beanFactory, name);
|
||||
if (bean != null && reset.equals(MockReset.get(bean))) {
|
||||
Mockito.reset(bean);
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
MockitoBeans mockedBeans = beanFactory.getBean(MockitoBeans.class);
|
||||
for (Object mockedBean : mockedBeans) {
|
||||
if (reset.equals(MockReset.get(mockedBean))) {
|
||||
Mockito.reset(mockedBean);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (NoSuchBeanDefinitionException ex) {
|
||||
// Continue
|
||||
}
|
||||
if (applicationContext.getParent() != null) {
|
||||
resetMocks(applicationContext.getParent(), reset);
|
||||
}
|
||||
}
|
||||
|
||||
private Object getBean(ConfigurableListableBeanFactory beanFactory, String name) {
|
||||
try {
|
||||
if (isStandardBeanOrSingletonFactoryBean(beanFactory, name)) {
|
||||
return beanFactory.getBean(name);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Continue
|
||||
}
|
||||
return beanFactory.getSingleton(name);
|
||||
}
|
||||
|
||||
private boolean isStandardBeanOrSingletonFactoryBean(ConfigurableListableBeanFactory beanFactory, String name) {
|
||||
String factoryBeanName = BeanFactory.FACTORY_BEAN_PREFIX + name;
|
||||
if (beanFactory.containsBean(factoryBeanName)) {
|
||||
FactoryBean<?> factoryBean = (FactoryBean<?>) beanFactory.getBean(factoryBeanName);
|
||||
return factoryBean.isSingleton();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.plugins.MockResolver;
|
||||
|
||||
import org.springframework.aop.TargetSource;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link MockResolver} for testing Spring Boot applications with Mockito. It resolves
|
||||
* mocks by walking the proxy chain until the target or a non-static proxy is found.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 2.4.0
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of Spring Framework's
|
||||
* {@link MockitoBean} and {@link MockitoSpyBean}
|
||||
*/
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class SpringBootMockResolver implements MockResolver {
|
||||
|
||||
@Override
|
||||
public Object resolve(Object instance) {
|
||||
return getUltimateTargetObject(instance);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T getUltimateTargetObject(Object candidate) {
|
||||
Assert.notNull(candidate, "'candidate' must not be null");
|
||||
try {
|
||||
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) {
|
||||
TargetSource targetSource = advised.getTargetSource();
|
||||
if (targetSource.isStatic()) {
|
||||
Object target = targetSource.getTarget();
|
||||
if (target != null) {
|
||||
return getUltimateTargetObject(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new IllegalStateException("Failed to unwrap proxied object", ex);
|
||||
}
|
||||
return (T) candidate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.mockito.Mockito;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* Annotation that can be used to apply Mockito 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 a matching type (including subclasses) will be wrapped with the spy. If no existing
|
||||
* bean is defined a new one will be added. Dependencies that are known to the application
|
||||
* context but are not beans (such as those
|
||||
* {@link org.springframework.beans.factory.config.ConfigurableListableBeanFactory#registerResolvableDependency(Class, Object)
|
||||
* registered directly}) will not be found and a spied bean will be added to the context
|
||||
* alongside the existing dependency.
|
||||
* <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">
|
||||
* @RunWith(SpringRunner.class)
|
||||
* public class ExampleTests {
|
||||
*
|
||||
* @SpyBean
|
||||
* private ExampleService service;
|
||||
*
|
||||
* @Autowired
|
||||
* private UserOfService userOfService;
|
||||
*
|
||||
* @Test
|
||||
* public void testUserOfService() {
|
||||
* String actual = this.userOfService.makeUse();
|
||||
* assertEquals("Was: Hello", actual);
|
||||
* verify(this.service).greet();
|
||||
* }
|
||||
*
|
||||
* @Configuration
|
||||
* @Import(UserOfService.class) // A @Component injected with ExampleService
|
||||
* static class Config {
|
||||
* }
|
||||
*
|
||||
*
|
||||
* }
|
||||
* </pre> If there is more than one bean of the requested type, qualifier metadata must be
|
||||
* specified at field level: <pre class="code">
|
||||
* @RunWith(SpringRunner.class)
|
||||
* public class ExampleTests {
|
||||
*
|
||||
* @SpyBean
|
||||
* @Qualifier("example")
|
||||
* private ExampleService service;
|
||||
*
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
* <p>
|
||||
* This annotation is {@code @Repeatable} and may be specified multiple times when working
|
||||
* with Java 8 or contained within a {@link SpyBeans @SpyBeans} annotation.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
* @see MockitoPostProcessor
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of
|
||||
* {@link org.springframework.test.context.bean.override.mockito.MockitoSpyBean}
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@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 spy
|
||||
*/
|
||||
@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 @SpyBean} also defines a {@code name} this attribute can only contain a
|
||||
* single value.
|
||||
* <p>
|
||||
* If this is the only specified attribute consider using the {@code value} alias
|
||||
* instead.
|
||||
* @return the classes to spy
|
||||
*/
|
||||
@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;
|
||||
|
||||
/**
|
||||
* Indicates that Mockito methods such as {@link Mockito#verify(Object) verify(mock)}
|
||||
* should use the {@code target} of AOP advised beans, rather than the proxy itself.
|
||||
* If set to {@code false} you may need to use the result of
|
||||
* {@link org.springframework.test.util.AopTestUtils#getUltimateTargetObject(Object)
|
||||
* AopTestUtils.getUltimateTargetObject(...)} when calling Mockito methods.
|
||||
* @return {@code true} if the target of AOP advised beans is used or {@code false} if
|
||||
* the proxy is used directly
|
||||
*/
|
||||
boolean proxyTargetAware() default true;
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 @SpyBean} annotations.
|
||||
* <p>
|
||||
* Can be used natively, declaring several nested {@link SpyBean @SpyBean} annotations.
|
||||
* Can also be used in conjunction with Java 8's support for <em>repeatable
|
||||
* annotations</em>, where {@link SpyBean @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
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0 in favor of
|
||||
* {@link org.springframework.test.context.bean.override.mockito.MockitoSpyBean}
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
public @interface SpyBeans {
|
||||
|
||||
/**
|
||||
* Return the contained {@link SpyBean @SpyBean} annotations.
|
||||
* @return the spy beans
|
||||
*/
|
||||
SpyBean[] value();
|
||||
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.reflect.Proxy;
|
||||
|
||||
import org.mockito.AdditionalAnswers;
|
||||
import org.mockito.MockSettings;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.listeners.VerificationStartedEvent;
|
||||
import org.mockito.listeners.VerificationStartedListener;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.test.util.AopTestUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* A complete definition that can be used to create a Mockito spy.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class SpyDefinition extends Definition {
|
||||
|
||||
private static final int MULTIPLIER = 31;
|
||||
|
||||
private final ResolvableType typeToSpy;
|
||||
|
||||
SpyDefinition(String name, ResolvableType typeToSpy, MockReset reset, boolean proxyTargetAware,
|
||||
QualifierDefinition qualifier) {
|
||||
super(name, reset, proxyTargetAware, qualifier);
|
||||
Assert.notNull(typeToSpy, "'typeToSpy' must not be null");
|
||||
this.typeToSpy = typeToSpy;
|
||||
|
||||
}
|
||||
|
||||
ResolvableType getTypeToSpy() {
|
||||
return this.typeToSpy;
|
||||
}
|
||||
|
||||
@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 = result && ObjectUtils.nullSafeEquals(this.typeToSpy, other.typeToSpy);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = super.hashCode();
|
||||
result = MULTIPLIER * result + ObjectUtils.nullSafeHashCode(this.typeToSpy);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new ToStringCreator(this).append("name", getName())
|
||||
.append("typeToSpy", this.typeToSpy)
|
||||
.append("reset", getReset())
|
||||
.toString();
|
||||
}
|
||||
|
||||
<T> T createSpy(Object instance) {
|
||||
return createSpy(getName(), instance);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<T> T createSpy(String name, Object instance) {
|
||||
Assert.notNull(instance, "'instance' must not be null");
|
||||
Assert.isInstanceOf(this.typeToSpy.resolve(), instance);
|
||||
if (Mockito.mockingDetails(instance).isSpy()) {
|
||||
return (T) instance;
|
||||
}
|
||||
MockSettings settings = MockReset.withSettings(getReset());
|
||||
if (StringUtils.hasLength(name)) {
|
||||
settings.name(name);
|
||||
}
|
||||
if (isProxyTargetAware()) {
|
||||
settings.verificationStartedListeners(new SpringAopBypassingVerificationStartedListener());
|
||||
}
|
||||
Class<?> toSpy;
|
||||
if (Proxy.isProxyClass(instance.getClass())) {
|
||||
settings.defaultAnswer(AdditionalAnswers.delegatesTo(instance));
|
||||
toSpy = this.typeToSpy.toClass();
|
||||
}
|
||||
else {
|
||||
settings.defaultAnswer(Mockito.CALLS_REAL_METHODS);
|
||||
settings.spiedInstance(instance);
|
||||
toSpy = instance.getClass();
|
||||
}
|
||||
return (T) mock(toSpy, settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link VerificationStartedListener} that bypasses any proxy created by Spring AOP
|
||||
* when the verification of a spy starts.
|
||||
*/
|
||||
private static final class SpringAopBypassingVerificationStartedListener implements VerificationStartedListener {
|
||||
|
||||
@Override
|
||||
public void onVerificationStarted(VerificationStartedEvent event) {
|
||||
event.setMock(AopTestUtils.getUltimateTargetObject(event.getMock()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mockito integration for Spring Boot tests.
|
||||
* <p>
|
||||
* Deprecated since 3.4.0 for removal in 4.0.0 in favor of Spring Framework's
|
||||
* {@link org.springframework.test.context.bean.override.mockito.MockitoBean} and
|
||||
* {@link org.springframework.test.context.bean.override.mockito.MockitoSpyBean}
|
||||
*/
|
||||
package org.springframework.boot.test.mock.mockito;
|
||||
@@ -2,13 +2,7 @@
|
||||
org.springframework.test.context.ContextCustomizerFactory=\
|
||||
org.springframework.boot.test.context.ImportsContextCustomizerFactory,\
|
||||
org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizerFactory,\
|
||||
org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory,\
|
||||
org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory
|
||||
|
||||
# Test Execution Listeners
|
||||
org.springframework.test.context.TestExecutionListener=\
|
||||
org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener,\
|
||||
org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener
|
||||
org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory
|
||||
|
||||
# Application Context Initializers
|
||||
org.springframework.context.ApplicationContextInitializer=\
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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;
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link AbstractMockBeanOnGenericTests}.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class AbstractMockBeanOnGenericExtensionTests extends
|
||||
AbstractMockBeanOnGenericTests<AbstractMockBeanOnGenericTests.ThingImpl, AbstractMockBeanOnGenericTests.SomethingImpl> {
|
||||
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockBean} with abstract class and generics.
|
||||
*
|
||||
* @param <T> type of thing
|
||||
* @param <U> type of something
|
||||
* @author Madhura Bhave
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@SpringBootTest(classes = AbstractMockBeanOnGenericTests.TestConfiguration.class)
|
||||
abstract class AbstractMockBeanOnGenericTests<T extends AbstractMockBeanOnGenericTests.Thing<U>, U extends AbstractMockBeanOnGenericTests.Something> {
|
||||
|
||||
@Autowired
|
||||
@SuppressWarnings("unused")
|
||||
private T thing;
|
||||
|
||||
@MockBean
|
||||
private U something;
|
||||
|
||||
@Test
|
||||
void mockBeanShouldResolveConcreteType() {
|
||||
assertThat(this.something).isInstanceOf(SomethingImpl.class);
|
||||
}
|
||||
|
||||
abstract static class Thing<T extends AbstractMockBeanOnGenericTests.Something> {
|
||||
|
||||
@Autowired
|
||||
private T something;
|
||||
|
||||
T getSomething() {
|
||||
return this.something;
|
||||
}
|
||||
|
||||
void setSomething(T something) {
|
||||
this.something = something;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class SomethingImpl extends Something {
|
||||
|
||||
}
|
||||
|
||||
static class ThingImpl extends Thing<SomethingImpl> {
|
||||
|
||||
}
|
||||
|
||||
static class Something {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
ThingImpl thing() {
|
||||
return new ThingImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,303 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Answers;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleExtraInterface;
|
||||
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.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefinitionsParser}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class DefinitionsParserTests {
|
||||
|
||||
private final DefinitionsParser parser = new DefinitionsParser();
|
||||
|
||||
@Test
|
||||
void parseSingleMockBean() {
|
||||
this.parser.parse(SingleMockBean.class);
|
||||
assertThat(getDefinitions()).hasSize(1);
|
||||
assertThat(getMockDefinition(0).getTypeToMock().resolve()).isEqualTo(ExampleService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseRepeatMockBean() {
|
||||
this.parser.parse(RepeatMockBean.class);
|
||||
assertThat(getDefinitions()).hasSize(2);
|
||||
assertThat(getMockDefinition(0).getTypeToMock().resolve()).isEqualTo(ExampleService.class);
|
||||
assertThat(getMockDefinition(1).getTypeToMock().resolve()).isEqualTo(ExampleServiceCaller.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseMockBeanAttributes() {
|
||||
this.parser.parse(MockBeanAttributes.class);
|
||||
assertThat(getDefinitions()).hasSize(1);
|
||||
MockDefinition definition = getMockDefinition(0);
|
||||
assertThat(definition.getName()).isEqualTo("Name");
|
||||
assertThat(definition.getTypeToMock().resolve()).isEqualTo(ExampleService.class);
|
||||
assertThat(definition.getExtraInterfaces()).containsExactly(ExampleExtraInterface.class);
|
||||
assertThat(definition.getAnswer()).isEqualTo(Answers.RETURNS_SMART_NULLS);
|
||||
assertThat(definition.isSerializable()).isTrue();
|
||||
assertThat(definition.getReset()).isEqualTo(MockReset.NONE);
|
||||
assertThat(definition.getQualifier()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseMockBeanOnClassAndField() {
|
||||
this.parser.parse(MockBeanOnClassAndField.class);
|
||||
assertThat(getDefinitions()).hasSize(2);
|
||||
MockDefinition classDefinition = getMockDefinition(0);
|
||||
assertThat(classDefinition.getTypeToMock().resolve()).isEqualTo(ExampleService.class);
|
||||
assertThat(classDefinition.getQualifier()).isNull();
|
||||
MockDefinition fieldDefinition = getMockDefinition(1);
|
||||
assertThat(fieldDefinition.getTypeToMock().resolve()).isEqualTo(ExampleServiceCaller.class);
|
||||
QualifierDefinition qualifier = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(MockBeanOnClassAndField.class, "caller"));
|
||||
assertThat(fieldDefinition.getQualifier()).isNotNull().isEqualTo(qualifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseMockBeanInferClassToMock() {
|
||||
this.parser.parse(MockBeanInferClassToMock.class);
|
||||
assertThat(getDefinitions()).hasSize(1);
|
||||
assertThat(getMockDefinition(0).getTypeToMock().resolve()).isEqualTo(ExampleService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseMockBeanMissingClassToMock() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.parser.parse(MockBeanMissingClassToMock.class))
|
||||
.withMessageContaining("Unable to deduce type to mock");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseMockBeanMultipleClasses() {
|
||||
this.parser.parse(MockBeanMultipleClasses.class);
|
||||
assertThat(getDefinitions()).hasSize(2);
|
||||
assertThat(getMockDefinition(0).getTypeToMock().resolve()).isEqualTo(ExampleService.class);
|
||||
assertThat(getMockDefinition(1).getTypeToMock().resolve()).isEqualTo(ExampleServiceCaller.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseMockBeanMultipleClassesWithName() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.parser.parse(MockBeanMultipleClassesWithName.class))
|
||||
.withMessageContaining("The name attribute can only be used when mocking a single class");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseSingleSpyBean() {
|
||||
this.parser.parse(SingleSpyBean.class);
|
||||
assertThat(getDefinitions()).hasSize(1);
|
||||
assertThat(getSpyDefinition(0).getTypeToSpy().resolve()).isEqualTo(RealExampleService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseRepeatSpyBean() {
|
||||
this.parser.parse(RepeatSpyBean.class);
|
||||
assertThat(getDefinitions()).hasSize(2);
|
||||
assertThat(getSpyDefinition(0).getTypeToSpy().resolve()).isEqualTo(RealExampleService.class);
|
||||
assertThat(getSpyDefinition(1).getTypeToSpy().resolve()).isEqualTo(ExampleServiceCaller.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseSpyBeanAttributes() {
|
||||
this.parser.parse(SpyBeanAttributes.class);
|
||||
assertThat(getDefinitions()).hasSize(1);
|
||||
SpyDefinition definition = getSpyDefinition(0);
|
||||
assertThat(definition.getName()).isEqualTo("Name");
|
||||
assertThat(definition.getTypeToSpy().resolve()).isEqualTo(RealExampleService.class);
|
||||
assertThat(definition.getReset()).isEqualTo(MockReset.NONE);
|
||||
assertThat(definition.getQualifier()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseSpyBeanOnClassAndField() {
|
||||
this.parser.parse(SpyBeanOnClassAndField.class);
|
||||
assertThat(getDefinitions()).hasSize(2);
|
||||
SpyDefinition classDefinition = getSpyDefinition(0);
|
||||
assertThat(classDefinition.getQualifier()).isNull();
|
||||
assertThat(classDefinition.getTypeToSpy().resolve()).isEqualTo(RealExampleService.class);
|
||||
SpyDefinition fieldDefinition = getSpyDefinition(1);
|
||||
QualifierDefinition qualifier = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(SpyBeanOnClassAndField.class, "caller"));
|
||||
assertThat(fieldDefinition.getQualifier()).isNotNull().isEqualTo(qualifier);
|
||||
assertThat(fieldDefinition.getTypeToSpy().resolve()).isEqualTo(ExampleServiceCaller.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseSpyBeanInferClassToMock() {
|
||||
this.parser.parse(SpyBeanInferClassToMock.class);
|
||||
assertThat(getDefinitions()).hasSize(1);
|
||||
assertThat(getSpyDefinition(0).getTypeToSpy().resolve()).isEqualTo(RealExampleService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseSpyBeanMissingClassToMock() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.parser.parse(SpyBeanMissingClassToMock.class))
|
||||
.withMessageContaining("Unable to deduce type to spy");
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseSpyBeanMultipleClasses() {
|
||||
this.parser.parse(SpyBeanMultipleClasses.class);
|
||||
assertThat(getDefinitions()).hasSize(2);
|
||||
assertThat(getSpyDefinition(0).getTypeToSpy().resolve()).isEqualTo(RealExampleService.class);
|
||||
assertThat(getSpyDefinition(1).getTypeToSpy().resolve()).isEqualTo(ExampleServiceCaller.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseSpyBeanMultipleClassesWithName() {
|
||||
assertThatIllegalStateException().isThrownBy(() -> this.parser.parse(SpyBeanMultipleClassesWithName.class))
|
||||
.withMessageContaining("The name attribute can only be used when spying a single class");
|
||||
}
|
||||
|
||||
private MockDefinition getMockDefinition(int index) {
|
||||
return (MockDefinition) getDefinitions().get(index);
|
||||
}
|
||||
|
||||
private SpyDefinition getSpyDefinition(int index) {
|
||||
return (SpyDefinition) getDefinitions().get(index);
|
||||
}
|
||||
|
||||
private List<Definition> getDefinitions() {
|
||||
return new ArrayList<>(this.parser.getDefinitions());
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean(ExampleService.class)
|
||||
static class SingleMockBean {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBeans({ @MockBean(ExampleService.class), @MockBean(ExampleServiceCaller.class) })
|
||||
static class RepeatMockBean {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean(name = "Name", classes = ExampleService.class, extraInterfaces = ExampleExtraInterface.class,
|
||||
answer = Answers.RETURNS_SMART_NULLS, serializable = true, reset = MockReset.NONE)
|
||||
static class MockBeanAttributes {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean(ExampleService.class)
|
||||
static class MockBeanOnClassAndField {
|
||||
|
||||
@MockBean(ExampleServiceCaller.class)
|
||||
@Qualifier("test")
|
||||
private Object caller;
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean({ ExampleService.class, ExampleServiceCaller.class })
|
||||
static class MockBeanMultipleClasses {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean(name = "name", classes = { ExampleService.class, ExampleServiceCaller.class })
|
||||
static class MockBeanMultipleClassesWithName {
|
||||
|
||||
}
|
||||
|
||||
static class MockBeanInferClassToMock {
|
||||
|
||||
@MockBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean
|
||||
static class MockBeanMissingClassToMock {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@SpyBean(RealExampleService.class)
|
||||
static class SingleSpyBean {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@SpyBeans({ @SpyBean(RealExampleService.class), @SpyBean(ExampleServiceCaller.class) })
|
||||
static class RepeatSpyBean {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@SpyBean(name = "Name", classes = RealExampleService.class, reset = MockReset.NONE)
|
||||
static class SpyBeanAttributes {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@SpyBean(RealExampleService.class)
|
||||
static class SpyBeanOnClassAndField {
|
||||
|
||||
@SpyBean(ExampleServiceCaller.class)
|
||||
@Qualifier("test")
|
||||
private Object caller;
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@SpyBean({ RealExampleService.class, ExampleServiceCaller.class })
|
||||
static class SpyBeanMultipleClasses {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@SpyBean(name = "name", classes = { RealExampleService.class, ExampleServiceCaller.class })
|
||||
static class SpyBeanMultipleClassesWithName {
|
||||
|
||||
}
|
||||
|
||||
static class SpyBeanInferClassToMock {
|
||||
|
||||
@SpyBean
|
||||
private RealExampleService exampleService;
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@SpyBean
|
||||
static class SpyBeanMissingClassToMock {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.BootstrapContext;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate;
|
||||
import org.springframework.test.context.cache.DefaultContextCache;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for application context caching when using {@link MockBean @MockBean}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockBeanContextCachingTests {
|
||||
|
||||
private final DefaultContextCache contextCache = new DefaultContextCache(2);
|
||||
|
||||
private final DefaultCacheAwareContextLoaderDelegate delegate = new DefaultCacheAwareContextLoaderDelegate(
|
||||
this.contextCache);
|
||||
|
||||
@AfterEach
|
||||
@SuppressWarnings("unchecked")
|
||||
void clearCache() {
|
||||
Map<MergedContextConfiguration, ApplicationContext> contexts = (Map<MergedContextConfiguration, ApplicationContext>) ReflectionTestUtils
|
||||
.getField(this.contextCache, "contextMap");
|
||||
for (ApplicationContext context : contexts.values()) {
|
||||
if (context instanceof ConfigurableApplicationContext configurableContext) {
|
||||
configurableContext.close();
|
||||
}
|
||||
}
|
||||
this.contextCache.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenThereIsANormalBeanAndAMockBeanThenTwoContextsAreCreated() {
|
||||
bootstrapContext(TestClass.class);
|
||||
assertThat(this.contextCache.size()).isOne();
|
||||
bootstrapContext(MockedBeanTestClass.class);
|
||||
assertThat(this.contextCache.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenThereIsTheSameMockedBeanInEachTestClassThenOneContextIsCreated() {
|
||||
bootstrapContext(MockedBeanTestClass.class);
|
||||
assertThat(this.contextCache.size()).isOne();
|
||||
bootstrapContext(AnotherMockedBeanTestClass.class);
|
||||
assertThat(this.contextCache.size()).isOne();
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void bootstrapContext(Class<?> testClass) {
|
||||
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
|
||||
BootstrapContext bootstrapContext = mock(BootstrapContext.class);
|
||||
given((Class) bootstrapContext.getTestClass()).willReturn(testClass);
|
||||
bootstrapper.setBootstrapContext(bootstrapContext);
|
||||
given(bootstrapContext.getCacheAwareContextLoaderDelegate()).willReturn(this.delegate);
|
||||
TestContext testContext = bootstrapper.buildTestContext();
|
||||
testContext.getApplicationContext();
|
||||
}
|
||||
|
||||
@SpringBootTest(classes = TestConfiguration.class)
|
||||
static class TestClass {
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest(classes = TestConfiguration.class)
|
||||
static class MockedBeanTestClass {
|
||||
|
||||
@MockBean
|
||||
private TestBean testBean;
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest(classes = TestConfiguration.class)
|
||||
static class AnotherMockedBeanTestClass {
|
||||
|
||||
@MockBean
|
||||
private TestBean testBean;
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
TestBean testBean() {
|
||||
return new TestBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestBean {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} for a factory bean.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanForBeanFactoryIntegrationTests {
|
||||
|
||||
// gh-7439
|
||||
|
||||
@MockBean
|
||||
private TestFactoryBean testFactoryBean;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
void testName() {
|
||||
TestBean testBean = mock(TestBean.class);
|
||||
given(testBean.hello()).willReturn("amock");
|
||||
given(this.testFactoryBean.getObjectType()).willReturn((Class) TestBean.class);
|
||||
given(this.testFactoryBean.getObject()).willReturn(testBean);
|
||||
TestBean bean = this.applicationContext.getBean(TestBean.class);
|
||||
assertThat(bean.hello()).isEqualTo("amock");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
TestFactoryBean testFactoryBean() {
|
||||
return new TestFactoryBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestFactoryBean implements FactoryBean<TestBean> {
|
||||
|
||||
@Override
|
||||
public TestBean getObject() {
|
||||
return () -> "normal";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return TestBean.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface TestBean {
|
||||
|
||||
String hello();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.FailingExampleService;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a configuration class can be used to replace
|
||||
* existing beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanOnConfigurationClassForExistingBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.caller.getService().greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@MockBean(ExampleService.class)
|
||||
@Import({ ExampleServiceCaller.class, FailingExampleService.class })
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a configuration class can be used to inject new mock
|
||||
* instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockBeanOnConfigurationClassForNewBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.caller.getService().greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@MockBean(ExampleService.class)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.FailingExampleService;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a field on a {@code @Configuration} class can be
|
||||
* used to replace existing beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockBeanOnConfigurationFieldForExistingBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.config.exampleService.greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import({ ExampleServiceCaller.class, FailingExampleService.class })
|
||||
static class Config {
|
||||
|
||||
@MockBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a field on a {@code @Configuration} class can be
|
||||
* used to inject new mock instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockBeanOnConfigurationFieldForNewBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.config.exampleService.greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
@MockBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} can be used with a
|
||||
* {@link ContextHierarchy @ContextHierarchy}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextHierarchy({ @ContextConfiguration(classes = MockBeanOnContextHierarchyIntegrationTests.ParentConfig.class),
|
||||
@ContextConfiguration(classes = MockBeanOnContextHierarchyIntegrationTests.ChildConfig.class) })
|
||||
class MockBeanOnContextHierarchyIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ChildConfig childConfig;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
ApplicationContext context = this.childConfig.getContext();
|
||||
ApplicationContext parentContext = context.getParent();
|
||||
assertThat(parentContext
|
||||
.getBeanNamesForType(org.springframework.boot.test.mock.mockito.example.ExampleService.class)).hasSize(1);
|
||||
assertThat(parentContext
|
||||
.getBeanNamesForType(org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller.class))
|
||||
.isEmpty();
|
||||
assertThat(context.getBeanNamesForType(org.springframework.boot.test.mock.mockito.example.ExampleService.class))
|
||||
.isEmpty();
|
||||
assertThat(context
|
||||
.getBeanNamesForType(org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller.class))
|
||||
.hasSize(1);
|
||||
assertThat(context.getBean(org.springframework.boot.test.mock.mockito.example.ExampleService.class))
|
||||
.isNotNull();
|
||||
assertThat(context.getBean(org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller.class))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@MockBean(org.springframework.boot.test.mock.mockito.example.ExampleService.class)
|
||||
static class ParentConfig {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@MockBean(org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller.class)
|
||||
static class ChildConfig implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
ApplicationContext getContext() {
|
||||
return this.context;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.FailingExampleService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} when used in combination with scoped proxy targets.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see <a href="https://github.com/spring-projects/spring-boot/issues/5724">gh-5724</a>
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanOnScopedProxyTests {
|
||||
|
||||
@MockBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.caller.getService().greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import({ ExampleServiceCaller.class })
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
ExampleService exampleService() {
|
||||
return new FailingExampleService();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.FailingExampleService;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a test class can be used to replace existing beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@MockBean(ExampleService.class)
|
||||
class MockBeanOnTestClassForExistingBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.caller.getService().greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import({ ExampleServiceCaller.class, FailingExampleService.class })
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a test class can be used to inject new mock
|
||||
* instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@MockBean(ExampleService.class)
|
||||
class MockBeanOnTestClassForNewBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.caller.getService().greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a test class field can be used to replace existing
|
||||
* beans when the context is cached. This test is identical to
|
||||
* {@link MockBeanOnTestFieldForExistingBeanIntegrationTests} so one of them should
|
||||
* trigger application context caching.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see MockBeanOnTestFieldForExistingBeanIntegrationTests
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = MockBeanOnTestFieldForExistingBeanConfig.class)
|
||||
class MockBeanOnTestFieldForExistingBeanCacheIntegrationTests {
|
||||
|
||||
@MockBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.exampleService.greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.boot.test.mock.mockito.example.FailingExampleService;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* Config for {@link MockBeanOnTestFieldForExistingBeanIntegrationTests} and
|
||||
* {@link MockBeanOnTestFieldForExistingBeanCacheIntegrationTests}. Extracted to a shared
|
||||
* config to trigger caching.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import({ ExampleServiceCaller.class, FailingExampleService.class })
|
||||
public class MockBeanOnTestFieldForExistingBeanConfig {
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a test class field can be used to replace existing
|
||||
* beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see MockBeanOnTestFieldForExistingBeanCacheIntegrationTests
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = MockBeanOnTestFieldForExistingBeanConfig.class)
|
||||
class MockBeanOnTestFieldForExistingBeanIntegrationTests {
|
||||
|
||||
@MockBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.exampleService.greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a test class field can be used to replace existing
|
||||
* bean while preserving qualifiers.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
|
||||
|
||||
@MockBean
|
||||
@CustomQualifier
|
||||
private ExampleService service;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
this.caller.sayGreeting();
|
||||
then(this.service).should().greeting();
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyQualifiedBeanIsReplaced() {
|
||||
assertThat(this.applicationContext.getBean("service")).isSameAs(this.service);
|
||||
ExampleService anotherService = this.applicationContext.getBean("anotherService", ExampleService.class);
|
||||
assertThat(anotherService.greeting()).isEqualTo("Another");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class TestConfig {
|
||||
|
||||
@Bean
|
||||
CustomQualifierExampleService service() {
|
||||
return new CustomQualifierExampleService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExampleService anotherService() {
|
||||
return new RealExampleService("Another");
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExampleServiceCaller controller(@CustomQualifier ExampleService service) {
|
||||
return new ExampleServiceCaller(service);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a test class field can be used to inject new mock
|
||||
* instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanOnTestFieldForNewBeanIntegrationTests {
|
||||
|
||||
@MockBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.exampleService.greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.SimpleCacheResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} when mixed with Spring AOP.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see <a href="https://github.com/spring-projects/spring-boot/issues/5837">5837</a>
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanWithAopProxyTests {
|
||||
|
||||
@MockBean
|
||||
private DateService dateService;
|
||||
|
||||
@Test
|
||||
void verifyShouldUseProxyTarget() {
|
||||
given(this.dateService.getDate(false)).willReturn(1L);
|
||||
Long d1 = this.dateService.getDate(false);
|
||||
assertThat(d1).isOne();
|
||||
given(this.dateService.getDate(false)).willReturn(2L);
|
||||
Long d2 = this.dateService.getDate(false);
|
||||
assertThat(d2).isEqualTo(2L);
|
||||
then(this.dateService).should(times(2)).getDate(false);
|
||||
then(this.dateService).should(times(2)).getDate(eq(false));
|
||||
then(this.dateService).should(times(2)).getDate(anyBoolean());
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableCaching(proxyTargetClass = true)
|
||||
@Import(DateService.class)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
CacheResolver cacheResolver(CacheManager cacheManager) {
|
||||
SimpleCacheResolver resolver = new SimpleCacheResolver();
|
||||
resolver.setCacheManager(cacheManager);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ConcurrentMapCacheManager cacheManager() {
|
||||
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
|
||||
cacheManager.setCacheNames(Arrays.asList("test"));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Service
|
||||
static class DateService {
|
||||
|
||||
@Cacheable(cacheNames = "test")
|
||||
Long getDate(boolean argument) {
|
||||
return System.nanoTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests for a mock bean where the mocked interface has an async method.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanWithAsyncInterfaceMethodIntegrationTests {
|
||||
|
||||
@MockBean
|
||||
private Transformer transformer;
|
||||
|
||||
@Autowired
|
||||
private MyService service;
|
||||
|
||||
@Test
|
||||
void mockedMethodsAreNotAsync() {
|
||||
given(this.transformer.transform("foo")).willReturn("bar");
|
||||
assertThat(this.service.transform("foo")).isEqualTo("bar");
|
||||
}
|
||||
|
||||
interface Transformer {
|
||||
|
||||
@Async
|
||||
String transform(String input);
|
||||
|
||||
}
|
||||
|
||||
static class MyService {
|
||||
|
||||
private final Transformer transformer;
|
||||
|
||||
MyService(Transformer transformer) {
|
||||
this.transformer = transformer;
|
||||
}
|
||||
|
||||
String transform(String input) {
|
||||
return this.transformer.transform(input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableAsync
|
||||
static class MyConfiguration {
|
||||
|
||||
@Bean
|
||||
MyService myService(Transformer transformer) {
|
||||
return new MyService(transformer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Integration tests for using {@link MockBean @MockBean} with
|
||||
* {@link DirtiesContext @DirtiesContext} and {@link ClassMode#BEFORE_EACH_TEST_METHOD}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
|
||||
class MockBeanWithDirtiesContextClassModeBeforeMethodIntegrationTests {
|
||||
|
||||
@MockBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.exampleService.greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say Boot");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleGenericService;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleGenericServiceCaller;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Test {@link MockBean @MockBean} on a test class field can be used to inject new mock
|
||||
* instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanWithGenericsOnTestFieldForNewBeanIntegrationTests {
|
||||
|
||||
@MockBean
|
||||
private ExampleGenericService<Integer> exampleIntegerService;
|
||||
|
||||
@MockBean
|
||||
private ExampleGenericService<String> exampleStringService;
|
||||
|
||||
@Autowired
|
||||
private ExampleGenericServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
given(this.exampleIntegerService.greeting()).willReturn(200);
|
||||
given(this.exampleStringService.greeting()).willReturn("Boot");
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say 200 Boot");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleGenericServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests for a mock bean where the class being mocked uses field injection.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockBeanWithInjectedFieldIntegrationTests {
|
||||
|
||||
@MockBean
|
||||
private MyService myService;
|
||||
|
||||
@Test
|
||||
void fieldInjectionIntoMyServiceMockIsNotAttempted() {
|
||||
given(this.myService.getCount()).willReturn(5);
|
||||
assertThat(this.myService.getCount()).isEqualTo(5);
|
||||
}
|
||||
|
||||
static class MyService {
|
||||
|
||||
@Autowired
|
||||
private MyRepository repository;
|
||||
|
||||
int getCount() {
|
||||
return this.repository.findAll().size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface MyRepository {
|
||||
|
||||
List<Object> findAll();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.AfterClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.test.annotation.Repeat;
|
||||
import org.springframework.test.context.junit4.rules.SpringMethodRule;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockBean} and {@link Repeat}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @see <a href="https://github.com/spring-projects/spring-boot/issues/27693">gh-27693</a>
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class MockBeanWithSpringMethodRuleRepeatJUnit4IntegrationTests {
|
||||
|
||||
@Rule
|
||||
public final SpringMethodRule springMethodRule = new SpringMethodRule();
|
||||
|
||||
@MockBean
|
||||
private FirstService first;
|
||||
|
||||
private static int invocations;
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
assertThat(invocations).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Repeat(2)
|
||||
public void repeatedTest() {
|
||||
invocations++;
|
||||
}
|
||||
|
||||
interface FirstService {
|
||||
|
||||
String greeting();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.mock.MockCreationSettings;
|
||||
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleExtraInterface;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleService;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockDefinition}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockDefinitionTests {
|
||||
|
||||
private static final ResolvableType EXAMPLE_SERVICE_TYPE = ResolvableType.forClass(ExampleService.class);
|
||||
|
||||
@Test
|
||||
void classToMockMustNotBeNull() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new MockDefinition(null, null, null, null, false, null, null))
|
||||
.withMessageContaining("'typeToMock' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithDefaults() {
|
||||
MockDefinition definition = new MockDefinition(null, EXAMPLE_SERVICE_TYPE, null, null, false, null, null);
|
||||
assertThat(definition.getName()).isNull();
|
||||
assertThat(definition.getTypeToMock()).isEqualTo(EXAMPLE_SERVICE_TYPE);
|
||||
assertThat(definition.getExtraInterfaces()).isEmpty();
|
||||
assertThat(definition.getAnswer()).isEqualTo(Answers.RETURNS_DEFAULTS);
|
||||
assertThat(definition.isSerializable()).isFalse();
|
||||
assertThat(definition.getReset()).isEqualTo(MockReset.AFTER);
|
||||
assertThat(definition.getQualifier()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createExplicit() {
|
||||
QualifierDefinition qualifier = mock(QualifierDefinition.class);
|
||||
MockDefinition definition = new MockDefinition("name", EXAMPLE_SERVICE_TYPE,
|
||||
new Class<?>[] { ExampleExtraInterface.class }, Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE,
|
||||
qualifier);
|
||||
assertThat(definition.getName()).isEqualTo("name");
|
||||
assertThat(definition.getTypeToMock()).isEqualTo(EXAMPLE_SERVICE_TYPE);
|
||||
assertThat(definition.getExtraInterfaces()).containsExactly(ExampleExtraInterface.class);
|
||||
assertThat(definition.getAnswer()).isEqualTo(Answers.RETURNS_SMART_NULLS);
|
||||
assertThat(definition.isSerializable()).isTrue();
|
||||
assertThat(definition.getReset()).isEqualTo(MockReset.BEFORE);
|
||||
assertThat(definition.isProxyTargetAware()).isFalse();
|
||||
assertThat(definition.getQualifier()).isEqualTo(qualifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createMock() {
|
||||
MockDefinition definition = new MockDefinition("name", EXAMPLE_SERVICE_TYPE,
|
||||
new Class<?>[] { ExampleExtraInterface.class }, Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE,
|
||||
null);
|
||||
ExampleService mock = definition.createMock();
|
||||
MockCreationSettings<?> settings = Mockito.mockingDetails(mock).getMockCreationSettings();
|
||||
assertThat(mock).isInstanceOf(ExampleService.class);
|
||||
assertThat(mock).isInstanceOf(ExampleExtraInterface.class);
|
||||
assertThat(settings.getMockName()).hasToString("name");
|
||||
assertThat(settings.getDefaultAnswer()).isEqualTo(Answers.RETURNS_SMART_NULLS);
|
||||
assertThat(settings.isSerializable()).isTrue();
|
||||
assertThat(MockReset.get(mock)).isEqualTo(MockReset.BEFORE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleService;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockReset}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockResetTests {
|
||||
|
||||
@Test
|
||||
void noneAttachesReset() {
|
||||
ExampleService mock = mock(ExampleService.class);
|
||||
assertThat(MockReset.get(mock)).isEqualTo(MockReset.NONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withSettingsOfNoneAttachesReset() {
|
||||
ExampleService mock = mock(ExampleService.class, MockReset.withSettings(MockReset.NONE));
|
||||
assertThat(MockReset.get(mock)).isEqualTo(MockReset.NONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void beforeAttachesReset() {
|
||||
ExampleService mock = mock(ExampleService.class, MockReset.before());
|
||||
assertThat(MockReset.get(mock)).isEqualTo(MockReset.BEFORE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void afterAttachesReset() {
|
||||
ExampleService mock = mock(ExampleService.class, MockReset.after());
|
||||
assertThat(MockReset.get(mock)).isEqualTo(MockReset.AFTER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withSettingsAttachesReset() {
|
||||
ExampleService mock = mock(ExampleService.class, MockReset.withSettings(MockReset.BEFORE));
|
||||
assertThat(MockReset.get(mock)).isEqualTo(MockReset.BEFORE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void apply() {
|
||||
ExampleService mock = mock(ExampleService.class, MockReset.apply(MockReset.AFTER, withSettings()));
|
||||
assertThat(MockReset.get(mock)).isEqualTo(MockReset.AFTER);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
|
||||
import org.springframework.test.context.ContextCustomizer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockitoContextCustomizerFactory}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockitoContextCustomizerFactoryTests {
|
||||
|
||||
private final MockitoContextCustomizerFactory factory = new MockitoContextCustomizerFactory();
|
||||
|
||||
@Test
|
||||
void getContextCustomizerWithoutAnnotationReturnsCustomizer() {
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(NoMockBeanAnnotation.class, null);
|
||||
assertThat(customizer).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getContextCustomizerWithAnnotationReturnsCustomizer() {
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(WithMockBeanAnnotation.class, null);
|
||||
assertThat(customizer).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getContextCustomizerUsesMocksAsCacheKey() {
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(WithMockBeanAnnotation.class, null);
|
||||
assertThat(customizer).isNotNull();
|
||||
ContextCustomizer same = this.factory.createContextCustomizer(WithSameMockBeanAnnotation.class, null);
|
||||
assertThat(customizer).isNotNull();
|
||||
ContextCustomizer different = this.factory.createContextCustomizer(WithDifferentMockBeanAnnotation.class, null);
|
||||
assertThat(different).isNotNull();
|
||||
assertThat(customizer).hasSameHashCodeAs(same);
|
||||
assertThat(customizer.hashCode()).isNotEqualTo(different.hashCode());
|
||||
assertThat(customizer).isEqualTo(customizer).isEqualTo(same).isNotEqualTo(different);
|
||||
}
|
||||
|
||||
static class NoMockBeanAnnotation {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean({ Service1.class, Service2.class })
|
||||
static class WithMockBeanAnnotation {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean({ Service2.class, Service1.class })
|
||||
static class WithSameMockBeanAnnotation {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@MockBean({ Service1.class })
|
||||
static class WithDifferentMockBeanAnnotation {
|
||||
|
||||
}
|
||||
|
||||
interface Service1 {
|
||||
|
||||
}
|
||||
|
||||
interface Service2 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleService;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller;
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockitoContextCustomizer}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockitoContextCustomizerTests {
|
||||
|
||||
private static final Set<MockDefinition> NO_DEFINITIONS = Collections.emptySet();
|
||||
|
||||
@Test
|
||||
void hashCodeAndEquals() {
|
||||
MockDefinition d1 = createTestMockDefinition(ExampleService.class);
|
||||
MockDefinition d2 = createTestMockDefinition(ExampleServiceCaller.class);
|
||||
MockitoContextCustomizer c1 = new MockitoContextCustomizer(NO_DEFINITIONS);
|
||||
MockitoContextCustomizer c2 = new MockitoContextCustomizer(new LinkedHashSet<>(Arrays.asList(d1, d2)));
|
||||
MockitoContextCustomizer c3 = new MockitoContextCustomizer(new LinkedHashSet<>(Arrays.asList(d2, d1)));
|
||||
assertThat(c2).hasSameHashCodeAs(c3);
|
||||
assertThat(c1).isEqualTo(c1).isNotEqualTo(c2);
|
||||
assertThat(c2).isEqualTo(c2).isEqualTo(c3).isNotEqualTo(c1);
|
||||
}
|
||||
|
||||
private MockDefinition createTestMockDefinition(Class<?> typeToMock) {
|
||||
return new MockDefinition(null, ResolvableType.forClass(typeToMock), null, null, false, null, null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,360 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
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 org.springframework.core.Ordered;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Test for {@link MockitoPostProcessor}. See also the integration tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Andreas Neiser
|
||||
* @author Madhura Bhave
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class MockitoPostProcessorTests {
|
||||
|
||||
@Test
|
||||
void cannotMockMultipleBeans() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
MockitoPostProcessor.register(context);
|
||||
context.register(MultipleBeans.class);
|
||||
assertThatIllegalStateException().isThrownBy(context::refresh)
|
||||
.withMessageContaining("Unable to register mock bean " + ExampleService.class.getName()
|
||||
+ " expected a single matching bean to replace but found [example1, example2]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotMockMultipleQualifiedBeans() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
MockitoPostProcessor.register(context);
|
||||
context.register(MultipleQualifiedBeans.class);
|
||||
assertThatIllegalStateException().isThrownBy(context::refresh)
|
||||
.withMessageContaining("Unable to register mock bean " + ExampleService.class.getName()
|
||||
+ " expected a single matching bean to replace but found [example1, example3]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void canMockBeanProducedByFactoryBeanWithClassObjectTypeAttribute() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
MockitoPostProcessor.register(context);
|
||||
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
|
||||
factoryBeanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, SomeInterface.class);
|
||||
context.registerBeanDefinition("beanToBeMocked", factoryBeanDefinition);
|
||||
context.register(MockedFactoryBean.class);
|
||||
context.refresh();
|
||||
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void canMockBeanProducedByFactoryBeanWithResolvableTypeObjectTypeAttribute() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
MockitoPostProcessor.register(context);
|
||||
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
|
||||
ResolvableType objectType = ResolvableType.forClass(SomeInterface.class);
|
||||
factoryBeanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, objectType);
|
||||
context.registerBeanDefinition("beanToBeMocked", factoryBeanDefinition);
|
||||
context.register(MockedFactoryBean.class);
|
||||
context.refresh();
|
||||
assertThat(Mockito.mockingDetails(context.getBean("beanToBeMocked")).isMock()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
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
|
||||
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
|
||||
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();
|
||||
}
|
||||
|
||||
@Test
|
||||
void postProcessorShouldNotTriggerEarlyInitialization() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(FactoryBeanRegisteringPostProcessor.class);
|
||||
MockitoPostProcessor.register(context);
|
||||
context.register(TestBeanFactoryPostProcessor.class);
|
||||
context.register(EagerInitBean.class);
|
||||
context.refresh();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@MockBean(SomeInterface.class)
|
||||
static class MockedFactoryBean {
|
||||
|
||||
@Bean
|
||||
TestFactoryBean testFactoryBean() {
|
||||
return new TestFactoryBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@MockBean(ExampleService.class)
|
||||
static class MultipleBeans {
|
||||
|
||||
@Bean
|
||||
ExampleService example1() {
|
||||
return new FailingExampleService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExampleService example2() {
|
||||
return new FailingExampleService();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class MultipleQualifiedBeans {
|
||||
|
||||
@MockBean
|
||||
@Qualifier("test")
|
||||
private ExampleService mock;
|
||||
|
||||
@Bean
|
||||
@Qualifier("test")
|
||||
ExampleService example1() {
|
||||
return new FailingExampleService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExampleService example2() {
|
||||
return new FailingExampleService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Qualifier("test")
|
||||
ExampleService example3() {
|
||||
return new FailingExampleService();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class MockPrimaryBean {
|
||||
|
||||
@MockBean
|
||||
private ExampleService mock;
|
||||
|
||||
@Bean
|
||||
@Qualifier("test")
|
||||
ExampleService exampleQualified() {
|
||||
return new RealExampleService("qualified");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
ExampleService examplePrimary() {
|
||||
return new RealExampleService("primary");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class MockQualifiedBean {
|
||||
|
||||
@MockBean
|
||||
@Qualifier("test")
|
||||
private ExampleService mock;
|
||||
|
||||
@Bean
|
||||
@Qualifier("test")
|
||||
ExampleService exampleQualified() {
|
||||
return new RealExampleService("qualified");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
ExampleService examplePrimary() {
|
||||
return new RealExampleService("primary");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class SpyPrimaryBean {
|
||||
|
||||
@SpyBean
|
||||
private ExampleService spy;
|
||||
|
||||
@Bean
|
||||
@Qualifier("test")
|
||||
ExampleService exampleQualified() {
|
||||
return new RealExampleService("qualified");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
ExampleService examplePrimary() {
|
||||
return new RealExampleService("primary");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class SpyQualifiedBean {
|
||||
|
||||
@SpyBean
|
||||
@Qualifier("test")
|
||||
private ExampleService spy;
|
||||
|
||||
@Bean
|
||||
@Qualifier("test")
|
||||
ExampleService exampleQualified() {
|
||||
return new RealExampleService("qualified");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
ExampleService examplePrimary() {
|
||||
return new RealExampleService("primary");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class EagerInitBean {
|
||||
|
||||
@MockBean
|
||||
private ExampleService service;
|
||||
|
||||
}
|
||||
|
||||
static class TestFactoryBean implements FactoryBean<Object> {
|
||||
|
||||
@Override
|
||||
public Object getObject() {
|
||||
return new TestBean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class FactoryBeanRegisteringPostProcessor implements BeanFactoryPostProcessor, Ordered {
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
RootBeanDefinition beanDefinition = new RootBeanDefinition(TestFactoryBean.class);
|
||||
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition("test", beanDefinition);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.HIGHEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
Map<String, BeanWrapper> cache = (Map<String, BeanWrapper>) ReflectionTestUtils.getField(beanFactory,
|
||||
"factoryBeanInstanceCache");
|
||||
Assert.isTrue(cache.isEmpty(), "Early initialization of factory bean triggered.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface SomeInterface {
|
||||
|
||||
}
|
||||
|
||||
static class TestBean implements SomeInterface {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,503 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.ClassOrderer;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestClassOrder;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link MockitoTestExecutionListener}.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class MockitoTestExecutionListenerIntegrationTests {
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class MockedStaticTests {
|
||||
|
||||
private static final UUID uuid = UUID.randomUUID();
|
||||
|
||||
@Mock
|
||||
private MockedStatic<UUID> mockedStatic;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
@Disabled
|
||||
void shouldReturnConstantValueDisabled() {
|
||||
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||
UUID result = UUID.randomUUID();
|
||||
assertThat(result).isEqualTo(uuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldNotFailBecauseOfMockedStaticNotBeingClosed() {
|
||||
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||
UUID result = UUID.randomUUID();
|
||||
assertThat(result).isEqualTo(uuid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
|
||||
class MockedStaticTestsDirtiesContext {
|
||||
|
||||
private static final UUID uuid = UUID.randomUUID();
|
||||
|
||||
@Mock
|
||||
private MockedStatic<UUID> mockedStatic;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
@Disabled
|
||||
void shouldReturnConstantValueDisabled() {
|
||||
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||
UUID result = UUID.randomUUID();
|
||||
assertThat(result).isEqualTo(uuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldNotFailBecauseOfMockedStaticNotBeingClosed() {
|
||||
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||
UUID result = UUID.randomUUID();
|
||||
assertThat(result).isEqualTo(uuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldNotFailBecauseOfMockedStaticNotBeingClosedWhenMocksAreReinjected() {
|
||||
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||
UUID result = UUID.randomUUID();
|
||||
assertThat(result).isEqualTo(uuid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
|
||||
class MockedStaticTestsIfClassContainsOnlyDisabledTests {
|
||||
|
||||
@Nested
|
||||
@Order(1)
|
||||
class TestClass1 {
|
||||
|
||||
private static final UUID uuid = UUID.randomUUID();
|
||||
|
||||
@Mock
|
||||
private MockedStatic<UUID> mockedStatic;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
@Disabled
|
||||
void disabledTest() {
|
||||
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@Order(2)
|
||||
class TestClass2 {
|
||||
|
||||
private static final UUID uuid = UUID.randomUUID();
|
||||
|
||||
@Mock
|
||||
private MockedStatic<UUID> mockedStatic;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldNotFailBecauseMockedStaticHasNotBeenClosed() {
|
||||
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||
UUID result = UUID.randomUUID();
|
||||
assertThat(result).isEqualTo(uuid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
|
||||
class MockedStaticTestsIfClassContainsNoTests {
|
||||
|
||||
@Nested
|
||||
@Order(1)
|
||||
class TestClass1 {
|
||||
|
||||
@Mock
|
||||
private MockedStatic<UUID> mockedStatic;
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@Order(2)
|
||||
class TestClass2 {
|
||||
|
||||
private static final UUID uuid = UUID.randomUUID();
|
||||
|
||||
@Mock
|
||||
private MockedStatic<UUID> mockedStatic;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldNotFailBecauseMockedStaticHasNotBeenClosed() {
|
||||
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||
UUID result = UUID.randomUUID();
|
||||
assertThat(result).isEqualTo(uuid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ConfigureMockInBeforeEach {
|
||||
|
||||
@Mock
|
||||
private List<String> mock;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
given(this.mock.size()).willReturn(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldUseSetUpConfiguration() {
|
||||
assertThat(this.mock.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldBeAbleToReconfigureMock() {
|
||||
given(this.mock.size()).willReturn(2);
|
||||
assertThat(this.mock.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldNotBeAffectedByOtherTests() {
|
||||
assertThat(this.mock.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
@Disabled("https://github.com/spring-projects/spring-framework/issues/33690")
|
||||
class ConfigureMockInBeforeAll {
|
||||
|
||||
@Mock
|
||||
private List<String> mock;
|
||||
|
||||
@BeforeAll
|
||||
void setUp() {
|
||||
given(this.mock.size()).willReturn(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldUseSetUpConfiguration() {
|
||||
assertThat(this.mock.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldBeAbleToReconfigureMock() {
|
||||
given(this.mock.size()).willReturn(2);
|
||||
assertThat(this.mock.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldNotBeAffectedByOtherTest() {
|
||||
assertThat(this.mock.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@Import(MyBeanConfiguration.class)
|
||||
class ConfigureMockBeanWithResetAfterInBeforeEach {
|
||||
|
||||
@MockBean(reset = MockReset.AFTER)
|
||||
private MyBean mock;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
given(this.mock.call()).willReturn(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldUseSetUpConfiguration() {
|
||||
assertThat(this.mock.call()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldBeAbleToReconfigureMock() {
|
||||
given(this.mock.call()).willReturn(2);
|
||||
assertThat(this.mock.call()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldNotBeAffectedByOtherTests() {
|
||||
assertThat(this.mock.call()).isEqualTo(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@Import(MyBeanConfiguration.class)
|
||||
class ConfigureMockBeanWithResetBeforeInBeforeEach {
|
||||
|
||||
@MockBean(reset = MockReset.BEFORE)
|
||||
private MyBean mock;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
given(this.mock.call()).willReturn(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldUseSetUpConfiguration() {
|
||||
assertThat(this.mock.call()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldBeAbleToReconfigureMock() {
|
||||
given(this.mock.call()).willReturn(2);
|
||||
assertThat(this.mock.call()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldNotBeAffectedByOtherTests() {
|
||||
assertThat(this.mock.call()).isEqualTo(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@Import(MyBeanConfiguration.class)
|
||||
class ConfigureMockBeanWithResetNoneInBeforeEach {
|
||||
|
||||
@MockBean(reset = MockReset.NONE)
|
||||
private MyBean mock;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
given(this.mock.call()).willReturn(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldUseSetUpConfiguration() {
|
||||
assertThat(this.mock.call()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldBeAbleToReconfigureMock() {
|
||||
given(this.mock.call()).willReturn(2);
|
||||
assertThat(this.mock.call()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldNotBeAffectedByOtherTests() {
|
||||
assertThat(this.mock.call()).isEqualTo(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
@Import(MyBeanConfiguration.class)
|
||||
class ConfigureMockBeanWithResetAfterInBeforeAll {
|
||||
|
||||
@MockBean(reset = MockReset.AFTER)
|
||||
private MyBean mock;
|
||||
|
||||
@BeforeAll
|
||||
void setUp() {
|
||||
given(this.mock.call()).willReturn(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldUseSetUpConfiguration() {
|
||||
assertThat(this.mock.call()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldBeAbleToReconfigureMock() {
|
||||
given(this.mock.call()).willReturn(2);
|
||||
assertThat(this.mock.call()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldResetMockAfterReconfiguration() {
|
||||
assertThat(this.mock.call()).isEqualTo(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
@Import(MyBeanConfiguration.class)
|
||||
class ConfigureMockBeanWithResetBeforeInBeforeAll {
|
||||
|
||||
@MockBean(reset = MockReset.BEFORE)
|
||||
private MyBean mock;
|
||||
|
||||
@BeforeAll
|
||||
void setUp() {
|
||||
given(this.mock.call()).willReturn(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldResetMockBeforeThisMethod() {
|
||||
assertThat(this.mock.call()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldBeAbleToReconfigureMock() {
|
||||
given(this.mock.call()).willReturn(2);
|
||||
assertThat(this.mock.call()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldResetMockAfterReconfiguration() {
|
||||
assertThat(this.mock.call()).isEqualTo(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
@Import(MyBeanConfiguration.class)
|
||||
class ConfigureMockBeanWithResetNoneInBeforeAll {
|
||||
|
||||
@MockBean(reset = MockReset.NONE)
|
||||
private MyBean mock;
|
||||
|
||||
@BeforeAll
|
||||
void setUp() {
|
||||
given(this.mock.call()).willReturn(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void shouldUseSetUpConfiguration() {
|
||||
assertThat(this.mock.call()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void shouldBeAbleToReconfigureMock() {
|
||||
given(this.mock.call()).willReturn(2);
|
||||
assertThat(this.mock.call()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void shouldNotResetMock() {
|
||||
assertThat(this.mock.call()).isEqualTo(2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface MyBean {
|
||||
|
||||
int call();
|
||||
|
||||
}
|
||||
|
||||
private static final class DefaultMyBean implements MyBean {
|
||||
|
||||
@Override
|
||||
public int call() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
private static final class MyBeanConfiguration {
|
||||
|
||||
@Bean
|
||||
MyBean myBean() {
|
||||
return new DefaultMyBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.io.InputStream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.assertArg;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link MockitoTestExecutionListener}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class MockitoTestExecutionListenerTests {
|
||||
|
||||
private final MockitoTestExecutionListener listener = new MockitoTestExecutionListener();
|
||||
|
||||
@Mock
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Mock
|
||||
private MockitoPostProcessor postProcessor;
|
||||
|
||||
@Test
|
||||
void prepareTestInstanceShouldInitMockitoAnnotations() throws Exception {
|
||||
WithMockitoAnnotations instance = new WithMockitoAnnotations();
|
||||
this.listener.prepareTestInstance(mockTestContext(instance));
|
||||
assertThat(instance.mock).isNotNull();
|
||||
assertThat(instance.captor).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void prepareTestInstanceShouldInjectMockBean() throws Exception {
|
||||
given(this.applicationContext.getBean(MockitoPostProcessor.class)).willReturn(this.postProcessor);
|
||||
WithMockBean instance = new WithMockBean();
|
||||
TestContext testContext = mockTestContext(instance);
|
||||
given(testContext.getApplicationContext()).willReturn(this.applicationContext);
|
||||
this.listener.prepareTestInstance(testContext);
|
||||
then(this.postProcessor).should()
|
||||
.inject(assertArg((field) -> assertThat(field.getName()).isEqualTo("mockBean")), eq(instance),
|
||||
any(MockDefinition.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void beforeTestMethodShouldDoNothingWhenDirtiesContextAttributeIsNotSet() throws Exception {
|
||||
this.listener.beforeTestMethod(mock(TestContext.class));
|
||||
then(this.postProcessor).shouldHaveNoMoreInteractions();
|
||||
}
|
||||
|
||||
@Test
|
||||
void beforeTestMethodShouldInjectMockBeanWhenDirtiesContextAttributeIsSet() throws Exception {
|
||||
given(this.applicationContext.getBean(MockitoPostProcessor.class)).willReturn(this.postProcessor);
|
||||
WithMockBean instance = new WithMockBean();
|
||||
TestContext mockTestContext = mockTestContext(instance);
|
||||
given(mockTestContext.getApplicationContext()).willReturn(this.applicationContext);
|
||||
given(mockTestContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))
|
||||
.willReturn(Boolean.TRUE);
|
||||
this.listener.beforeTestMethod(mockTestContext);
|
||||
then(this.postProcessor).should()
|
||||
.inject(assertArg((field) -> assertThat(field.getName()).isEqualTo("mockBean")), eq(instance),
|
||||
any(MockDefinition.class));
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private TestContext mockTestContext(Object instance) {
|
||||
TestContext testContext = mock(TestContext.class);
|
||||
given(testContext.getTestInstance()).willReturn(instance);
|
||||
given(testContext.getTestClass()).willReturn((Class) instance.getClass());
|
||||
return testContext;
|
||||
}
|
||||
|
||||
static class WithMockitoAnnotations {
|
||||
|
||||
@Mock
|
||||
InputStream mock;
|
||||
|
||||
@Captor
|
||||
ArgumentCaptor<InputStream> captor;
|
||||
|
||||
}
|
||||
|
||||
static class WithMockBean {
|
||||
|
||||
@MockBean
|
||||
InputStream mockBean;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,166 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.assertArg;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Tests for {@link QualifierDefinition}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class QualifierDefinitionTests {
|
||||
|
||||
@Mock
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@Test
|
||||
void forElementFieldIsNullShouldReturnNull() {
|
||||
assertThat(QualifierDefinition.forElement((Field) null)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void forElementWhenElementIsNotFieldShouldReturnNull() {
|
||||
assertThat(QualifierDefinition.forElement(getClass())).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void forElementWhenElementIsFieldWithNoQualifiersShouldReturnNull() {
|
||||
QualifierDefinition definition = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(ConfigA.class, "noQualifier"));
|
||||
assertThat(definition).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void forElementWhenElementIsFieldWithQualifierShouldReturnDefinition() {
|
||||
QualifierDefinition definition = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(ConfigA.class, "directQualifier"));
|
||||
assertThat(definition).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesShouldCallBeanFactory() {
|
||||
Field field = ReflectionUtils.findField(ConfigA.class, "directQualifier");
|
||||
QualifierDefinition qualifierDefinition = QualifierDefinition.forElement(field);
|
||||
qualifierDefinition.matches(this.beanFactory, "bean");
|
||||
then(this.beanFactory).should()
|
||||
.isAutowireCandidate(eq("bean"), assertArg(
|
||||
(dependencyDescriptor) -> assertThat(dependencyDescriptor.getAnnotatedElement()).isEqualTo(field)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyToShouldSetQualifierElement() {
|
||||
Field field = ReflectionUtils.findField(ConfigA.class, "directQualifier");
|
||||
QualifierDefinition qualifierDefinition = QualifierDefinition.forElement(field);
|
||||
RootBeanDefinition definition = new RootBeanDefinition();
|
||||
qualifierDefinition.applyTo(definition);
|
||||
assertThat(definition.getQualifiedElement()).isEqualTo(field);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hashCodeAndEqualsShouldWorkOnDifferentClasses() {
|
||||
QualifierDefinition directQualifier1 = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(ConfigA.class, "directQualifier"));
|
||||
QualifierDefinition directQualifier2 = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(ConfigB.class, "directQualifier"));
|
||||
QualifierDefinition differentDirectQualifier1 = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(ConfigA.class, "differentDirectQualifier"));
|
||||
QualifierDefinition differentDirectQualifier2 = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(ConfigB.class, "differentDirectQualifier"));
|
||||
QualifierDefinition customQualifier1 = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(ConfigA.class, "customQualifier"));
|
||||
QualifierDefinition customQualifier2 = QualifierDefinition
|
||||
.forElement(ReflectionUtils.findField(ConfigB.class, "customQualifier"));
|
||||
assertThat(directQualifier1).hasSameHashCodeAs(directQualifier2);
|
||||
assertThat(differentDirectQualifier1).hasSameHashCodeAs(differentDirectQualifier2);
|
||||
assertThat(customQualifier1).hasSameHashCodeAs(customQualifier2);
|
||||
assertThat(differentDirectQualifier1).isEqualTo(differentDirectQualifier1)
|
||||
.isEqualTo(differentDirectQualifier2)
|
||||
.isNotEqualTo(directQualifier2);
|
||||
assertThat(directQualifier1).isEqualTo(directQualifier1)
|
||||
.isEqualTo(directQualifier2)
|
||||
.isNotEqualTo(differentDirectQualifier1);
|
||||
assertThat(customQualifier1).isEqualTo(customQualifier1)
|
||||
.isEqualTo(customQualifier2)
|
||||
.isNotEqualTo(differentDirectQualifier1);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class ConfigA {
|
||||
|
||||
@MockBean
|
||||
private Object noQualifier;
|
||||
|
||||
@MockBean
|
||||
@Qualifier("test")
|
||||
private Object directQualifier;
|
||||
|
||||
@MockBean
|
||||
@Qualifier("different")
|
||||
private Object differentDirectQualifier;
|
||||
|
||||
@MockBean
|
||||
@CustomQualifier
|
||||
private Object customQualifier;
|
||||
|
||||
}
|
||||
|
||||
static class ConfigB {
|
||||
|
||||
@MockBean
|
||||
@Qualifier("test")
|
||||
private Object directQualifier;
|
||||
|
||||
@MockBean
|
||||
@Qualifier("different")
|
||||
private Object differentDirectQualifier;
|
||||
|
||||
@MockBean
|
||||
@CustomQualifier
|
||||
private Object customQualifier;
|
||||
|
||||
}
|
||||
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface CustomQualifier {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,218 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleService;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link ResetMocksTestExecutionListener}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@TestMethodOrder(MethodOrderer.MethodName.class)
|
||||
class ResetMocksTestExecutionListenerTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@SpyBean
|
||||
ToSpy spied;
|
||||
|
||||
@Test
|
||||
void test001() {
|
||||
given(getMock("none").greeting()).willReturn("none");
|
||||
given(getMock("before").greeting()).willReturn("before");
|
||||
given(getMock("after").greeting()).willReturn("after");
|
||||
given(getMock("fromFactoryBean").greeting()).willReturn("fromFactoryBean");
|
||||
assertThat(this.context.getBean(NonSingletonFactoryBean.class).getObjectInvocations).isEqualTo(0);
|
||||
given(this.spied.action()).willReturn("spied");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test002() {
|
||||
assertThat(getMock("none").greeting()).isEqualTo("none");
|
||||
assertThat(getMock("before").greeting()).isNull();
|
||||
assertThat(getMock("after").greeting()).isNull();
|
||||
assertThat(getMock("fromFactoryBean").greeting()).isNull();
|
||||
assertThat(this.context.getBean(NonSingletonFactoryBean.class).getObjectInvocations).isEqualTo(0);
|
||||
assertThat(this.spied.action()).isNull();
|
||||
}
|
||||
|
||||
ExampleService getMock(String name) {
|
||||
return this.context.getBean(name, ExampleService.class);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
ExampleService before(MockitoBeans mockedBeans) {
|
||||
ExampleService mock = mock(ExampleService.class, MockReset.before());
|
||||
mockedBeans.add(mock);
|
||||
return mock;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExampleService after(MockitoBeans mockedBeans) {
|
||||
ExampleService mock = mock(ExampleService.class, MockReset.after());
|
||||
mockedBeans.add(mock);
|
||||
return mock;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExampleService none(MockitoBeans mockedBeans) {
|
||||
ExampleService mock = mock(ExampleService.class);
|
||||
mockedBeans.add(mock);
|
||||
return mock;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
ExampleService fail() {
|
||||
// gh-5870
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Bean
|
||||
BrokenFactoryBean brokenFactoryBean() {
|
||||
// gh-7270
|
||||
return new BrokenFactoryBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
WorkingFactoryBean fromFactoryBean() {
|
||||
return new WorkingFactoryBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
NonSingletonFactoryBean nonSingletonFactoryBean() {
|
||||
return new NonSingletonFactoryBean();
|
||||
}
|
||||
|
||||
@Bean
|
||||
ToSpyFactoryBean toSpyFactoryBean() {
|
||||
return new ToSpyFactoryBean();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class BrokenFactoryBean implements FactoryBean<String> {
|
||||
|
||||
@Override
|
||||
public String getObject() {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class WorkingFactoryBean implements FactoryBean<ExampleService> {
|
||||
|
||||
private final ExampleService service = mock(ExampleService.class, MockReset.before());
|
||||
|
||||
@Override
|
||||
public ExampleService getObject() {
|
||||
return this.service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return ExampleService.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class ToSpy {
|
||||
|
||||
String action() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class NonSingletonFactoryBean implements FactoryBean<ExampleService> {
|
||||
|
||||
private int getObjectInvocations = 0;
|
||||
|
||||
@Override
|
||||
public ExampleService getObject() {
|
||||
this.getObjectInvocations++;
|
||||
return mock(ExampleService.class, MockReset.before());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return ExampleService.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class ToSpyFactoryBean implements FactoryBean<ToSpy> {
|
||||
|
||||
@Override
|
||||
public ToSpy getObject() throws Exception {
|
||||
return new ToSpy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return ToSpy.class;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aop.SpringProxy;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.target.HotSwappableTargetSource;
|
||||
import org.springframework.aop.target.SingletonTargetSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringBootMockResolver}.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class SpringBootMockResolverTests {
|
||||
|
||||
@Test
|
||||
void testStaticTarget() {
|
||||
MyServiceImpl myService = new MyServiceImpl();
|
||||
MyService proxy = ProxyFactory.getProxy(MyService.class, new SingletonTargetSource(myService));
|
||||
Object target = new SpringBootMockResolver().resolve(proxy);
|
||||
assertThat(target).isInstanceOf(MyServiceImpl.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNonStaticTarget() {
|
||||
MyServiceImpl myService = new MyServiceImpl();
|
||||
MyService proxy = ProxyFactory.getProxy(MyService.class, new HotSwappableTargetSource(myService));
|
||||
Object target = new SpringBootMockResolver().resolve(proxy);
|
||||
assertThat(target).isInstanceOf(SpringProxy.class);
|
||||
}
|
||||
|
||||
private interface MyService {
|
||||
|
||||
int a();
|
||||
|
||||
}
|
||||
|
||||
private static final class MyServiceImpl implements MyService {
|
||||
|
||||
@Override
|
||||
public int a() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a configuration class can be used to spy existing
|
||||
* beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnConfigurationClassForExistingBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.caller.getService()).should().greeting();
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@SpyBean(SimpleExampleService.class)
|
||||
@Import({ ExampleServiceCaller.class, SimpleExampleService.class })
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a configuration class can be used to inject new spy
|
||||
* instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnConfigurationClassForNewBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.caller.getService()).should().greeting();
|
||||
}
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@SpyBean(SimpleExampleService.class)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a field on a {@code @Configuration} class can be used
|
||||
* to replace existing beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnConfigurationFieldForExistingBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.config.exampleService).should().greeting();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import({ ExampleServiceCaller.class, SimpleExampleService.class })
|
||||
static class Config {
|
||||
|
||||
@SpyBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a field on a {@code @Configuration} class can be used
|
||||
* to inject new spy instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnConfigurationFieldForNewBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.config.exampleService).should().greeting();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
@SpyBean
|
||||
private SimpleExampleService exampleService;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} can be used with a
|
||||
* {@link ContextHierarchy @ContextHierarchy}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextHierarchy({ @ContextConfiguration(classes = SpyBeanOnContextHierarchyIntegrationTests.ParentConfig.class),
|
||||
@ContextConfiguration(classes = SpyBeanOnContextHierarchyIntegrationTests.ChildConfig.class) })
|
||||
class SpyBeanOnContextHierarchyIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ChildConfig childConfig;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
ApplicationContext context = this.childConfig.getContext();
|
||||
ApplicationContext parentContext = context.getParent();
|
||||
assertThat(parentContext
|
||||
.getBeanNamesForType(org.springframework.boot.test.mock.mockito.example.ExampleService.class)).hasSize(1);
|
||||
assertThat(parentContext
|
||||
.getBeanNamesForType(org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller.class))
|
||||
.isEmpty();
|
||||
assertThat(context.getBeanNamesForType(org.springframework.boot.test.mock.mockito.example.ExampleService.class))
|
||||
.isEmpty();
|
||||
assertThat(context
|
||||
.getBeanNamesForType(org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller.class))
|
||||
.hasSize(1);
|
||||
assertThat(context.getBean(org.springframework.boot.test.mock.mockito.example.ExampleService.class))
|
||||
.isNotNull();
|
||||
assertThat(context.getBean(org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller.class))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@SpyBean(org.springframework.boot.test.mock.mockito.example.SimpleExampleService.class)
|
||||
static class ParentConfig {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@SpyBean(org.springframework.boot.test.mock.mockito.example.ExampleServiceCaller.class)
|
||||
static class ChildConfig implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
ApplicationContext getContext() {
|
||||
return this.context;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class can be used to replace existing beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpyBean(SimpleExampleService.class)
|
||||
class SpyBeanOnTestClassForExistingBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.caller.getService()).should().greeting();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import({ ExampleServiceCaller.class, SimpleExampleService.class })
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class can be used to inject new spy instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpyBean(SimpleExampleService.class)
|
||||
class SpyBeanOnTestClassForNewBeanIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.caller.getService()).should().greeting();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @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
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = SpyBeanOnTestFieldForExistingBeanConfig.class)
|
||||
class SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests {
|
||||
|
||||
@SpyBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.caller.getService()).should().greeting();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.boot.test.mock.mockito.example.SimpleExampleService;
|
||||
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
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import({ ExampleServiceCaller.class, SimpleExampleService.class })
|
||||
public class SpyBeanOnTestFieldForExistingBeanConfig {
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace existing
|
||||
* beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = SpyBeanOnTestFieldForExistingBeanConfig.class)
|
||||
class SpyBeanOnTestFieldForExistingBeanIntegrationTests {
|
||||
|
||||
@SpyBean
|
||||
private ExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.caller.getService()).should().greeting();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace existing
|
||||
* bean while preserving qualifiers.
|
||||
*
|
||||
* @author Andreas Neiser
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnTestFieldForExistingBeanWithQualifierIntegrationTests {
|
||||
|
||||
@SpyBean
|
||||
@CustomQualifier
|
||||
private ExampleService service;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Test
|
||||
void testMocking() {
|
||||
this.caller.sayGreeting();
|
||||
then(this.service).should().greeting();
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyQualifiedBeanIsReplaced() {
|
||||
assertThat(this.applicationContext.getBean("service")).isSameAs(this.service);
|
||||
ExampleService anotherService = this.applicationContext.getBean("anotherService", ExampleService.class);
|
||||
assertThat(anotherService.greeting()).isEqualTo("Another");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class TestConfig {
|
||||
|
||||
@Bean
|
||||
CustomQualifierExampleService service() {
|
||||
return new CustomQualifierExampleService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExampleService anotherService() {
|
||||
return new RealExampleService("Another");
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExampleServiceCaller controller(@CustomQualifier ExampleService service) {
|
||||
return new ExampleServiceCaller(service);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace existing
|
||||
* beans with circular dependencies.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(
|
||||
classes = SpyBeanOnTestFieldForExistingCircularBeansIntegrationTests.SpyBeanOnTestFieldForExistingCircularBeansConfig.class)
|
||||
class SpyBeanOnTestFieldForExistingCircularBeansIntegrationTests {
|
||||
|
||||
@SpyBean
|
||||
private One one;
|
||||
|
||||
@Autowired
|
||||
private Two two;
|
||||
|
||||
@Test
|
||||
void beanWithCircularDependenciesCanBeSpied() {
|
||||
this.two.callOne();
|
||||
then(this.one).should().someMethod();
|
||||
}
|
||||
|
||||
@Import({ One.class, Two.class })
|
||||
static class SpyBeanOnTestFieldForExistingCircularBeansConfig {
|
||||
|
||||
}
|
||||
|
||||
static class One {
|
||||
|
||||
@Autowired
|
||||
@SuppressWarnings("unused")
|
||||
private Two two;
|
||||
|
||||
void someMethod() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class Two {
|
||||
|
||||
@Autowired
|
||||
private One one;
|
||||
|
||||
void callOne() {
|
||||
this.one.someMethod();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleGenericService;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleGenericServiceCaller;
|
||||
import org.springframework.boot.test.mock.mockito.example.SimpleExampleIntegerGenericService;
|
||||
import org.springframework.boot.test.mock.mockito.example.SimpleExampleStringGenericService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace existing
|
||||
* beans.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnTestFieldForExistingGenericBeanIntegrationTests {
|
||||
|
||||
// gh-7625
|
||||
|
||||
@SpyBean
|
||||
private ExampleGenericService<String> exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleGenericServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say 123 simple");
|
||||
then(this.exampleService).should().greeting();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import({ ExampleGenericServiceCaller.class, SimpleExampleIntegerGenericService.class })
|
||||
static class SpyBeanOnTestFieldForExistingBeanConfig {
|
||||
|
||||
@Bean
|
||||
ExampleGenericService<String> simpleExampleStringGenericService() {
|
||||
// In order to trigger issue we need a method signature that returns the
|
||||
// generic type not the actual implementation class
|
||||
return new SimpleExampleStringGenericService();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleGenericService;
|
||||
import org.springframework.boot.test.mock.mockito.example.SimpleExampleStringGenericService;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class field can be used to replace an existing
|
||||
* bean with generics that's produced by a factory bean.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests {
|
||||
|
||||
// gh-40234
|
||||
|
||||
@SpyBean(name = "exampleService")
|
||||
private ExampleGenericService<String> exampleService;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(Mockito.mockingDetails(this.exampleService).isSpy()).isTrue();
|
||||
assertThat(Mockito.mockingDetails(this.exampleService).getMockCreationSettings().getSpiedInstance())
|
||||
.isInstanceOf(SimpleExampleStringGenericService.class);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(FactoryBeanRegistrar.class)
|
||||
static class SpyBeanOnTestFieldForExistingBeanConfig {
|
||||
|
||||
}
|
||||
|
||||
static class FactoryBeanRegistrar implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
|
||||
BeanDefinitionRegistry registry) {
|
||||
RootBeanDefinition definition = new RootBeanDefinition(ExampleGenericServiceFactoryBean.class);
|
||||
definition.setTargetType(ResolvableType.forClassWithGenerics(ExampleGenericServiceFactoryBean.class, null,
|
||||
ExampleGenericService.class));
|
||||
registry.registerBeanDefinition("exampleService", definition);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class ExampleGenericServiceFactoryBean<T, U extends ExampleGenericService<T>> implements FactoryBean<U> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public U getObject() throws Exception {
|
||||
return (U) new SimpleExampleStringGenericService();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Class<ExampleGenericService> getObjectType() {
|
||||
return ExampleGenericService.class;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.mock.mockito.example.ExampleGenericStringServiceCaller;
|
||||
import org.springframework.boot.test.mock.mockito.example.SimpleExampleStringGenericService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class field can be used to inject a spy
|
||||
* instance when there are multiple candidates and one is primary.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnTestFieldForMultipleExistingBeansWithOnePrimaryIntegrationTests {
|
||||
|
||||
@SpyBean
|
||||
private SimpleExampleStringGenericService spy;
|
||||
|
||||
@Autowired
|
||||
private ExampleGenericStringServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say two");
|
||||
assertThat(Mockito.mockingDetails(this.spy).getMockCreationSettings().getMockName()).hasToString("two");
|
||||
then(this.spy).should().greeting();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleGenericStringServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
SimpleExampleStringGenericService one() {
|
||||
return new SimpleExampleStringGenericService("one");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
SimpleExampleStringGenericService two() {
|
||||
return new SimpleExampleStringGenericService("two");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class field can be used to inject new spy
|
||||
* instances.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanOnTestFieldForNewBeanIntegrationTests {
|
||||
|
||||
@SpyBean
|
||||
private SimpleExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
assertThat(this.caller.sayGreeting()).isEqualTo("I say simple");
|
||||
then(this.caller.getService()).should().greeting();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.exceptions.misusing.UnfinishedVerificationException;
|
||||
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.SimpleCacheResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.reset;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} when mixed with Spring AOP.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see <a href="https://github.com/spring-projects/spring-boot/issues/5837">5837</a>
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanWithAopProxyAndNotProxyTargetAwareTests {
|
||||
|
||||
@SpyBean(proxyTargetAware = false)
|
||||
private DateService dateService;
|
||||
|
||||
@Test
|
||||
void verifyShouldUseProxyTarget() {
|
||||
this.dateService.getDate(false);
|
||||
then(this.dateService).should().getDate(false);
|
||||
assertThatExceptionOfType(UnfinishedVerificationException.class).isThrownBy(() -> reset(this.dateService));
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableCaching(proxyTargetClass = true)
|
||||
@Import(DateService.class)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
CacheResolver cacheResolver(CacheManager cacheManager) {
|
||||
SimpleCacheResolver resolver = new SimpleCacheResolver();
|
||||
resolver.setCacheManager(cacheManager);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ConcurrentMapCacheManager cacheManager() {
|
||||
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
|
||||
cacheManager.setCacheNames(Arrays.asList("test"));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Service
|
||||
public static class DateService {
|
||||
|
||||
@Cacheable(cacheNames = "test")
|
||||
public Long getDate(boolean arg) {
|
||||
return System.nanoTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
|
||||
import org.springframework.cache.interceptor.CacheResolver;
|
||||
import org.springframework.cache.interceptor.SimpleCacheResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} when mixed with Spring AOP.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @see <a href="https://github.com/spring-projects/spring-boot/issues/5837">5837</a>
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanWithAopProxyTests {
|
||||
|
||||
@SpyBean
|
||||
private DateService dateService;
|
||||
|
||||
@Test
|
||||
void verifyShouldUseProxyTarget() throws Exception {
|
||||
Long d1 = this.dateService.getDate(false);
|
||||
Thread.sleep(200);
|
||||
Long d2 = this.dateService.getDate(false);
|
||||
assertThat(d1).isEqualTo(d2);
|
||||
then(this.dateService).should().getDate(false);
|
||||
then(this.dateService).should().getDate(eq(false));
|
||||
then(this.dateService).should().getDate(anyBoolean());
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableCaching(proxyTargetClass = true)
|
||||
@Import(DateService.class)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
CacheResolver cacheResolver(CacheManager cacheManager) {
|
||||
SimpleCacheResolver resolver = new SimpleCacheResolver();
|
||||
resolver.setCacheManager(cacheManager);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ConcurrentMapCacheManager cacheManager() {
|
||||
ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
|
||||
cacheManager.setCacheNames(Arrays.asList("test"));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Service
|
||||
public static class DateService {
|
||||
|
||||
@Cacheable(cacheNames = "test")
|
||||
public Long getDate(boolean arg) {
|
||||
return System.nanoTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
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.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Integration tests for using {@link SpyBean @SpyBean} with
|
||||
* {@link DirtiesContext @DirtiesContext} and {@link ClassMode#BEFORE_EACH_TEST_METHOD}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
|
||||
class SpyBeanWithDirtiesContextClassModeBeforeMethodIntegrationTests {
|
||||
|
||||
@SpyBean
|
||||
private SimpleExampleService exampleService;
|
||||
|
||||
@Autowired
|
||||
private ExampleServiceCaller caller;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
this.caller.sayGreeting();
|
||||
then(this.exampleService).should().greeting();
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleServiceCaller.class)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.reflect.Proxy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpyBean @SpyBean} with a JDK proxy.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanWithJdkProxyTests {
|
||||
|
||||
@Autowired
|
||||
private ExampleService service;
|
||||
|
||||
@SpyBean
|
||||
private ExampleRepository repository;
|
||||
|
||||
@Test
|
||||
void jdkProxyCanBeSpied() {
|
||||
Example example = this.service.find("id");
|
||||
assertThat(example.id).isEqualTo("id");
|
||||
then(this.repository).should().find("id");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Import(ExampleService.class)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
ExampleRepository dateService() {
|
||||
return (ExampleRepository) Proxy.newProxyInstance(getClass().getClassLoader(),
|
||||
new Class<?>[] { ExampleRepository.class }, (proxy, method, args) -> new Example((String) args[0]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class ExampleService {
|
||||
|
||||
private final ExampleRepository repository;
|
||||
|
||||
ExampleService(ExampleRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
Example find(String id) {
|
||||
return this.repository.find(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface ExampleRepository {
|
||||
|
||||
Example find(String id);
|
||||
|
||||
}
|
||||
|
||||
static class Example {
|
||||
|
||||
private final String id;
|
||||
|
||||
Example(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.MockingDetails;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.boot.test.mock.mockito.example.SimpleExampleStringGenericService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test {@link SpyBean @SpyBean} on a test class field can be used to inject a spy
|
||||
* instance when there are multiple candidates and one is chosen using the name attribute.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class SpyBeanWithNameOnTestFieldForMultipleExistingBeansTests {
|
||||
|
||||
@SpyBean(name = "two")
|
||||
private SimpleExampleStringGenericService spy;
|
||||
|
||||
@Test
|
||||
void testSpying() {
|
||||
MockingDetails mockingDetails = Mockito.mockingDetails(this.spy);
|
||||
assertThat(mockingDetails.isSpy()).isTrue();
|
||||
assertThat(mockingDetails.getMockCreationSettings().getMockName()).hasToString("two");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
SimpleExampleStringGenericService one() {
|
||||
return new SimpleExampleStringGenericService("one");
|
||||
}
|
||||
|
||||
@Bean
|
||||
SimpleExampleStringGenericService two() {
|
||||
return new SimpleExampleStringGenericService("two");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2025 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
|
||||
*
|
||||
* https://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.jupiter.api.Test;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mockito;
|
||||
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 org.springframework.core.ResolvableType;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpyDefinition}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
class SpyDefinitionTests {
|
||||
|
||||
private static final ResolvableType REAL_SERVICE_TYPE = ResolvableType.forClass(RealExampleService.class);
|
||||
|
||||
@Test
|
||||
void classToSpyMustNotBeNull() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new SpyDefinition(null, null, null, true, null))
|
||||
.withMessageContaining("'typeToSpy' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithDefaults() {
|
||||
SpyDefinition definition = new SpyDefinition(null, REAL_SERVICE_TYPE, null, true, null);
|
||||
assertThat(definition.getName()).isNull();
|
||||
assertThat(definition.getTypeToSpy()).isEqualTo(REAL_SERVICE_TYPE);
|
||||
assertThat(definition.getReset()).isEqualTo(MockReset.AFTER);
|
||||
assertThat(definition.isProxyTargetAware()).isTrue();
|
||||
assertThat(definition.getQualifier()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createExplicit() {
|
||||
QualifierDefinition qualifier = mock(QualifierDefinition.class);
|
||||
SpyDefinition definition = new SpyDefinition("name", REAL_SERVICE_TYPE, MockReset.BEFORE, false, qualifier);
|
||||
assertThat(definition.getName()).isEqualTo("name");
|
||||
assertThat(definition.getTypeToSpy()).isEqualTo(REAL_SERVICE_TYPE);
|
||||
assertThat(definition.getReset()).isEqualTo(MockReset.BEFORE);
|
||||
assertThat(definition.isProxyTargetAware()).isFalse();
|
||||
assertThat(definition.getQualifier()).isEqualTo(qualifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSpy() {
|
||||
SpyDefinition definition = new SpyDefinition("name", REAL_SERVICE_TYPE, MockReset.BEFORE, true, null);
|
||||
RealExampleService spy = definition.createSpy(new RealExampleService("hello"));
|
||||
MockCreationSettings<?> settings = Mockito.mockingDetails(spy).getMockCreationSettings();
|
||||
assertThat(spy).isInstanceOf(ExampleService.class);
|
||||
assertThat(settings.getMockName()).hasToString("name");
|
||||
assertThat(settings.getDefaultAnswer()).isEqualTo(Answers.CALLS_REAL_METHODS);
|
||||
assertThat(MockReset.get(spy)).isEqualTo(MockReset.BEFORE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSpyWhenNullInstanceShouldThrowException() {
|
||||
SpyDefinition definition = new SpyDefinition("name", REAL_SERVICE_TYPE, MockReset.BEFORE, true, null);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> definition.createSpy(null))
|
||||
.withMessageContaining("'instance' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSpyWhenWrongInstanceShouldThrowException() {
|
||||
SpyDefinition definition = new SpyDefinition("name", REAL_SERVICE_TYPE, MockReset.BEFORE, true, null);
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> definition.createSpy(new ExampleServiceCaller(null)))
|
||||
.withMessageContaining("must be an instance of");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSpyTwice() {
|
||||
SpyDefinition definition = new SpyDefinition("name", REAL_SERVICE_TYPE, MockReset.BEFORE, true, null);
|
||||
Object instance = new RealExampleService("hello");
|
||||
instance = definition.createSpy(instance);
|
||||
definition.createSpy(instance);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
/**
|
||||
* Custom qualifier for testing.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface CustomQualifier {
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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;
|
||||
|
||||
/**
|
||||
* An {@link ExampleService} that uses a custom qualifier.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@CustomQualifier
|
||||
public class CustomQualifierExampleService implements ExampleService {
|
||||
|
||||
@Override
|
||||
public String greeting() {
|
||||
return "CustomQualifier";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 extra interface for mocking tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public interface ExampleExtraInterface {
|
||||
|
||||
String doExtra();
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 interface for mocking tests.
|
||||
*
|
||||
* @param <T> the generic type
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public interface ExampleGenericService<T> {
|
||||
|
||||
T greeting();
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 bean for mocking tests that calls {@link ExampleGenericService}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class ExampleGenericServiceCaller {
|
||||
|
||||
private final ExampleGenericService<Integer> integerService;
|
||||
|
||||
private final ExampleGenericService<String> stringService;
|
||||
|
||||
public ExampleGenericServiceCaller(ExampleGenericService<Integer> integerService,
|
||||
ExampleGenericService<String> stringService) {
|
||||
this.integerService = integerService;
|
||||
this.stringService = stringService;
|
||||
}
|
||||
|
||||
public ExampleGenericService<Integer> getIntegerService() {
|
||||
return this.integerService;
|
||||
}
|
||||
|
||||
public ExampleGenericService<String> getStringService() {
|
||||
return this.stringService;
|
||||
}
|
||||
|
||||
public String sayGreeting() {
|
||||
return "I say " + this.integerService.greeting() + " " + this.stringService.greeting();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 bean for mocking tests that calls {@link ExampleGenericService}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class ExampleGenericStringServiceCaller {
|
||||
|
||||
private final ExampleGenericService<String> stringService;
|
||||
|
||||
public ExampleGenericStringServiceCaller(ExampleGenericService<String> stringService) {
|
||||
this.stringService = stringService;
|
||||
}
|
||||
|
||||
public ExampleGenericService<String> getStringService() {
|
||||
return this.stringService;
|
||||
}
|
||||
|
||||
public String sayGreeting() {
|
||||
return "I say " + this.stringService.greeting();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 interface for mocking tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public interface ExampleService {
|
||||
|
||||
String greeting();
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 bean for mocking tests that calls {@link ExampleService}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class ExampleServiceCaller {
|
||||
|
||||
private final ExampleService service;
|
||||
|
||||
public ExampleServiceCaller(ExampleService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public ExampleService getService() {
|
||||
return this.service;
|
||||
}
|
||||
|
||||
public String sayGreeting() {
|
||||
return "I say " + this.service.greeting();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* An {@link ExampleService} that always throws an exception.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
@Service
|
||||
public class FailingExampleService implements ExampleService {
|
||||
|
||||
@Override
|
||||
public String greeting() {
|
||||
throw new IllegalStateException("Failed");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class RealExampleService implements ExampleService {
|
||||
|
||||
private final String greeting;
|
||||
|
||||
public RealExampleService(String greeting) {
|
||||
this.greeting = greeting;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String greeting() {
|
||||
return this.greeting;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 generic service implementation for spy tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class SimpleExampleIntegerGenericService implements ExampleGenericService<Integer> {
|
||||
|
||||
@Override
|
||||
public Integer greeting() {
|
||||
return 123;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class SimpleExampleService extends RealExampleService {
|
||||
|
||||
public SimpleExampleService() {
|
||||
super("simple");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
*
|
||||
* https://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 generic service implementation for spy tests.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @deprecated since 3.4.0 for removal in 4.0.0
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
@Deprecated(since = "3.4.0", forRemoval = true)
|
||||
public class SimpleExampleStringGenericService implements ExampleGenericService<String> {
|
||||
|
||||
private final String greeting;
|
||||
|
||||
public SimpleExampleStringGenericService() {
|
||||
this("simple");
|
||||
}
|
||||
|
||||
public SimpleExampleStringGenericService(String greeting) {
|
||||
this.greeting = greeting;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String greeting() {
|
||||
return this.greeting;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user