Post-processing of mocked beans causes a number of problems:
- The mock may be proxied for asynchronous processing which can cause
problems when configuring expectations on a mock (gh-6573)
- The mock may be proxied so that its return values can be cached or
so that its methods can be transactional. This causes problems with
verification of the expected calls to a mock (gh-6573, gh-5837)
- If the mock is created from a class that uses field injection, the
container will attempt to inject values into its fields. This causes
problems if the mock is being created to avoid the use of one of
those dependencies (gh-6663)
- Proxying a mocked bean can lead to a JDK proxy being created
(if proxyTargetClass=false) as the mock implements a Mockito
interface. This can then cause injection failures as the types don’t
match (gh-6405, gh-6665)
All of these problems can be avoided if a mocked bean is not
post-processed. Avoiding post-processing prevents proxies from being
created and autowiring from being performed. This commit avoids
post-processing by registering mocked beans as singletons as well as
via a bean definition. The latter is still used by the context for type
matching purposes.
Closes gh-6573, gh-6663, gh-6664
Update @MockBean and @SpyBean to support field generics. Prior to this
commit the following fields would fail with a "Duplicate mock
definition" exception:
@MockBean
private IdentityProvider<PasswordIdentity> passwordIdentityProvider;
@MockBean
private IdentityProvider<Oauth2Identity> oauth2IdentityProvider;
Fixes gh-6602
Previously, MockitoPostProcessor would fail to replace a factory bean
with a mock if the factory bean didn't return a matching type from
getObjectType(). This prevented Spring Data respoitories from being
replaced with a mock as Spring Data's repository factory beans
generally do not know the specific repository type that they will
produce when MockPostProcesser (a bean factory post-processor) is
running.
Spring Data has been updated to add a factoryBeanObjectType attribute
to its factory bean definitions. MockitoPostProcessor has been updated
to look for FactoryBeans with this attribute and to use its value
to determine whether or not the factory bean produces a bean of the
required type and, therefore, should be replaced with a mock.
Closes gh-6541
Switch `@AutoConfigureJsonTesters` to use regular `@Autowired` injection
for JSON testers. Prior to this commit JSON Tester fields were
initialized directly which caused IDE issues and was also a little
confusing.
Fixes gh-6451
Update MockDefinition to only call `Answers.get()` if a direct cast
isn't possible. This provides compatibility with Mockito 2.0 (currently
2.0.47-beta) without breaking support for Mockto 1.x.
Closes gh-6323
Where possible, code that previously synchronized on this (or on the
class in the case of static methods) has been updated to use an
internal monitor object instead. This allows the locking model that's
employed to be an implementation detail rather than part of the
class's API.
Classes that override a synchronized method continue to declare
the overriding method as synchronized. This ensures that locking
is consistent across the superclass and its subclass.
Closes gh-6262
This commit changes the default version of Tomcat to 8.5.3 while
also retaining support for Tomcat 8.0 and 7.0. The main difference
in 8.5 is that the ServerSocketFactory abstraction that allowed the
TrustStore and KeyStore to be configured programatically no longer
exists. This logic has been replaced with the use of a custom URL
protocol (springbootssl) that provides access to the key store and
trust store of an SslStoreProvider. In addition to working with 8.5,
this approach has the advantage of also working with 8.0 and 7.0.
Closes gh-6164
Add @RestClientTest annotation that can be used when testing REST
clients. Provides auto-configuration for a MockRestServiceServer which
can be used when the bean under test builds a single RestTemplate
via the auto-configured RestTemplateBuilder.
Closes gh-6030
Update TestRestTemplate so that it no longer directly extends
RestTemplate. Prior to this commit it was possible that TestRestTemplate
could interfere with user defined RestTemplate beans.
TestRestTemplate offers the same methods as RestTemplate so should be
a drop-in replacement. If access is needed to the actual underlying
template the `getRestTemplate()` method should be used.
Fixes gh-5915
Update Mockito support so that AOP Proxies automatically get additional
`Advice` that allows them to work with Mockito. Prior to this commit a
call to `verify` would fail because exiting AOP advice would confuse
Mockito and an `UnfinishedVerificationException` would be thrown.
The `MockitoAopProxyTargetInterceptor` works by detecting calls to a
mock that have been proceeded by `verify()` and bypassing AOP to
directly call the mock.
The order that `@SpyBean` creation occurs has also been updated to
ensure that that the spy is created before AOP advice is applied.
Without this, the creation of a spy would fail because Mockito copies
'state' to the newly created spied instance. Unfortunately, in the case
of AOP proxies, 'state' includes cglib interceptor fields. This means
that Mockito's own interceptors are clobbered by Spring's AOP
interceptors.
Fixes gh-5837
It is legal for an annotation to be annotated with itself. Previously,
when searching for meta annotations this could lead to a stack overflow.
This was likely to occur when using Kotlin as, like Java, its Target
annotation is annotated with itself. A stack overflow doesn’t occur
with Java’s Target annotation due to some short-circuiting logic for
annotations in java.lang.
This commit updates the logic for finding meta-annotations to
short-circuit when an annotation that has already been seen is
encountered.
Closes gh-5902
The intention in 1.4 is for the deprecated testing functionality to
behave exactly as it did in 1.3. To help with this, this commit
updates SpringApplicationConfiguration to use
SpringApplicationContextLoader as its loader, just as it did in 1.3.
Closes gh-5882