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

Mockito remains in unfinished stubbing state if a stubbing fails #1514

Closed
5 tasks done
dmitry-timofeev opened this issue Oct 9, 2018 · 4 comments
Closed
5 tasks done
Assignees
Labels

Comments

@dmitry-timofeev
Copy link
Contributor

If one of doThrow instructions failed due to InstantiationError, Mockito remains in "unfinished stubbing" state and can no longer mock anything.

I had this issue in a single JUnit 4 test, but the fact that Mockito remained in "unfinished stubbing" state used to result in all tests being marked as failed (I couldn't reproduce the last effect with the latest Mockito neither in JUnit 4 nor with JUnit 5 now, so the issue is applicable to a single test invocation only).

import java.util.List;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.mockito.Mockito;

class UninstantiableTest {

  @RepeatedTest(2)
  void tryThrow01(RepetitionInfo i) {
    if (i.getCurrentRepetition() == 1) {
      // Stub using uninstantiable exception.
      try {
        List mock = Mockito.mock(List.class);

        Mockito.doThrow(UninstantiableException.class).when(mock).clear();
      } catch (InstantiationError expected) {
        // Get a j.l.InstantiationError.
      }
    }

    // Try to stub another mock, this time using a valid exception type.
    {
      List mock = Mockito.mock(List.class);

      // The following operation results in "UnfinishedStubbing"
      Mockito.doThrow(RuntimeException.class).when(mock).clear();
    }
  }

  abstract static class UninstantiableException extends RuntimeException {}
}

Mockito version 2.23.0

check that

  • The mockito message in the stacktrace have useful information, but it didn't help
  • The problematic code (if that's possible) is copied here;
    Note that some configuration are impossible to mock via Mockito
  • Provide versions (mockito / jdk / os / any other relevant information)
  • Provide a Short, Self Contained, Correct (Compilable), Example of the issue
    (same as any question on stackoverflow.com)
  • Read the contributing guide
@dmitry-timofeev
Copy link
Contributor Author

A fix (if this is an issue) might be simple:

Index: src/main/java/org/mockito/internal/stubbing/StubberImpl.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/org/mockito/internal/stubbing/StubberImpl.java	(revision 11e28796ecd89470ca50632e0fe9b2e4260e323a)
+++ src/main/java/org/mockito/internal/stubbing/StubberImpl.java	(date 1532965613000)
@@ -93,6 +93,9 @@
         } catch (RuntimeException instantiationError) {
             mockingProgress().reset();
             throw instantiationError;
+        } catch (InstantiationError instantiationError) {
+            mockingProgress().reset();
+            throw instantiationError;
         }
         return doThrow(e);
     }

@mockitoguy
Copy link
Member

@dmitry-timofeev, do you want to take a stab at it? Please provide top level test that demonstrates the problem, too. It would be really helpful. Thank you!

@mockitoguy mockitoguy added bug and removed strictness labels Nov 23, 2018
@dmitry-timofeev
Copy link
Contributor Author

Hi @mockitoguy , thank you for having a look. I'm a little busy at work, therefore I won't be able to in the coming weeks.

@mockitoguy
Copy link
Member

That's fair, we will try find time to work on this. Thank you for reporting!

@mockitoguy mockitoguy self-assigned this Nov 27, 2018
mockitoguy added a commit that referenced this issue Nov 27, 2018
When stubbing with bad throwables we were leaving state behind. Fixes #1514
SamBarker pushed a commit to SamBarker/mockito that referenced this issue Feb 25, 2019
When stubbing with bad throwables we were leaving state behind. Fixes mockito#1514
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants