Skip to content

Conversation

mockitoguy
Copy link
Member

@mockitoguy mockitoguy commented Dec 11, 2016

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!

@codecov-io
Copy link

codecov-io commented Dec 11, 2016

Current coverage is 86.58% (diff: 94.11%)

Merging #808 into release/2.x will increase coverage by 0.01%

@@           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   

Powered by Codecov. Last update a75b5a1...ea1ec71

@mockitoguy
Copy link
Member Author

Friendly ping

Copy link
Contributor

@TimvdLippe TimvdLippe left a 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

@mockitoguy
Copy link
Member Author

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 {
Copy link
Contributor

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 ?

Copy link
Member Author

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?

Copy link
Contributor

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 {
Copy link
Contributor

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

Copy link
Member Author

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? :)

Copy link
Contributor

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
Copy link
Contributor

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)

Copy link
Member Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment paraphrase ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) Removing...

Copy link
Member Author

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];
Copy link
Contributor

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..

Copy link
Member Author

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);
Copy link
Contributor

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() ?

Copy link
Member Author

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!

Copy link
Contributor

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());
}
});
}
Copy link
Contributor

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 ?

Copy link
Member Author

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!
@mockitoguy
Copy link
Member Author

Merging today. Thanks guys for really nice feedback!!!

@mockitoguy mockitoguy merged commit 064a3f2 into release/2.x Dec 21, 2016
@mockitoguy mockitoguy deleted the strict-runner branch December 21, 2016 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants