-
Notifications
You must be signed in to change notification settings - Fork 36
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
Introduce MockitoMockClassReference
check
#454
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d742d6c
Introduce MockitoMockClassReference bug check
Badbond 87956a2
Add check for compatible variable tree types and extend test cases
Badbond 20be36d
Add test for unimplemented edge-cases
Badbond 54973a6
Self-apply and add XXX
Badbond 70a4cb1
Add another XXX
Badbond cabec75
Suggestions
rickie fedd8ef
Implement return type matches and do not flag variable class arguments
Badbond e1b5f72
Suggestion to restructure the check
rickie 2c1d096
Suggestions
Stephan202 fd70908
Suggestions (2)
Stephan202 180ddf6
Add XXX
rickie f0c7563
Suggestion
rickie 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,17 +13,13 @@ void identification() { | |
"A.java", | ||
"import static org.mockito.Mockito.mock;", | ||
"import static org.mockito.Mockito.spy;", | ||
"import static org.mockito.Mockito.withSettings;", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: always check for more edge cases that we can test 😛. |
||
"", | ||
"import java.util.List;", | ||
"import java.util.Objects;", | ||
"import org.mockito.invocation.InvocationOnMock;", | ||
"", | ||
"class A {", | ||
" // BUG: Diagnostic contains:", | ||
" String memberMock = mock(String.class);", | ||
" // BUG: Diagnostic contains:", | ||
" String memberSpy = mock(String.class);", | ||
"", | ||
" {", | ||
" Double d = Objects.requireNonNullElseGet(null, () -> mock(Double.class));", | ||
" Double d2 =", | ||
|
@@ -35,40 +31,43 @@ void identification() { | |
" }", | ||
"", | ||
" void m() {", | ||
" Integer variableMock;", | ||
" Number variableMock = 42;", | ||
" // BUG: Diagnostic contains:", | ||
" variableMock = mock(Number.class);", | ||
" // BUG: Diagnostic contains:", | ||
" variableMock = mock(Number.class, \"name\");", | ||
" // BUG: Diagnostic contains:", | ||
" variableMock = mock(Number.class, InvocationOnMock::callRealMethod);", | ||
" // BUG: Diagnostic contains:", | ||
" variableMock = mock(Number.class, withSettings());", | ||
" variableMock = mock(Integer.class);", | ||
" variableMock = 42;", | ||
" // BUG: Diagnostic contains:", | ||
" List nonGenericallyTypedMock = mock(List.class);", | ||
" List rawMock = mock(List.class);", | ||
" // BUG: Diagnostic contains:", | ||
" List<String> genericallyTypedMock = mock(List.class);", | ||
" var nonExplicitlyTypedMock = mock(Integer.class);", | ||
" Class<? extends Number> variableType = Integer.class;", | ||
" Number variableTypedMock = mock(variableType);", | ||
" Integer namedMock = mock(Integer.class, \"name\");", | ||
" List<String> genericMock = mock(List.class);", | ||
" var varMock = mock(Integer.class);", | ||
" Class<? extends Number> numberType = Integer.class;", | ||
" Number variableTypeMock = mock(numberType);", | ||
" Object subtypeMock = mock(Integer.class);", | ||
"", | ||
" Number variableSpy = 42;", | ||
" // BUG: Diagnostic contains:", | ||
" String equalTypedSpy = spy(String.class);", | ||
" variableSpy = spy(Number.class);", | ||
" variableSpy = spy(Integer.class);", | ||
" variableSpy = 42;", | ||
" // BUG: Diagnostic contains:", | ||
" List nonGenericallyTypedSpy = spy(List.class);", | ||
" List rawSpy = spy(List.class);", | ||
" // BUG: Diagnostic contains:", | ||
" List<String> genericallyTypedSpy = spy(List.class);", | ||
" var nonExplicitlyTypedSpy = spy(Integer.class);", | ||
" Number subtypeSpy = spy(Integer.class);", | ||
" List<String> genericSpy = spy(List.class);", | ||
" var varSpy = spy(Integer.class);", | ||
" Number variableTypeSpy = spy(numberType);", | ||
" Object subtypeSpy = spy(Integer.class);", | ||
" Object objectSpy = spy(new Object());", | ||
"", | ||
" Objects.hash(mock(Integer.class));", | ||
" Integer i = mock(mock(Integer.class));", | ||
" Integer i2 = mock(Integer.class, InvocationOnMock::callRealMethod);", | ||
" String s = new String(mock(String.class));", | ||
" Double d = Objects.requireNonNullElseGet(null, () -> mock(Double.class));", | ||
" Double d2 =", | ||
" Objects.requireNonNullElseGet(", | ||
" null,", | ||
" () -> {", | ||
" return mock(Double.class);", | ||
" });", | ||
" }", | ||
"", | ||
" Double getDoubleMock() {", | ||
|
@@ -102,62 +101,34 @@ void replacement() { | |
"A.java", | ||
"import static org.mockito.Mockito.mock;", | ||
"import static org.mockito.Mockito.spy;", | ||
"import static org.mockito.Mockito.withSettings;", | ||
"", | ||
"import java.util.List;", | ||
"import org.mockito.invocation.InvocationOnMock;", | ||
"", | ||
"class A {", | ||
" String memberMock = mock(String.class);", | ||
"", | ||
" void m() {", | ||
" Runnable runnableMock;", | ||
" runnableMock = mock(Runnable.class);", | ||
" List<String> listOfStringsMock = mock(List.class);", | ||
" List genericListMock = mock(List.class);", | ||
"", | ||
" var unknownTypeMock = mock(Integer.class);", | ||
" Integer namedMock = mock(Integer.class, \"name\");", | ||
"", | ||
" Runnable runnableSpy = spy(Runnable.class);", | ||
" List<String> listOfStringsSpy = spy(List.class);", | ||
" List genericListSpy = spy(List.class);", | ||
"", | ||
" var unknownTypeSpy = spy(Integer.class);", | ||
" Object objectSpy = spy(new Object());", | ||
" }", | ||
"", | ||
" Integer getIntegerMock() {", | ||
" return mock(Integer.class);", | ||
" Number simpleMock = mock(Number.class);", | ||
" Number namedMock = mock(Number.class, \"name\");", | ||
" Number customAnswerMock = mock(Number.class, InvocationOnMock::callRealMethod);", | ||
" Number customSettingsMock = mock(Number.class, withSettings());", | ||
" Number simpleSpy = spy(Number.class);", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"A.java", | ||
"import static org.mockito.Mockito.mock;", | ||
"import static org.mockito.Mockito.spy;", | ||
"import static org.mockito.Mockito.withSettings;", | ||
"", | ||
"import java.util.List;", | ||
"import org.mockito.invocation.InvocationOnMock;", | ||
"", | ||
"class A {", | ||
" String memberMock = mock();", | ||
"", | ||
" void m() {", | ||
" Runnable runnableMock;", | ||
" runnableMock = mock();", | ||
" List<String> listOfStringsMock = mock();", | ||
" List genericListMock = mock();", | ||
"", | ||
" var unknownTypeMock = mock(Integer.class);", | ||
" Integer namedMock = mock(Integer.class, \"name\");", | ||
"", | ||
" Runnable runnableSpy = spy();", | ||
" List<String> listOfStringsSpy = spy();", | ||
" List genericListSpy = spy();", | ||
"", | ||
" var unknownTypeSpy = spy(Integer.class);", | ||
" Object objectSpy = spy(new Object());", | ||
" }", | ||
"", | ||
" Integer getIntegerMock() {", | ||
" return mock();", | ||
" Number simpleMock = mock();", | ||
" Number namedMock = mock(\"name\");", | ||
" Number customAnswerMock = mock(InvocationOnMock::callRealMethod);", | ||
" Number customSettingsMock = mock(withSettings());", | ||
" Number simpleSpy = spy();", | ||
" }", | ||
"}") | ||
.doTest(TestMode.TEXT_MATCH); | ||
|
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.
There can be caveats when the passed argument is a variable that can lead into non-compilable/working-with-Mockito code, e.g. the following is odd and won't work:
There might be cases where we could still replace the variable, but I couldn't find an elegant way for it without going into the caveats. For example the trivial:
Feel free to play around and see if you find an elegant way.
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.
Yeah, I think it's fair to leave this out of scope.