Refine @MockBean/@SpyBean qualifier support so that qualifiers form part
of the context cache key. Prior to this commit is was possible that two
different tests could accidentally share the same context if they
defined the same @Mock but with different @Qualifiers.
See gh-6753
Previously, if an injection point used a qualifier, `MockBean` and
`SpyBean` couldn't be used to mock/spy it as there was no way to
specify that qualifier information.
This commit now detects qualifier information on the injection point
and associate it with the created `BeanDefintion`. If one wants to
mock a bean that is qualified with `@Qualifier("foo")`, the definition
of the mock should be as follows:
```
public class MyTest {
@MockBean
@Qualifier("foo")
private ExampleService service;
}
```
As a side effect, it is now possible to mock a service by type even if
there are multiple instances of that type in the application context. The
provided qualifier information is used to determine the right candidate
and the proper bean definition is replaced accordingly.
Closes gh-6753
@WebAppConfiguration expects a mock web environment. If it is used
in conjuction with @SpringBootTest configured with a RANDOM_PORT or
DEFINED_PORT web environment a null pointer exception occurs as an
assumption that's made by MockServerContainerContextCustomizer doesn't
hold true in a non-mock web environment.
This commit updates SpringBootTestContextBootstrap to detect the
illegal configuration combination and fail fast, advising the user
to remove @WebAppConfiguration or reconfigure @SpringBootTest.
Closes gh-6795
Rename AutoConfigureReportTestExecutionListener to
SpringBootDependencyInjectionTestExecutionListener and ensure that it
replaces any existing DependencyInjectionTestExecutionListener.
Prior to this commit the registration of two DependencyInjection
listeners would cause @PostConstruct methods on tests to be called
twice.
In order to allow the standard DependencyInjectionTestExecutionListener
to be removed a new DefaultTestExecutionListenersPostProcessor interface
has been introduced.
Fixes gh-6874
Update MockitoAopProxyTargetInterceptor to deal with deal with any
existing argument matchers when working with the VerificationMode.
Prior to this commit `@SpyBean` when combined with AOP could not support
argument matchers.
Fixes gh-6871
Update RootUriRequestExpectationManager to only wrap requests when
they cannot be cast to MockClientHttpRequest. This prevents later
ClassCastExceptions from being thrown with @RestClientTests that
define expected body content.
Fixes gh-6845
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