-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honor MockReset without @MockitoBean or @MockitoSpyBean fields
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
- Loading branch information
Showing
3 changed files
with
67 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters