-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
JUnit runner reports argument stubbing mismatches #808
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
Conversation
Current coverage is 86.58% (diff: 94.11%)
@@ release/2.x #808 diff @@
=============================================
Files 284 286 +2
Lines 5628 5657 +29
Methods 0 0
Messages 0 0
Branches 913 916 +3
=============================================
+ Hits 4872 4898 +26
- Misses 563 565 +2
- Partials 193 194 +1
|
Friendly ping |
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.
Did a quick overview, PR seems fine. Let's wait for the others to review before merging
Friendly ping no. 2 ;) If no other feedback I'll merge the PR. |
@@ -2,7 +2,7 @@ | |||
|
|||
import org.mockito.mock.MockCreationSettings; | |||
|
|||
class NoOpTestListener implements MockitoTestListener { | |||
public class NoOpTestListener implements MockitoTestListener { |
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.
Is it necessary to have this class public ?
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.
yes. We still have 2 internal packages: internal.runners and internal.junit. We should merge them but perhaps after we stop maintaining 2.x?
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.
That makes sense.
*/ | ||
public interface RunnerImpl extends Filterable{ | ||
public interface RunnerImpl extends Filterable { |
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.
Since it is internal, I don't see a blocker to rename it now
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.
It will make the diffs in GitHub unusable. Are you sure? :)
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.
Nope git (and so is github) can track renames too
|
||
//TODO rename to DefaultInternalRunner and the parent to InternalRunner |
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.
I prefer the current name SilentJUnitRunner, it describes better the intention (even if it becomes the default runner)
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.
Given that it has more features that just being "silent" (initializing mocks, validating framework) would still prefer the Silent in the name?
protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) { | ||
// get new test listener and add add it to the framework | ||
mockitoTestListener = listenerSupplier.get(); | ||
Mockito.framework().addListener(mockitoTestListener); |
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.
comment paraphrase ;)
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.
:) Removing...
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.
Actually, I kinda like it in the code :). Leaving it for now.
Constructor<?> constructor; | ||
try { | ||
Class<?> runnerClass = Class.forName(runnerClassName); | ||
constructor = runnerClass.getConstructor(Class.class.getClass()); | ||
constructor = runnerClass.getConstructors()[0]; |
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.
It's not safe to rely on order of constructor, I don't believe the JVM garantees it. If there's a single constructor that's ok, but any implementor can add another (e.g. for tests), etc..
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.
Good point! For simplicity I'll just add a check that we expect exactly one constructor.
try { | ||
return new RunnerProvider().newInstance("org.mockito.internal.runners.SilentJUnitRunner", klass); | ||
return new RunnerProvider().newInstance("org.mockito.internal.runners.SilentJUnitRunner", klass, listenerSupplier); |
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.
Why not SilentJUnitRunner.class.getName()
?
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.
To avoid static linking that leads to uncaught class errors. Good question!
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.
Yes I supposed this was that, but since the class is in the same package it feels not needed. And may complicate a tad more renaming.
return new MismatchReportingTestListener(new ConsoleMockitoLogger()); | ||
} | ||
}); | ||
} |
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.
Why not using the same construction with strictness enum and a switch statement ?
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.
Good point. It feels that the current implementation is simpler then when I add conditional logic.
- Makes the behavior of JUnit runner more consistent with JUnit rules. Lack of this consistency was reported by the users. - In Mockito 3.0 we will make the runner have exactly the same behavior as the rule and the implementation will be reused via MockitoTestListener implementations. Refactored JUnit runner implementation will make it easy. - The change should be safe. The most critical change is in SilentJUnitRunner - please review this class most carefully.
After some nice feedback from Brice!
Subjective decision. JUnit runner reporting stubbing mismatches is a nice feature!
f45fafb
to
ea1ec71
Compare
Merging today. Thanks guys for really nice feedback!!! |
Mockito v2 introduced a new feature for reporting unused stubs and reporting argument mismatches (when stubbing uses different arguments than invocation in code under test). We received feedback from our users that the behavior should be more consistent for JUnit rules and JUnit runner. This change introduces that consistency, making JUnit runner also report stubbing argument mismatches to the console. This way, the users should be able to debug tests faster and gain more productivity when writing and maintaining tests!
Also checkout new feature added in Mockito 2.3.x - opt-in strictness for Mockito JUnit rules, making the tests even cleaner and easier to debug!