Commit Graph

2631 Commits

Author SHA1 Message Date
Stéphane Nicoll 0ac7e41beb Provide an implementation of JsonComparator
See gh-32791
2024-05-23 16:17:02 +02:00
Phillip Webb d8dcd3af0b Introduce JSON comparison abstraction for tests
This commit introduces a JsonComparator abstraction, with an
implementation using JSONAssert. Previously, JSONAssert was the only
choice.

Test APIs have been adapted to allow the new abstraction while relying
on JSONAssert still for high-level methods.

Closes gh-32791

Co-authored-by: Stéphane Nicoll <stephane.nicoll@broadcom.com>
2024-05-23 12:32:39 +02:00
Arjen Poutsma 80faa94afc Support Map in FormHttpMessageConverter
This commit changes the FormHttpMessageConverter from a
HttpMessageConverter<MultiValueMap<String, ?>> to a
HttpMessageConverter<Map<String, ?>>, so that normal, single-value maps
can also be used as form representation, both for reading and writing.

Closes gh-32826
2024-05-23 10:33:49 +02:00
Arjen Poutsma 67d2b2566e Revert "WIP"
This reverts commit c09b0f57631905a7b9cca32be5516853e4263ac0.
2024-05-23 10:33:49 +02:00
Arjen Poutsma 14861024d5 WIP 2024-05-23 10:33:49 +02:00
Sam Brannen bc291949c2 Polishing 2024-05-22 15:09:54 +02:00
Sam Brannen b47cdaa259 Lazily register DynamicValuesPropertySource in the TestContext framework
Prior to this commit, as a result of commit 6cdb34410b, the
DynamicValuesPropertySource was eagerly registered in the Environment
even if the DynamicPropertyRegistry was never used to register dynamic
properties.

This commit ensures that the DynamicValuesPropertySource is only
registered if we know that the DynamicPropertyRegistry is actually used
-- either by a @⁠DynamicPropertySource method in a test class or via a
bean in the ApplicationContext that invokes add() on the
DynamicPropertyRegistry bean.

See gh-32271
Closes gh-32871
2024-05-22 14:25:19 +02:00
Simon Baslé d5c7a5e2db Bean overriding by type uses isAutowireCandidate for matching
This commit uses the bean factory `isAutowiredCandidate` method directly
in `BeanOverrideBeanFactoryPostProcessor` to select a single match among
multiple candidates when matching by type.

The expected consequence, in most cases, is that this will delegate to
a `@Qualifier`-aware `QualifierAnnotationAutowireCandidateResolver`.
In that sense, bean overriding by-type matching is now potentially
taking Qualifier annotations or meta-annotations into account.

It also changes the way existing bean definitions are checked in case
a bean name has been specified: factory beans are now taken into account
when checking the type of an existing definition matches the expected
bean override type.

Closes gh-32822
2024-05-21 14:06:52 +02:00
Simon Baslé b17d1c5124 Polishing BeanOverrideBeanFactoryPostProcessor to also copy isFallback 2024-05-21 14:06:52 +02:00
Stéphane Nicoll 99adc58ee3 Merge branch '6.1.x' 2024-05-20 13:58:09 +02:00
Stéphane Nicoll f2605fd9e5 Avoid reader on empty content to be shared by multiple requests
This commit avoids several instances of MockHttpServletRequest to
have a common reader for empty content as closing it will have an
unwanted side effect on the others.

Closes gh-32820
2024-05-20 13:32:04 +02:00
Stéphane Nicoll 4bbfd4c5e7 Add convenient request/response accessors on MvcTestResult
Closes gh-32846
2024-05-20 11:19:12 +02:00
Sam Brannen 93387e69b5 Polishing 2024-05-16 16:47:02 +02:00
Sam Brannen 6cdb34410b Register DynamicPropertyRegistry as singleton bean in test ApplicationContext
Prior to this commit, DynamicPropertyRegistry could only be used with a
static @⁠DynamicPropertySource method in an integration test class;
however, it can also be useful to be able to register a "dynamic
property" from within a test's ApplicationContext -- for example, in a
@⁠Bean method in a @⁠Configuration class that is specific to testing
scenarios.

To support such use cases, this commit updates the dynamic property
source infrastructure so that a DynamicPropertyRegistry is always
registered as a singleton bean in a test's ApplicationContext. This
allows DynamicPropertyRegistry to be autowired into a @⁠Configuration
class or supplied to a @⁠Bean method as an argument as shown in the
following example.

@⁠Bean
@⁠DynamicPropertySource
ApiServer apiServer(DynamicPropertyRegistry registry) {
	ApiServer apiServer = new ApiServer();
	registry.add("api.url", apiServer::getUrl);
	return apiServer;
}

Note that the use of @⁠DynamicPropertySource on the @⁠Bean method is
optional and results in the corresponding bean being eagerly
initialized so that other singleton beans in the context can be given
access to the dynamic properties sourced from that bean when those
other beans are initialized.

Side note: DynamicPropertySourceBeanInitializer temporarily implements
LoadTimeWeaverAware since doing so is currently the only way to have a
component eagerly initialized before the
ConfigurableListableBeanFactory.preInstantiateSingletons() phase.
However, we plan to introduce a first-class callback to support such
use cases in the future.

Closes gh-32271
2024-05-15 17:09:03 +02:00
Simon Baslé 531da015e1 Improve MediaTypeAssert to-string-equality assertions
This commit introduces a `isNotEqualTo(String)` assertion in order to
avoid false negatives when using the default Object-based assertion.

This is a risk since MediaTypeAssert has a `isEqualTo(String)` method
that overrides the base object method and parses the provided String
into a MediaType. Users may thus be tempted to use the reverse assertion
and expect the same parsing behaviour.

This commit also adds tests around the String parsing and the isNotEqual
cases.

Closes gh-32756
2024-05-15 11:14:13 +02:00
Simon Baslé a86612a254 Use by-type semantics in bean overriding if no explicit name is provided
This change switches default behavior of `@TestBean`, `@MockitoBean` and
`@MockitoSpyBean` to match the bean definition / bean to override by
type in the case there is no explicit bean name provided via the
annotation. The previous behavior of using the annotated field's name
is still an option for implementors, but no longer the default.

Closes gh-32761
2024-05-14 16:42:11 +02:00
evan-mc 376d7839e0 Align StandaloneMockMvcBuilder with trailing slash defaults
Prior to this commit, trailing slash matching was disabled by default in
Spring MVC with gh-28552. `StandaloneMockMvcBuilder` was not changed as
a result and still had the trailing slash match option enabled.

This commit aligns the defaults in `StandaloneMockMvcBuilder` to better
reflect the expected behavior in tests.

Closes gh-32796
2024-05-14 09:15:56 +02:00
Sam Brannen 589b6dbba0 Merge branch '6.1.x' 2024-05-11 16:01:31 +02:00
Sam Brannen c1229b03a7 Polishing 2024-05-11 16:01:08 +02:00
Yanming Zhou a02861f7db Insist on using `CollectionUtils.isEmpty()` and `StringUtils.hasLength()`
search `(\w+) != null && !(\1).isEmpty\(\)`
2024-05-09 09:25:09 +02:00
Stéphane Nicoll 8d90817ba3 Polish and fix build failure 2024-05-08 15:59:28 +02:00
Stéphane Nicoll 36295e664a Merge branch '6.1.x' 2024-05-08 15:47:10 +02:00
Stéphane Nicoll 6250b64766 Add support for form data in MockHttpServletRequestBuilder
Closes gh-32757
2024-05-08 11:56:16 +02:00
Sam Brannen 3ce79c3fbf Polishing 2024-05-08 11:54:55 +03:00
Stéphane Nicoll bcecce7aac Add shortcuts for frequently used assertions
See gh-32712
2024-05-07 16:28:44 +02:00
Stéphane Nicoll c8967de065 Provide better name for entry points
This commit renames AssertableMockMvc to MockMvcTester as the former
was too mouthful, and we are looking for a naming pattern that can be
applied consistently to test utilities that rely on AssertJ.

Rather than AssertableMvcResult, we now use MvcTestResult and it no
longer extends from MvcResult itself. That avoids having existing code
that is migrated to the new API whilst assigning the result to MvcResult
and get a bad DevXP as that doesn't bring any of the AssertJ support.

See gh-32712
2024-05-07 16:28:44 +02:00
Stéphane Nicoll 5567d14700 Move response body directly in AbstractMockHttpServletResponseAssert
This commit removes ResponseBodyAssert and rather offers first-class
access support for the response body at the root level using bodyText(),
bodyJson(), and body().

This avoids a double navigation to assert the response body.

See gh-32712
2024-05-07 16:28:44 +02:00
Stéphane Nicoll 24cc77655f Merge JSON assertions in a single Assert type
This commit merges the JSONCompare and JsonPath support in a single
assert object. The rationale for the previous situation was that
JsonPath is optional but merging the assertions methods do not make
it mandatory as the usage if JsonPath is triggered only if it is
actually used.

See gh-32712
2024-05-07 16:28:44 +02:00
Sam Brannen ac0136b75c Clarify supported targets for annotations in the TestContext framework
Closes gh-32772
2024-05-07 16:45:12 +03:00
Sébastien Deleuze 12272d6e41 Remove unneeded `@SuppressWarnings("NullAway")`
Closes gh-32542
2024-05-06 18:28:54 +02:00
Stéphane Nicoll 93dcbc41b0 Polish 2024-05-06 17:40:23 +02:00
Stéphane Nicoll 371d1a02f2 Fix typo 2024-05-06 17:38:52 +02:00
Arjen Poutsma 92f54da628 Bug fixes in WebMvc.fn testing support
See gh-30477
2024-05-03 14:28:21 +02:00
Sam Brannen 12b285add3 Polishing 2024-05-03 14:03:50 +03:00
Arjen Poutsma 8f3b7484e2 Add testing support for WebMvc.fn
This commit introduces testing support for WebMvc.fn in the form of a
RouterFunctionMockMvcBuilder and RouterFunctionMockMvcSpec.

Closes gh-30477
2024-05-02 15:20:56 +02:00
Sam Brannen 3002813598 Clean up warnings in spring-test 2024-04-26 15:49:09 +03:00
Stéphane Nicoll c21090ad31 Merge branch '6.1.x' 2024-04-22 15:16:50 +02:00
Stéphane Nicoll 8a8c8fe00e Detect target of factory method with AOT
Previously, if a factory method is defined on a parent, the generated
code would blindly use the method's declaring class for both the target
of the generated code, and the signature of the method.

This commit improves the resolution by considering the factory metadata
in the BeanDefinition.

Closes gh-32609
2024-04-22 15:13:56 +02:00
Sam Brannen 8727d723f3 Polish "Bean Overriding in Tests" support 2024-04-16 17:08:15 +02:00
Sam Brannen b13963ee81 Reject private JUnit Jupiter lifecycle methods annotated with @⁠Autowired
Closes gh-32627
2024-04-12 14:27:38 +02:00
Stéphane Nicoll 2e3a923225 Polish 2024-04-11 08:45:26 +02:00
Simon Baslé 2d33aac350 Improve Bean Overriding support, testing and documentation
This commit improves on the bean overriding feature in several ways:
the API is simplified and polished (metadata and processor contracts,
 etc...).

The commit also reworks infrastructure classes (context customizer,
test execution listener, BeanOverrideBeanFactoryPostProcessor, etc...).
Parsing of annotations is now fully stateless.

In order to avoid OverrideMetadata in bean definition and to make a
first step towards AOT support, the BeanOverrideBeanFactoryPostProcessor
now delegates to a BeanOverrideRegistrar to track classes to parse,
the metadata-related state as well as for the field injection methods
for tests.

Lastly, this commit increases the test coverage for the provided
annotations and adds integration tests and fixes a few `@TestBean`
issues.
2024-04-10 18:16:43 +02:00
Stéphane Nicoll 7a74e45946 Make use of bean definition overriding more visible
This commit makes the use of bean definition overriding more visible and
prepare for a deprecation of the feature in the next major release.

As of this commit, use of bean definition overriding logs at INFO level.
The previous log level can be restored by setting the
allowBeanDefinitionOverriding flag explicitly on the BeanFactory (or
via the related ApplicationContext).

A number of tests that are using bean overriding on purpose have been
updated to set this flag, which will make them easier to find once we
actually deprecate the feature.

Closes gh-31288
2024-04-02 14:05:12 +02:00
Sébastien Deleuze 996e66abdb Perform NullAway build-time checks in spring-test
Closes gh-32475
2024-03-26 18:14:56 +01:00
Sébastien Deleuze 96d9081190 Merge branch '6.1.x' 2024-03-26 18:10:25 +01:00
Sébastien Deleuze 88b9c0567d Refine null-safety in spring-test
See gh-32475
2024-03-26 18:09:55 +01:00
Stéphane Nicoll bf5b22d64f Merge branch '6.1.x' 2024-03-26 17:45:25 +01:00
Stéphane Nicoll c7c9da56da Consistent atMost period for Awaitility-based tests
Closes gh-32537
2024-03-26 17:45:14 +01:00
Sam Brannen 5698191ba0 Rename listener to CommonCachesTestExecutionListener
See gh-30954
2024-03-20 16:37:22 +01:00
Sam Brannen 7d197972e0 Polishing 2024-03-20 13:05:30 +01:00