Repository: mockito/mockito
Issue: Stubbing not stopped properly when running suite of tests

Hey,

I'm not sure the title describes the problem in the best way but I found the following.

Consider the two tests:
    
```java
public class MockitoMisusingUnitTest {

    @Test
    public void givenNotASpy_whenDoReturn_thenThrowNotAMock() {
        try {
            List<String> list = new ArrayList<String>();

            Mockito.doReturn(100, Mockito.withSettings().lenient())
                .when(list)
                .size();

            fail("Should have thrown a NotAMockException because 'list' is not a mock!");
        } catch (NotAMockException e) {
            assertThat(e.getMessage(), containsString("Argument passed to when() is not a mock!"));
        }
    }
}
```

```java
@RunWith(MockitoJUnitRunner.class)
public class MockitoVoidMethodsUnitTest {

    @Test
    public void whenAddCalledValueCaptured() {
        ArrayList<String> mockVoid = mock(ArrayList.class);
        ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class);
        doNothing().when(mockVoid).add(any(Integer.class), valueCapture.capture());
        mockVoid.add(0, "captured");
        assertEquals("captured", valueCapture.getValue());
    }

}
```

When run independently pass. When run together from eclipse or mvn the second test `MockitoVoidMethodsUnitTest.whenAddCalledValueCaptured` fails but because of something detected in the other test:
```
org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at org.baeldung.mockito.misusing.MockitoMisusingUnitTest.givenNotASpy_whenDoReturn_thenThrowNotAMock(MockitoMisusingUnitTest.java:28)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

-> at org.baeldung.mockito.MockitoVoidMethodsUnitTest.whenAddCalledValueCaptured(MockitoVoidMethodsUnitTest.java:30)
```

This is the call stack:
```
MockingProgressImpl.validateState() line: 109	
MockingProgressImpl.stubbingStarted() line: 98	
MockitoCore.stubber(Strictness) line: 179	
MockitoCore.stubber() line: 174	
Mockito.doNothing() line: 2421	
MockitoVoidMethodsUnitTest.whenAddCalledValueCaptured() line: 30	
```

And the method where it then fails inside `MockingProgress.java`:
```java
public void validateState() {
    validateMostStuff();

    //validate stubbing:
    if (stubbingInProgress != null) {
        Location temp = stubbingInProgress;
        stubbingInProgress = null;
        throw unfinishedStubbing(temp);
    }
}
```

I think this is a bug, tests should be independent and not cause side affects on each other. Maybe something is not cleaned up properly between test runs.

If I add this tear down to `MockitoMisusingUnitTest` then it works ok:
```java
    @After
    public void tearDown() {
        ThreadSafeMockingProgress.mockingProgress().reset();
    }	
```

I don't mind to have a go at fixing it, but I would need some help or hints. This was tested with the latest version 2.24.5.

Thanks

Issue comments:
Any thoughts on this one? I'm just curious to know.

Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/java/org/mockito/internal/stubbing/StubberImpl.java b/src/main/java/org/mockito/internal/stubbing/StubberImpl.java
index 5a599c78aa..84f1e3c2e5 100644
--- a/src/main/java/org/mockito/internal/stubbing/StubberImpl.java
+++ b/src/main/java/org/mockito/internal/stubbing/StubberImpl.java
@@ -4,6 +4,7 @@
  */
 package org.mockito.internal.stubbing;
 
+import org.mockito.internal.configuration.plugins.Plugins;
 import org.mockito.internal.stubbing.answers.CallsRealMethods;
 import org.mockito.internal.stubbing.answers.Returns;
 import org.mockito.internal.stubbing.answers.ThrowsException;
@@ -15,7 +16,6 @@
 import java.util.LinkedList;
 import java.util.List;
 
-import org.mockito.internal.configuration.plugins.Plugins;
 import static org.mockito.internal.exceptions.Reporter.notAMockPassedToWhenMethod;
 import static org.mockito.internal.exceptions.Reporter.notAnException;
 import static org.mockito.internal.exceptions.Reporter.nullPassedToWhenMethod;
@@ -36,10 +36,12 @@ public StubberImpl(Strictness strictness) {
     @Override
     public <T> T when(T mock) {
         if (mock == null) {
+            mockingProgress().reset();
             throw nullPassedToWhenMethod();
         }
 
         if (!isMock(mock)) {
+            mockingProgress().reset();
             throw notAMockPassedToWhenMethod();
         }
 


Test patch:
diff --git a/src/test/java/org/mockitousage/stubbing/MisusingStubbingTest.java b/src/test/java/org/mockitousage/stubbing/MisusingStubbingTest.java
new file mode 100644
index 0000000000..2e5bf26d2c
--- /dev/null
+++ b/src/test/java/org/mockitousage/stubbing/MisusingStubbingTest.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2007 Mockito contributors
+ * This program is made available under the terms of the MIT License.
+ */
+package org.mockitousage.stubbing;
+
+import org.junit.Test;
+import org.mockitoutil.TestBase;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.validateMockitoUsage;
+
+public class MisusingStubbingTest extends TestBase {
+
+    @Test
+    public void clean_state_after_not_a_mock() {
+        //when
+        assertThatThrownBy(() ->
+            doReturn(100).when("not a mock"));
+
+        //then
+        validateMockitoUsage();
+    }
+
+    @Test
+    public void clean_state_after_null_passed() {
+        //when
+        assertThatThrownBy(() ->
+            doReturn(100).when(null));
+
+        //then
+        validateMockitoUsage();
+    }
+}