summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <android-test-infra-autosubmit@system.gserviceaccount.com>2024-01-29 20:05:18 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2024-01-29 20:05:18 +0000
commite1e7a35af3cd7d530343c0a2c0bc1ce40edd6508 (patch)
tree7e9f5ae3e621bf35333c6a2870f04cc453bf107f
parentfb1f9e9ccd2c2ae32326c74346cb33d7f925190a (diff)
parentece464fd0a2f69b4dfe7e57ff9d2e1c04f2172ae (diff)
downloadmockito-e1e7a35af3cd7d530343c0a2c0bc1ce40edd6508.tar.gz
Merge "Fixed DefaultMockitoSession constructor." into main am: ece464fd0a
Original change: https://android-review.googlesource.com/c/platform/external/mockito/+/2929425 Change-Id: Icf68eab9be471270f1f0cd7a74d19089b103b345 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--README.version1
-rw-r--r--src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java21
2 files changed, 20 insertions, 2 deletions
diff --git a/README.version b/README.version
index 33436a1..1a82721 100644
--- a/README.version
+++ b/README.version
@@ -9,4 +9,5 @@ Dexmaker module.
The source can be updated using the update_source.sh script.
Local Modifications:
+ Fixed DefaultMockitoSession constructor. (I14ed7c032a974c3a65caaf091d36d9667ea331b6)
New API to clean up all inline mocks after test (8bdfbf053ab6e4fc14a3eaecb613f5838fdf0f09)
diff --git a/src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java b/src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java
index c900bf7..c81baf2 100644
--- a/src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java
+++ b/src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java
@@ -32,8 +32,25 @@ public class DefaultMockitoSession implements MockitoSession {
} catch (RedundantListenerException e) {
Reporter.unfinishedMockingSession();
}
- for (Object testClassInstance : testClassInstances) {
- MockitoAnnotations.initMocks(testClassInstance);
+ try {
+ for (Object testClassInstance : testClassInstances) {
+ MockitoAnnotations.initMocks(testClassInstance);
+ }
+ } catch (RuntimeException | Error e) {
+ try {
+ // TODO: ideally this scenario should be tested on DefaultMockitoSessionBuilderTest,
+ // but we don't have any Android.bp project to run it.
+ // Besides, the latest Mockito code (https://github.com/mockito/mockito/blob/main/src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java
+ // at the time this patch was merged) has a different workflow, where the listener
+ // is marked as dirty when an exception is thrown, so we're forking the solution.
+ Mockito.framework().removeListener(listener);
+ } catch (RuntimeException | Error e2) {
+ // Ignore it, as the real failure is e, thrown at the end
+ System.err.println("DefaultMockitoSession: ignoring exception thrown when removing "
+ + "listener " + listener);
+ e2.printStackTrace(System.err);
+ }
+ throw e;
}
}