Commit Graph

2721 Commits

Author SHA1 Message Date
Sam Brannen f6110dda06 Polishing 2024-08-08 17:48:57 +03:00
Simon Baslé 70e5af7d16 Polishing: remove unnecessary code 2024-08-07 16:57:52 +02:00
Simon Baslé 52bc3cf5aa Polishing 2024-08-07 16:43:09 +02:00
Stéphane Nicoll 313b063aef Polish 2024-08-07 15:28:52 +02:00
Simon Baslé 7052af97a6 Polish test to focus on the FactoryBean results being mocked 2024-08-07 14:57:15 +02:00
Simon Baslé 1c893e6354 Add `@MockitoBeanSettings`, use MockitoSession with strict stubs default
This commit changes the way the `MockitoTestExecutionListener` sets up
mockito, now using the `MockitoSession` feature. Additionally, stubbing
now defaults to a STRICT mode which can be overruled with a newly
introduced annotation: `@MockitoBeanSettings`.

Closes gh-33318
2024-08-07 14:57:15 +02:00
Sanghyuk Jung ca377e4854 Remove duplicated "the" in Javadoc
See gh-33308
2024-08-03 09:52:23 +02:00
rstoyanchev 31a3119784 Add FilterRegistration's defensively
Follow-up to d2225c, which broke Boot tests because the ServletContext can
be the one of the embedded Servlet container if initializing a live server
and as well as MockMvc (e.g. via `@AutoConfigureMockMvc`).

See gh-33252
2024-07-31 14:50:08 +03:00
rstoyanchev 5ac7e74bf2 Replace test FilterRegistration with the one in testFixtures
See gh-33252
2024-07-30 11:33:47 +03:00
rstoyanchev d2225c2140 MockMvc supports FilterRegistration filter init
Closes gh-33252
2024-07-30 11:33:47 +03:00
Stéphane Nicoll 70f3b0eb91 Only register DynamicPropertySourceBeanInitializer if necessary
This commit makes sure to check first if
DynamicPropertySourceBeanInitializer has been registered before trying
to register it.

When running with AOT optimizations, the bean definition has been
contributed so there is no need to register it again when the context
customizer runs.

Closes gh-33272
2024-07-29 09:30:27 +02:00
Stéphane Nicoll 89453905cb Copy tests for multipart request builder
See gh-33229
2024-07-18 12:11:59 +02:00
Stéphane Nicoll 6590ab6bf9 Consistently set content type of the request with multipart
Closes gh-33232
2024-07-18 12:01:47 +02:00
Stéphane Nicoll 265299c790 Restore source and binary compatibility in MockMvc
Previous to this commit, MockHttpServletRequestBuilder was not binary
compatible as its methods had moved to a parent class with a generic
argument on the return type. MockMultipartHttpServletRequestBuilder
was also not source compatible as it no longed extended from
MockHttpServletRequestBuilder.

Both these changes were introduced to allow the AssertJ support to
expose builders that implement an extra AssertJ interface, without
copying the features the builders provide.

This commit restore compatibility. For MockHttpServletRequestBuilder
we simply override all methods that returns the generic type into
the resolved type.

MockMultipartHttpServletRequestBuilder is more involved. Because we
need to extend from MockHttpServletRequestBuilder, we have no other
choice than duplicating the code. For now, the abstract builder for
multipart is only used by the AssertJ support, but we can revisit this
again in a major release.

Closes gh-33229
2024-07-18 11:21:35 +02:00
Sam Brannen 3e48498663 Change MIME mapping for .js from application/javascript to text/javascript
This application/javascript MIME type is deprecated.

This commit therefore changes the MIME type mapping for *.js files from
application/javascript to text/javascript in order to align with
industry standards.

Closes gh-33197
2024-07-12 18:22:18 +02:00
rstoyanchev 545228d693 Remove unused tiles config files 2024-07-08 12:47:10 +01:00
Stéphane Nicoll f1658079a5 Polish documentation for RegisterReflection
See gh-29194
2024-07-06 09:31:12 +02:00
Stéphane Nicoll db1e6e1e26 Polish 2024-07-05 08:26:27 +02:00
Stéphane Nicoll ca8bd7b06b Simplify tests 2024-07-04 16:34:33 +02:00
Stéphane Nicoll 4bdb772d39 Introduce HttpMessageContentConverter
This commit introduces an abstraction that allows to convert HTTP
inputs to a data type based on a set of HttpMessageConverter.

Previously, the AssertJ integration was finding the first converter
that is able to convert JSON to a Map (and vice-versa) and used that
in its API. With the introduction of SmartHttpMessageConverter, exposing
a specific converter is fragile.

The added abstraction allows for converting other kind of input than
JSON if we need to do that in the future.

Closes gh-33148
2024-07-04 14:06:00 +02:00
Sam Brannen abcad5dbcf Support property placeholders in @⁠Sql script paths
Prior to this commit, paths configured via the scripts attribute in
@⁠Sql were required to be final paths without dynamic placeholders;
however, being able to make script paths dependent on the current
environment can be useful in certain testing scenarios.

This commit introduces support for property placeholders (${...}) in
@⁠Sql script paths which will be replaced by properties available in
the Environment of the test's ApplicationContext.

Closes gh-33114
2024-07-03 17:16:53 +02:00
Sam Brannen 932ce04541 Stop referring to old Spring versions in Javadoc 2024-07-01 17:50:52 +02:00
Sam Brannen cc002875c4 Support fully-qualified factory method names in @⁠TestBean
Prior to this commit, @⁠TestBean factory methods had to be defined in
the test class, one of its superclasses, or in an implemented
interface. However, users may wish to define common factory methods in
external classes that can be shared easily across multiple test classes
simply by referencing an external method via a fully-qualified method
name.

To address that, this commit introduces support for referencing a
@⁠TestBean factory method via its fully-qualified method name following
the syntax <fully-qualified class name>#<method name>.

Closes gh-33125
2024-06-30 21:16:04 +02:00
Sam Brannen c2f8d4803f Update Javadoc for @⁠TestBean 2024-06-30 21:15:56 +02:00
Sam Brannen b105fdc87a Polishing 2024-06-30 14:41:43 +02:00
Sébastien Deleuze 1d363e5a41 Use UTF-8 with JSON in MockHttpServletResponse
This commit makes MockHttpServletResponse consistent with the other
parts of the framework where the body of a response is read as an UTF-8
String when the content type is application/json or similar, overriding
the ISO-8859-1 default Servlet encoding.

Closes gh-33019
2024-06-19 09:33:43 +02:00
Stéphane Nicoll 1eaa9cd2f4 Polish 2024-06-18 16:04:19 +02:00
Stéphane Nicoll 24bbc6d80d Improve support of async request in MockMvcTester
This commit improves the handling of asynchronous requests by offering
a way to opt-in for the raw async result. This provides first class
support for asserting a request that might still be in process as well
as the asyncResult, if necessary.

See gh-33040
2024-06-18 15:19:47 +02:00
Stéphane Nicoll a7503e7200 Add explicit support for asynchronous requests in MockMvcTester
This commit makes asynchronous requests first class by providing a
MvcTestResult that represents the final, completed, state of a request
by default. Previously, the result was an intermediate step that may
require an ASYNC dispatch to be fully usable. Now it is de facto
immutable.

To make things a bit more explicit, an `.exchange(Duration)` method has
been added to provide a dedicated time to wait. The default applies a
default timeout that is consistent with what MVcResult#getAsyncResult
does.

Given that we apply the ASYNC dispatch automatically, the intermediate
response is no longer available by default. As a result, the asyncResult
is not available for assertions.

As always, it is possible to use plain MockMvc by using the `perform`
method that takes the regular RequestBuilder as an input. When this
method is invoked, no asynchronous handling is done.

Closes gh-33040
2024-06-18 13:03:09 +02:00
Stéphane Nicoll a1b0099795 Add missing debug(Writer) alternative
See gh-33059
2024-06-18 12:40:29 +02:00
Stéphane Nicoll acf73404c1 Simplify Mock[Multipart]HttpServletRequestBuilder
This commit simplifies the package private constructors on those two
builders now that the URI can be specified by dedicated builder methods.

Closes gh-33062
2024-06-18 10:10:07 +02:00
Stéphane Nicoll d76f37c90b Add multipart support for MockMvcTester
File uploads with MockMvc require a separate
MockHttpServletRequestBuilder implementation. This commit applies the
same change to support AssertJ on this builder, but for the multipart
version.

Any request builder can now use `multipart()` to "down cast" to a
dedicated multipart request builder that contains the settings
configured thus far.

Closes gh-33027
2024-06-18 09:43:08 +02:00
Stéphane Nicoll f2137c99e5 Add debug support for MvcResult
This commit adds a debug() shortcut to print the detail of a MvcResult
to ease debugging when a request did not work as expected.

Closes gh-33059
2024-06-18 06:43:26 +02:00
Stéphane Nicoll 84bcb65dd1 Streamline assertions on exceptions
This commit simplifies assertions on MockMvc requests that have failed
to process. A now general hasFailed/doesNotHaveFailed/failure provides
the necessary to assert the exception.

Any attempt to access anything from the result with an unresolved
exception still fails as before.

Closes gh-33060
2024-06-18 06:23:13 +02:00
Stéphane Nicoll 1582f5f7d1 Handle URI as candidate for a merge from a parent builder
Closes gh-33057
2024-06-17 17:06:10 +02:00
Stéphane Nicoll b8790077b9 Polish 2024-06-17 16:47:37 +02:00
Stéphane Nicoll 69c44dee99 Add support for conversion of the whole JSON document
Closes gh-33018
2024-06-13 00:12:33 +02:00
Stéphane Nicoll 1d0af7e5d3 Move TestBeanFactory to example package 2024-06-12 11:05:22 +02:00
Stéphane Nicoll 3f2d9c372d Migrate "Failing" test cases to regular tests
This commit replaces the use of EngineTestKit to test scenarios where
bean override could not process the test class. There is no need to
run an actual test for this as:

1. Regular integration tests are already validating that adding the
annotation triggers the processing as part of executing the test.
2. The entry point is a ContextCustomizer that can easily be invoked
in a regular test.

As part of harmonizing this, AbstractTestBeanIntegrationTestCase can
be nested, and BeanOverrideTestSuite serve no purpose. The tests can
be executed in an IDE without any special setup.
2024-06-12 11:03:14 +02:00
Stéphane Nicoll 1318f65223 Harmonize test class names
This commit harmonizes the class names of the bean override support by
using a consistent prefix for integration test, inspire from the one
that was used in Spring Boot
2024-06-12 08:54:24 +02:00
Stéphane Nicoll 2d48371c6d Revert polish on Bean Override support
This commit revers the removal of the `private` keyword in the examples
of the reference documentation and the tests for consistency. While the
default visibility makes the example more concise, it could imply to the
reader that the field (or the factory method) cannot be private.

Also, there is no need to multiply anything returned from `Objects.hash`
as its very distinct on its own already.
2024-06-12 08:11:18 +02:00
Sam Brannen 46dccd8f97 Polishing 2024-06-11 16:39:24 +02:00
Sam Brannen f481a4b60b Polish reference documentation 2024-06-11 16:31:56 +02:00
Arjen Poutsma 60b5bbe334 Introduce request attributes in RestClient
This commit introduces request attributes in the RestClient and
underlying infrastructure (i.e. HttpRequest).

Closes gh-32027
2024-06-11 15:51:29 +02:00
Stéphane Nicoll 2a680934ee Harmonize equals/hashCode of OverrideMetadata to use class identity
This commit harmonizes the equals/hashCode behavior of OverrideMetadata
to always take the implementation class as a factor for its identity.

This is important as two OverrideMetadata implementations could use
the same strategy and other settings while creating the override value
in a totally different way. This commit makes sure they are identified
as different.

Closes gh-33005
2024-06-11 12:03:31 +02:00
Stéphane Nicoll 1be92f82af Polish 2024-06-11 11:58:19 +02:00
Stéphane Nicoll 28f62abda4 Use the field name as a fallback qualifier for Bean Overriding
This commit harmonizes how a candidate bean definition is determined
for overriding using `@TestBean`, `@MockitoBean`, and `@MockitoSpyBean`.

Previously, a qualifier was necessary even if the name of the annotated
field matches the name of a candidate. After this commit, such candidate
will be picked up transparently, the same it is done for regular
autowiring.

This commit also reviews the documentation of the feature as considering
the field means that its name is taken into account to compute a cache
key if by-type lookup is requested.

Closes gh-32939
2024-06-11 11:30:01 +02:00
Stéphane Nicoll b9a6ba1242 Fix package tangle in bean override tests
This commit fixes a package tangle between the root bean override
package and its sub-packages. This was vastly improved with the
introduction of `@DummyBean` but we still had a few tests that were
at the wrong place.
2024-06-10 15:23:36 +02:00
Stéphane Nicoll 96302a7102 Stop using a conventional suffix for TestBean factory methods
This commit changes how factory method for `@TestBean` usage is
discovered. Previously the field name or bean name suffixed with
'TestOverride' was used. It sounds more natural to just use the
field name or bean name, leaving cases where a suffix is required
to explicitly providing the method name.

As part of this change, the exception messages have been revisited as
it's less since the method name candidates have the exact same name
as the field or bean name. A `()` is added to make it more clear the
name is for a method.

Closes gh-32940
2024-06-10 12:52:21 +02:00
Stéphane Nicoll 45e9f04b57 Remove TestOverrideMetadata and use dummy implementation
TestOverrideMetadata was doing byName lookup by default, even without
a bean name. This makes writing tests that need to do by type lookup
way more complicated that it should.

This commit introduces DummyBean, that merely replaces two types with
hardcoded values. It also exposes the raw attribute of OverrideMetadata
and doesn't override anything from it to make sure the behavior is not
altered.

Closes gh-32982
2024-06-10 11:27:27 +02:00