GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing github.com — Fix #2616 wrong stub for nested static by fishautumn · Pull Request #2685 · mockito/mockito Conversation
Contributor
It fixed 2 nested static spy cases (see added unittest for detail) case1 private static class StaticInt { static int getInt() { return 1; } } private static class StaticStr { static String getStr() { return Integer.toString(StaticInt.getInt()); } } @Test public void keep_ongoing_stub_when_spy() { try (MockedStatic<StaticInt> mockInt = mockStatic(StaticInt.class); MockedStatic<StaticStr> mockStr = mockStatic(StaticStr.class, CALLS_REAL_METHODS)) { mockStr.when(StaticStr::getStr).thenReturn("1"); // here, StaticInt.getInt() is considered as the stubbing point assertEquals("1", StaticStr.getStr()); } } case2 private static class StaticWithException { static String getString() { return Integer.toString(getInt()); } static int getInt() { throw new NullPointerException(); } } @Test public void keep_ongoing_stub_when_exception() { try (MockedStatic<StaticWithException> mock = mockStatic(StaticWithException.class, CALLS_REAL_METHODS)) { mock.when(StaticWithException::getString).thenReturn("1"); assertEquals("1", StaticWithException.getString()); } } the fix is in org.mockito.internal.handler.MockHandlerImpl#handle branch (stubbing == null) branch only:
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
Codecov Report
@@ Coverage Diff @@ ## main #2685 +/- ## ============================================ + Coverage 86.26% 86.27% +0.01% - Complexity 2776 2777 +1 ============================================ Files 315 315 Lines 8334 8335 +1 Branches 1037 1037 ============================================ + Hits 7189 7191 +2 + Misses 874 873 -1 Partials 271 271
Continue to review full report at Codecov.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Nice and easy fix! Interesting that the spy misusing was so subtle, but glad to see that working as intended as well now.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.