Commit Graph

3215 Commits

Author SHA1 Message Date
Sébastien Deleuze 2641b5d783 Polishing
See gh-35861
2025-11-26 10:27:01 +01:00
Vincent Potucek 83bbf16e6c Modernize java.time API usages
See gh-35861
Signed-off-by: Vincent Potucek <vpotucek@me.com>
2025-11-26 10:24:34 +01:00
rstoyanchev f80b79bc45 Configure JsonPath in RestTestClient with MappingProvider
Closes gh-35793
2025-11-18 15:46:57 +00:00
rstoyanchev adffd3dcf6 Fix JsonConverterDelegate initialization in RestTestClient
If the RestClient was built with default message converters, then
in mutate, the saved builder also has 0 converters, and adding a
interferes with default registrations.

We need to check if there are no converters at all, and if so
use the default registrations.

See gh-35793
2025-11-18 15:46:57 +00:00
Sam Brannen b133c2b346 Document semantics of SpringExtension.getApplicationContext()
Since getApplicationContext() was originally not intended to be part of
the public API, its Javadoc is intentionally sparse. However, since it
is actually a public API used by third parties, this commit improves the
documentation for getApplicationContext() by pointing out that invoking
the method actually results in the context being eagerly loaded, which
may not be desired.

This commit also updates the Javadoc for supportsParameter() along the
same lines.

Closes gh-35764
2025-11-15 15:44:21 +01:00
Sam Brannen 648629fab2 Polish Javadoc for SpringExtension 2025-11-15 15:42:32 +01:00
rstoyanchev b648e226cd Update deprecation notices in WebTestClient 2025-11-12 10:54:46 +00:00
Sam Brannen e4f8ae0338 Stop setting local port in HtmlUnitRequestBuilder
This commit picks up where commit a0baeae9cf left off.

Specifically, in order to align with the behavior of
AbstractMockHttpServletRequestBuilder, HtmlUnitRequestBuilder no longer
sets the local port in the MockHttpServletRequest.

See gh-35709
2025-11-10 17:20:23 +01:00
Sam Brannen 386c6cafc9 Fix typo in test 2025-11-07 16:13:16 +01:00
Sam Brannen a0baeae9cf Fix port handling in HtmlUnitRequestBuilder
Prior to this commit, HtmlUnitRequestBuilder set the server port in the
MockHttpServletRequest to -1 if the URL did not contain an explicit
port. However, that can lead to errors in consumers of the request that
do not expect an invalid port number.

In addition, HtmlUnitRequestBuilder always set the remote port in the
MockHttpServletRequest to the value of the server port, which does not
make sense, since the remote port of the client has nothing to do with
the port on the server.

To address those issues, this commit revises HtmlUnitRequestBuilder so
that it:

- Does not set the server and local ports if the explicit or derived
  default value is -1.
- Consistently sets the server and local ports to the same valid value.
- Does not set the remote port.

Closes gh-35709
2025-11-06 15:03:41 +01:00
Sam Brannen 934394eb2a Document AOT workaround for @⁠PersistenceContext & @⁠PersistenceUnit in tests
Although @⁠PersistenceContext and @⁠PersistenceUnit are still not
supported in tests running in AOT mode, as of Spring Framework 7.0,
developers can inject an EntityManager or EntityManagerFactory into
tests using @⁠Autowired instead of @⁠PersistenceContext and
@⁠PersistenceUnit, respectively.

See commit 096303c477
See gh-33414
Closes gh-31442
2025-11-05 14:30:41 +01:00
rstoyanchev 02e928e4e3 Docs for the WebTestClient AssertJ integration
Closes gh-35737
2025-11-05 10:06:06 +00:00
rstoyanchev 7445f542f4 AssertJ support for WebTestClient
Closes gh-35737
2025-11-05 10:06:06 +00:00
rstoyanchev cd8690254b Polishing in RestTestClient AssertJ support
See gh-35701
2025-11-05 10:06:05 +00:00
rstoyanchev 125002844e Add JsonConverterDelegate
The JsonConverterDelegate interface replaces usages of
HttpMessageContentConverter to provides the flexibility to use either
message converters or WebFlux codecs.

HttpMessageContentConverter is deprecated, and replaced with a package
private copy (DefaultJsonConverterDelegate) in the
org.springframework.test.json package that is accessible through
a static method on JsonConverterDelegate.

See gh-35737
2025-11-05 10:04:56 +00:00
Sam Brannen b019ebee6e Document that @⁠MockitoSpyBean cannot spy on a scoped proxy
See gh-35722
2025-11-04 13:42:22 +01:00
Sam Brannen c0c94d5d86 Reject attempt to use @⁠MockitoSpyBean with a scoped proxy
Prior to this commit, an attempt to use @⁠MockitoSpyBean to spy on a
scoped proxy configured with
@⁠Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) resulted in an
exception thrown by Mockito when the spy was stubbed. The exception
message stated "Failed to unwrap proxied object" but did not provide
any further insight or context for the user.

The reason is that ScopedProxyFactoryBean is used to create such a
scoped proxy, which uses a SimpleBeanTargetSource, which is not a
static TargetSource. Consequently,
SpringMockResolver.getUltimateTargetObject(Object) is not able to
unwrap the proxy.

In order to improve diagnostics for users, this commit eagerly detects
an attempt to spy on a scoped proxy and throws an exception with a
meaningful message. The following is an example, trimmed stack trace
from the test suite.

org.springframework.beans.factory.BeanCreationException:
  Error creating bean with name 'myScopedProxy': Post-processing of
  FactoryBean's singleton object failed
  ...
Caused by: java.lang.IllegalStateException:
  @⁠MockitoSpyBean cannot be applied to bean 'myScopedProxy', because
  it is a Spring AOP proxy with a non-static TargetSource. Perhaps you
  have attempted to spy on a scoped proxy, which is not supported.
    at ...MockitoSpyBeanOverrideHandler.createSpy(MockitoSpyBeanOverrideHandler.java:78)

Closes gh-35722
2025-11-04 13:27:05 +01:00
Sam Brannen 1fc446b3fc Provide AOT support for @⁠Nested classes in a @⁠ParameterizedClass
This commit adds AOT support for discovering @⁠Nested test classes
within a @⁠ClassTemplate test class, which includes
@⁠ParameterizedClass test classes.

Closes gh-35744
2025-11-03 12:25:37 +01:00
Sam Brannen 096303c477 Use @⁠Autowired EntityManager in Jupiter JPA tests
Switching from @⁠PersistenceContext to @⁠Autowired for dependency
injection in tests allows such tests to participate in AOT processing.

Note, however, that we still have TestNG-based tests that use
@⁠PersistenceContext — for example, AbstractEjbTxDaoTestNGTests.

See gh-29122
See gh-31442
See gh-33414
2025-11-03 12:06:57 +01:00
rstoyanchev 1ee05cc180 Update contribution
Closes gh-35698
2025-11-03 09:51:25 +00:00
Thomas Recloux 8e046b8a3c Clone RestTestClientBuilder in mutate()
See gh-35698

Signed-off-by: Thomas Recloux <trecloux@purse.eu>
2025-11-03 09:32:19 +00:00
Sam Brannen 81d69216b1 Rename to MockMvcRestTestClientTests and remove use of SpringExtension
The MockMvcClientHttpRequestFactoryTests variant for RestTestClient was
copied from MockMvcClientHttpRequestFactoryTests which actually uses
MockMvcClientHttpRequestFactory. In addition,
MockMvcClientHttpRequestFactoryTests unnecessarily used the
SpringExtension.

This commit therefore renames the class to MockMvcRestTestClientTests
and removes the use of the SpringExtension.

See gh-29122
2025-11-02 16:47:38 +01:00
Sam Brannen 960df8e7a1 Merge branch '6.2.x' 2025-11-01 14:34:00 +01:00
Sam Brannen 661dca5e54 Remove accidental use of SpringExtension in ServletContextAwareBeanWacTests 2025-11-01 14:26:08 +01:00
Sam Brannen b976010a9e Simplify MockMvcClientHttpRequestFactoryTests 2025-11-01 14:26:07 +01:00
Sam Brannen 324b254849 Upgrade to JUnit 6.0.1 and prevent AOT scanning failure for JUnit 4 tests
This commit upgrades our test suite to use JUnit 6.0.1 and removes the
systemProperty("junit.platform.discovery.issue.severity.critical", "WARNING")
configuration from spring-test.gradle, so that all discovery issues will
fail the build for the spring-test module as well.

In addition, this commit prevents potential AOT test scanning failures
for JUnit 4 tests by setting the
"junit.vintage.discovery.issue.reporting.enabled" configuration
parameter to "false" in TestClassScanner.

See https://github.com/junit-team/junit-framework/issues/5030
Closes gh-35740
2025-11-01 12:28:59 +01:00
rstoyanchev a23c3775a8 Docs for the RestTestClient AssertJ integration
Closes gh-35701
2025-10-30 17:34:00 +00:00
Sam Brannen d33f2c6f73 Fix Javadoc links
See gh-35697
2025-10-30 14:43:29 +01:00
rstoyanchev a8349e1c5c AssertJ support for RestTestClient
See gh-35701
2025-10-30 13:30:46 +00:00
Sam Brannen 41ae13df5d Use test-method scoped ExtensionContext in the SpringExtension
As of Spring Framework 6.2.13, we support JUnit Jupiter 5.12's
ExtensionContextScope.TEST_METHOD behavior in the SpringExtension and
the BeanOverrideTestExecutionListener; however, users can only benefit
from that if they explicitly set the following configuration parameter
for their entire test suite, which may have adverse effects on other
third-party JUnit Jupiter extensions.

junit.jupiter.extensions.testinstantiation.extensioncontextscope.default=test_method

For Spring Framework 7.0, in order to support dependency injection into
test class constructors and fields in @⁠Nested test class hierarchies
from the same ApplicationContext that is already used to perform
dependency injection into lifecycle and test methods (@⁠BeforeEach,
@⁠AfterEach, @⁠Test, etc.), we have decided to configure the
SpringExtension to use ExtensionContextScope.TEST_METHOD by default. In
addition, we have decided to provide a mechanism for users to switch
back to the legacy "test-class scoped ExtensionContext" behavior in
case third-party TestExecutionListener implementations are not yet
compatible with test-method scoped ExtensionContext and TestContext
semantics.

This commit achieves the above goals as follows.

- A new @⁠SpringExtensionConfig annotation has been introduced, which
  allows developers to configure the effective ExtensionContext scope
  used by the SpringExtension.

- The SpringExtension now overrides
  getTestInstantiationExtensionContextScope() to return
  ExtensionContextScope.TEST_METHOD.

- The postProcessTestInstance() and resolveParameter() methods in the
  SpringExtension now find the properly scoped ExtensionContext for the
  supplied test class, based on whether the @⁠Nested test class
  hierarchy is annotated with
  @⁠SpringExtensionConfig(useTestClassScopedExtensionContext = true).

See gh-35680
See gh-35716
Closes gh-35697
2025-10-30 13:54:16 +01:00
Sam Brannen 61453bce6c Merge branch '6.2.x' 2025-10-30 11:25:55 +01:00
Sam Brannen 9c24f7ba8c Document test-method scoped TestContext semantics
Although gh-35680 introduced support for JUnit Jupiter's TEST_METHOD
ExtensionContextScope in the SpringExtension and
BeanOverrideTestExecutionListener, those were internal changes that are
not directly visible by TestExecutionListener authors.

However, in order to be compatible with a test-method scoped
TestContext (which takes its values from the current Jupiter
ExtensionContext), existing third-party TestExecutionListener
implementations may need to be modified similar to how
BeanOverrideTestExecutionListener was modified in commit d24a31d469.

To raise awareness of how to deal with such issues, this commit
documents test-method TestContext semantics for TestExecutionListener
authors.

Closes gh-35716
2025-10-30 11:22:25 +01:00
rstoyanchev 0319fe9211 Deprecate Hamcrest use in WebTestClient
Closes gh-35703
2025-10-29 09:50:36 +00:00
rstoyanchev b4c6300ac6 Remove Hamcrest use from RestTestClient
Closes gh-35702
2025-10-29 09:50:36 +00:00
Sébastien Deleuze 694224f1a6 Remove outdated Spring version mentions
Close gh-35696
2025-10-28 13:58:09 +01:00
Sam Brannen 65daea1e2a Polish tests for the SpringExtension 2025-10-27 12:18:36 +01:00
Sam Brannen 7c83892b9b Merge branch '6.2.x' 2025-10-22 19:16:07 +02:00
Sam Brannen 9ee16b8ea3 Switch back to HSQL for JpaEntityListenerTests on main 2025-10-22 19:14:26 +02:00
Sam Brannen 5f5b19a898 Merge branch '6.2.x' 2025-10-22 18:59:57 +02:00
Sam Brannen e61884e4bd Introduce tests for "No transaction in progress for @⁠Nested test class"
Since we now officially support the TEST_METHOD ExtensionContextScope
(see gh-35676 and gh-35680), this commit introduces tests which
demonstrate that the issue raised in gh-34576 is no longer an "issue" if
the user indirectly configures the SpringExtension to use the TEST_METHOD
scope via the "junit.jupiter.extensions.testinstantiation.extensioncontextscope.default"
configuration parameter.
2025-10-22 18:58:08 +02:00
Sam Brannen 0f2fc79fb7 Update exception message to use correct test class
See gh-35680
2025-10-22 18:04:13 +02:00
Sam Brannen fd6b18a07a Merge branch '6.2.x' 2025-10-22 17:09:38 +02:00
Sam Brannen ba70c1384a Polish SpringExtension internals 2025-10-22 17:07:53 +02:00
Sam Brannen d24a31d469 Support JUnit Jupiter ExtensionContextScope.TEST_METHOD
Historically, @⁠Autowired fields in an enclosing class of a @⁠Nested
test class have been injected from the ApplicationContext for the
enclosing class. If the enclosing test class and @⁠Nested test class
share the same ApplicationContext configuration, things work as
developers expect. However, if the enclosing class and @⁠Nested test
class have different ApplicationContexts, that can lead to
difficult-to-debug scenarios. For example, a bean injected into the
enclosing test class will not participate in a test-managed transaction
in the @⁠Nested test class (see gh-34576).

JUnit Jupiter 5.12 introduced a new ExtensionContextScope feature which
allows the SpringExtension to behave the same for @⁠Autowired fields as
it already does for @⁠Autowired arguments in lifecycle and test
methods. Specifically, if a developer sets the ExtensionContextScope to
TEST_METHOD — for example, by configuring the following configuration
parameter as a JVM system property or in a `junit-platform.properties`
file — the SpringExtension already supports dependency injection from
the current, @⁠Nested ApplicationContext in @⁠Autowired fields in an
enclosing class of the @⁠Nested test class.

junit.jupiter.extensions.testinstantiation.extensioncontextscope.default=test_method

However, there are two scenarios that fail as of Spring Framework
6.2.12.

1. @⁠TestConstructor configuration in @⁠Nested class hierarchies.
2. Field injection for bean overrides (such as @⁠MockitoBean) in
   @⁠Nested class hierarchies.

Commit 82c34f7b51 fixed the SpringExtension to support scenario #2
above.

To fix scenario #1, this commit revises
BeanOverrideTestExecutionListener's injectField() implementation to
look up the fields to inject for the "current test instance" instead of
for the "current test class".

This commit also introduces tests for both scenarios.

See gh-34576
See gh-35676
Closes gh-35680
2025-10-22 17:00:44 +02:00
Sam Brannen e937d8ee8a Remove reference to @⁠javax.inject.Inject on main 2025-10-21 18:02:12 +02:00
Sam Brannen bda8f20b0b Merge branch '6.2.x' 2025-10-21 17:43:10 +02:00
Sam Brannen 82c34f7b51 Introduce isAutowirableConstructor(Executable,PropertyProvider) in TestConstructorUtils
This commit introduces a new isAutowirableConstructor(Executable, PropertyProvider)
overload in TestConstructorUtils and deprecates all other existing variants
for removal in 7.1.

Closes gh-35676
2025-10-21 17:37:04 +02:00
Sam Brannen cb0f26a152 Use consistent naming for Bean Override test classes
cherry-picked from c6e5cfe03d
2025-10-21 16:25:12 +02:00
Sébastien Deleuze 79795c8e09 Avoid compilation error on WebTestClient without Hamcrest
This commit removes JSpecify (TYPE_USE) annotations on
org.hamcrest.Matcher parameters to prevent a related
"Cannot attach type annotations" error when Hamcrest
(optional dependency) is not in the classpath with Java > 22.

Closes gh-35666
2025-10-21 12:18:10 +02:00
Juergen Hoeller 83edf6a4aa Adapt test for MockServletContext version
See gh-35671
2025-10-21 01:07:03 +02:00