Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2616 wrong stub for nested static #2685

Merged
merged 2 commits into from Jun 18, 2022
Merged

Conversation

fishautumn
Copy link
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:

  • add a mockingProgress().reportOngoingStubbing() call
  • add try-finally block to ensure they are invoked.

@codecov-commenter
Copy link

codecov-commenter commented Jun 15, 2022

Codecov Report

Merging #2685 (81668d4) into main (ec27830) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@             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              
Impacted Files Coverage Δ
.../org/mockito/internal/handler/MockHandlerImpl.java 100.00% <100.00%> (ø)
...in/java/org/mockito/internal/MockedStaticImpl.java 80.51% <0.00%> (+1.29%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ec27830...81668d4. Read the comment docs.

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants