Repository: mockito/mockito
Issue: MockitoJUnitRunner too strict by default?
Labels: question
wontfix
strictness

In one of the projects being migrated to Mockito 2 I've observed that deprecated `org.mockito.runners.MockitoJUnitRunner` (tested with 2.11.0) works in a strict mode by default (fails a test on unnecessary stubbing). I wonder if it is an expected behavior?

Even Mockito rule works in a "warn" mode by default. It makes it a little bit harder to migrate from Mockito 1.10.x to 2.x.

Issue comments:
What's more even using the old Silent `@RunWith(org.mockito.runners.MockitoJUnitRunner.Silent.class)` still causes `UnnecessaryStubbingException`. The new one (`@RunWith(org.mockito.junit.MockitoJUnitRunner.Silent.class)`) has to be used to keep test passing.
https://github.com/mockito/mockito/blob/e10c5409e460c3ecd6b2ef9513a0b3802351d85f/src/main/java/org/mockito/runners/MockitoJUnitRunner.java#L29-L31

It seems we have to change the call to this instead: https://github.com/mockito/mockito/blob/e10c5409e460c3ecd6b2ef9513a0b3802351d85f/src/main/java/org/mockito/junit/MockitoJUnitRunner.java#L107

I submitted #1226 to discuss this change in behavior. I think it is an oversight when we deprecated the runner in the old package.
It's a bug. Thank you for reporting!

The previous runner: ```org.mockito.runners.MockitoJUnitRunner``` should behave exactly the same as the new runner: ```org.mockito.junit.MockitoJUnitRunner```
I started reviewing @TimvdLippe PR and I came to conclusion that it the current behavior works as designed.

 - the change of strictness of the runner was an intentional incompatible change in 2.0 release
 - the deprecation of the runner was a compatible change done after 2.0. Deprecated runner does not intend to help migration.
 - the current behavior of the code (unless I'm mistaken) is that deprecated runner behaves as the new runner which is what we want.

I hope above clarifies the situation :) Please reopen if needed!

Pull request review comments:
Curious why changing the public modifier?

I suspect that this change may not be binary compatible in case somebody extends the runner.
The code works as designed (https://github.com/mockito/mockito/issues/1212#issuecomment-340266646).

If you want to keep the PR I suggest that we add a unit test that uses deprecated runner so that we preserve and document this behavior.

Patch that resolved the issue:
diff --git a/src/main/java/org/mockito/junit/MockitoJUnitRunner.java b/src/main/java/org/mockito/junit/MockitoJUnitRunner.java
index 2a1558719b..fbdbfcf5eb 100644
--- a/src/main/java/org/mockito/junit/MockitoJUnitRunner.java
+++ b/src/main/java/org/mockito/junit/MockitoJUnitRunner.java
@@ -152,7 +152,7 @@ public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException {
         this(new StrictRunner(new RunnerFactory().createStrict(klass), klass));
     }
 
-    MockitoJUnitRunner(InternalRunner runner) throws InvocationTargetException {
+    protected MockitoJUnitRunner(InternalRunner runner) throws InvocationTargetException {
         this.runner = runner;
     }
 
diff --git a/src/main/java/org/mockito/runners/MockitoJUnitRunner.java b/src/main/java/org/mockito/runners/MockitoJUnitRunner.java
index 74f025c6f1..5ad17a4a2a 100644
--- a/src/main/java/org/mockito/runners/MockitoJUnitRunner.java
+++ b/src/main/java/org/mockito/runners/MockitoJUnitRunner.java
@@ -9,6 +9,8 @@
 import org.junit.runner.manipulation.Filter;
 import org.junit.runner.manipulation.NoTestsRemainException;
 import org.junit.runner.notification.RunNotifier;
+import org.mockito.internal.runners.InternalRunner;
+import org.mockito.internal.runners.RunnerFactory;
 
 
 /**
@@ -27,7 +29,7 @@ public class MockitoJUnitRunner extends org.mockito.junit.MockitoJUnitRunner {
     @Deprecated
     public static class Silent extends MockitoJUnitRunner {
         public Silent(Class<?> klass) throws InvocationTargetException {
-            super(klass);
+            super(new RunnerFactory().create(klass));
         }
     }
 
@@ -47,6 +49,10 @@ public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException {
         super(klass);
     }
 
+    MockitoJUnitRunner(InternalRunner runner) throws InvocationTargetException {
+        super(runner);
+    }
+
     @Deprecated
     @Override
     public void run(final RunNotifier notifier) {


Test patch: