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
Improved sensitivity of potential stubbing problem #1539
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/org/mockitousage/strictness/PotentialStubbingSensitivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
|
||
package org.mockitousage.strictness; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.assertj.core.api.ThrowableAssert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.exceptions.misusing.PotentialStubbingProblem; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
import org.mockito.quality.Strictness; | ||
import org.mockitousage.IMethods; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
public class PotentialStubbingSensitivityTest { | ||
|
||
@Rule public MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); | ||
@Mock IMethods mock; | ||
|
||
@Before | ||
public void setup() { | ||
when(mock.simpleMethod("1")).thenReturn("1"); | ||
} | ||
|
||
@Test | ||
public void allows_stubbing_with_different_arg_in_test_code() { | ||
//although we are calling 'simpleMethod' with different argument | ||
//Mockito understands that this is stubbing in the test code and does not trigger PotentialStubbingProblem | ||
when(mock.simpleMethod("2")).thenReturn("2"); | ||
|
||
//methods in anonymous inner classes are ok, too | ||
new Runnable() { | ||
public void run() { | ||
when(mock.simpleMethod("3")).thenReturn("3"); | ||
} | ||
}.run(); | ||
|
||
//avoiding unnecessary stubbing failures: | ||
mock.simpleMethod("1"); | ||
mock.simpleMethod("2"); | ||
mock.simpleMethod("3"); | ||
} | ||
|
||
@Test | ||
public void reports_potential_stubbing_problem_in_production_code() { | ||
//when | ||
Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { | ||
public void call() throws Throwable { | ||
ProductionCode.simpleMethod(mock, "2"); | ||
} | ||
}).isInstanceOf(PotentialStubbingProblem.class); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/test/java/org/mockitousage/strictness/ProductionCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
|
||
package org.mockitousage.strictness; | ||
|
||
import org.mockitousage.IMethods; | ||
|
||
/** | ||
* Test utility class that simulates invocation of mock in production code. | ||
* In certain tests, the production code needs to be invoked in a different class/source file than the test. | ||
*/ | ||
public class ProductionCode { | ||
|
||
public static void simpleMethod(IMethods mock, String argument) { | ||
mock.simpleMethod(argument); | ||
} | ||
|
||
public static void simpleMethod(IMethods mock, int argument) { | ||
mock.simpleMethod(argument); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This would not work if the setup is performed in a base class or helper class. Can we figure out a solution for these situations as well?
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.
This PR is improving the current behavior a lot already. We can always improve it further without blocking this PR. Feel free to suggest a solution!
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.
@TimvdLippe, thoughts?
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 have not been able to come up with a proper solution, other than using a different stubbing mechanism. I fear that, no matter how hard we try, we will keep on receiving issues on this feature, where users will run into all the edge-cases.
That said, this does improve the current situation, so we can push forward. I just think we have to solve the fundamental problem first to be able to prevent these kind of issues popping up.
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.
Thank you for review!