* pr/33850:
Polish "Prefer modified resources over the originals in TestCompiler"
Prefer modified resources over the originals in TestCompiler
Closes gh-33850
Previously, when the test compiler had been seeded with a resource
file, any modifications to this resource performed during
compilation would be lost as this original content would always
be returned.
This commit updates the DynamicJavaFileManager to always store
the dynamic resource in the dynamicResourceFiles map, irrespective
of whether it's being created afresh or from some existing resource
content. This ensures that any modifications made to the resource
can be retrieved later on.
Similarly, DynamicClassLoader has been updated to prefer dynamic
resource files over any original resource files. This ensures that
the resource that it finds reflects any modifications that have
been made to it.
See gh-33850
The `HttpHeaders#headerSet` method is intended as a drop-in replacement
for `entrySet` that guarantees a single casing for all header names
reported during the iteration, as the cost of some overhead but with
support for iterator removal and entry value-setting.
The `formatHeaders` static method is also altered to do a similar
deduplication of casing variants, but now additionally mentions
"with native header names [native name set]" if the native name set
contains casing variants.
Closes gh-33823
This commit introduces a TestBeanReflectiveProcessor that registers
GraalVM native image reflection hints for a fully-qualified method name
configured via @TestBean.
Closes gh-33836
Prior to this commit, the static factory methods in MockReset (such as
MockReset.before() and MockReset.after()) could only be applied to
beans within the ApplicationContext if the test class declared at least
one field annotated with either @MockitoBean or @MockitoSpyBean.
However, the Javadoc states that it should be possible to apply
MockReset directly to any mock in the ApplicationContext using the
static methods in MockReset.
To address that, this commit reworks the "enabled" logic in
MockitoResetTestExecutionListener as follows.
- We no longer check for the presence of annotations from the
org.springframework.test.context.bean.override.mockito package to
determine if MockReset is enabled.
- Instead, we now rely on a new isEnabled() method to determine if
MockReset is enabled.
The logic in the isEnabled() method still relies on the mockitoPresent
flag as an initial check; however, mockitoPresent only determines if
Mockito is present in the classpath. It does not determine if Mockito
can actually be used. For example, it does not detect if the necessary
reachability metadata has been registered to use Mockito within a
GraalVM native image.
To address that last point, the isEnabled() method performs an
additional check to determine if Mockito can be used in the current
environment. Specifically, it invokes Mockito.mockingDetails().isMock()
which in turn initializes core Mockito classes without actually
attempting to create a mock. If that fails, that means that Mockito
cannot actually be used in the current environment, which typically
indicates that GraalVM reachability metadata has not been registered
for the org.mockito.plugins.MockMaker in use (such as the
ProxyMockMaker).
In addition, isEnabled() lazily determines if Mockito can be
initialized, since attempting to detect that during static
initialization results in a GraalVM native image error stating that
Mockito internals were "unintentionally initialized at build time".
If Mockito cannot be initialized, MockitoResetTestExecutionListener
logs a DEBUG level message providing access to the corresponding stack
trace, and MockReset support is disabled.
Closes gh-33829
This commit verifies that MockReset.before() and MockReset.after() are
supported for beans within the ApplicationContext.
However, the test class must declare a field annotated with either
@MockitoBean or @MockitoSpyBean in order for the MockReset feature to
be triggered.
See gh-33742
Spring Boot has honored @Primary for selecting which candidate bean
@MockBean and @SpyBean should mock or spy since Spring Boot 1.4.3;
however, the support for @Primary was not ported from Spring Boot to
Spring Framework's new Bean Overrides feature in the TestContext
framework.
To address that, this commit introduces support for @Primary for
selecting bean overrides -- for example, for annotations such as
@TestBean, @MockitoBean, and @MockitoSpyBean.
See https://github.com/spring-projects/spring-boot/issues/7621
Closes gh-33819
This commit introduces a `@CheckReturnValue` annotation,
inspired from org.jetbrains.annotations.CheckReturnValue,
that specifies that the method return value must be used.
See gh-33818
This commit introduces integration tests which verify that Bean
Overrides (for example, @MockitoBean and @MockitoSpyBean) can select
a single candidate bean to override when there are multiple candidates
that match the required type.
To "select" the desired bean, these tests rely on one of the following.
- explicit bean name in the bean override annotation
- explicit @Qualifier on the bean override field
- explicit @Primary on one of the candidate beans
However, the @Primary tests are currently @Disabled until @Primary
is honored in the Spring TestContext Framework.
See gh-33742