diff options
author | Liran Binyamin <liranb@google.com> | 2024-04-03 16:04:11 -0400 |
---|---|---|
committer | Liran Binyamin <liranb@google.com> | 2024-04-03 16:04:11 -0400 |
commit | db4edcf60c7b9b2961681d8f44a248ff9117c218 (patch) | |
tree | 787b954a31dc7882ce69eb6824e294edc8aedd49 | |
parent | e4a0cddc68a5f1eda817b8d9e0ebacb73449d7f1 (diff) | |
download | mockito-db4edcf60c7b9b2961681d8f44a248ff9117c218.tar.gz |
Reapply "Fixed DefaultMockitoSession constructor"
It was not removing the UniversalTestListener when initmocks()
failed, which would cause subsequent tests to fail because they could
not start a new session.
To test it, run the following command in a pristine device running
Android TM (without an updated adservices apk) - a lot of tests will
fail due to missing classes and methods.
$ atest AdServicesServiceCoreUnitTests 2>&1 | tee ~/tmp/log.txt && grep "Unfinished mocking session" ~/tmp/log.txt | wc
Without this patch, it would return non zero occurrences; with it, it's 0.
Test: manual verification (see above)
Bug: 140773999
Change-Id: I98389077c09af406ac46514d49d4b546b9d5690d
-rw-r--r-- | README.version | 1 | ||||
-rw-r--r-- | src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java | 17 |
2 files changed, 15 insertions, 3 deletions
diff --git a/README.version b/README.version index a738946..3cce812 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 a472f55..c3f15f3 100644 --- a/src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java +++ b/src/main/java/org/mockito/internal/framework/DefaultMockitoSession.java @@ -34,9 +34,20 @@ public class DefaultMockitoSession implements MockitoSession { for (Object testClassInstance : testClassInstances) { MockitoAnnotations.initMocks(testClassInstance); } - } catch (RuntimeException e) { - //clean up in case 'initMocks' fails - listener.setListenerDirty(); + } 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; } } |