GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix #2616 wrong stub for nested static by fishautumn · Pull Request #2685 · mockito/mockito

Conversation

@fishautumn

@fishautumn fishautumn commented Jun 14, 2022

Copy link
Copy Markdown
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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
fishautumn added 2 commits June 14, 2022 18:17
@fishautumn
Fix mockito#2616 wrong stub for nested static
0b0a7cc
@fishautumn
test: move SpyStubbingMisuse to SpyAsDefaultMockUsage as it becomes s…
81668d4
…uccessful
@codecov-commenter

codecov-commenter commented Jun 15, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

TimvdLippe
TimvdLippe approved these changes Jun 18, 2022
View reviewed changes

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@TimvdLippe
TimvdLippe merged commit 807b121 into mockito:main Jun 18, 2022
@gastaldi gastaldi mentioned this pull request Aug 15, 2022
Bump mockito-core from 4.6.1 to 4.7.0 forge/roaster#249
Merged
@DaveTryon DaveTryon mentioned this pull request Jan 11, 2023
Regression starting in mockito-inline 4.7.0? #2860
Closed
5 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@TimvdLippe TimvdLippe TimvdLippe approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@fishautumn @codecov-commenter @TimvdLippe
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.