Skip to content
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

Working on Issue #3156, Adding MockitoMockedStatic and MockitoMockedS… #3161

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

u7498708
Copy link

…taticTest

Checklist

  • Read the contributing guide
  • PR should be motivated, i.e. what does it fix, why, and if relevant how
  • If possible / relevant include an example in the description, that could help all readers
    including project members to get a better picture of the change
  • Avoid other runtime dependencies
  • Meaningful commit history ; intention is important please rebase your commit history so that each
    commit is meaningful and help the people that will explore a change in 2 years
  • The pull request follows coding style
  • Mention Fixes #<issue number> in the description if relevant
  • At least one commit should mention Fixes #<issue number> if relevant

This pull request is for code commenting for TimvdLippe in regards to the Issue #3156 where there is an error with MockedStatic and Mock, so I am creating a catch for it with regards to previous methods. The main intention of this is to get some code feedback.

When running tests.

image
image
image
image
image
image

if (tree.getModifiers().getAnnotations().stream()
.anyMatch(annotation ->
state.getSourceForNode(annotation).equals("@org.mockito.Mock")
&& state.getSourceForNode(tree.getType()).equals("org.mockito.MockedStatic"))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this check for MockedConstruction too? Both have the same issue I think in that mocking them results in a lifecycle-bound mock that has to be closed to prevent interfering with external components

Copy link
Author

Choose a reason for hiding this comment

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

Would you reckon that should be a separate file or in the same one?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not entirely sure. Might be best to wait for confirmation from one of the maintainers! I'd assume it is almost identical logic minus the naming though so whether it is something that is reusable by just passing the class name and error message or not..?

They might not even want to cover the construction case though, so I'd definitely wait for confirmation :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's check for both in this same check. In that case, let's name the checker MockitoIncompatibleMockTypes

@ascopes
Copy link
Contributor

ascopes commented Oct 29, 2023

Regarding the module errors you posted in those screenshots, I guess you will need to add the JVM flags at https://github.com/mockito/mockito/blob/main/subprojects/errorprone/errorprone.gradle#L20 to your test runner configuration in IntelliJ. Failing that, you should be able to just run ./gradlew clean test in the console to trigger the tests.

When I've used errorprone before, I've used these JVM args:

      -Xshare:off
      --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
      --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
      --add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
      --add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
      --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
      --add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
      --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
      --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
      --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
      --add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED

In Maven, IntelliJ seems to be able to automatically pull these from the <argLine/> property but not sure about Gradle.

@u7498708
Copy link
Author

u7498708 commented Oct 29, 2023

I have added those variables to my test configuration under VM.
image

I now get this. Does this mean it ran well and passed? I would assume it didn't as it skipped it.

> Configure project :
Building version '5.6.1-SNAPSHOT'
  - reason: shipkit-auto-version deduced snapshot based on tag: 'v5.6.0'
> Task :errorprone:processResources NO-SOURCE
> Task :errorprone:processTestResources NO-SOURCE
> Task :compileJava UP-TO-DATE
> Task :copyMockMethodDispatcher UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar UP-TO-DATE
> Task :errorprone:compileJava UP-TO-DATE
> Task :errorprone:classes UP-TO-DATE
> Task :errorprone:compileTestJava UP-TO-DATE
> Task :errorprone:testClasses UP-TO-DATE
> Task :errorprone:test
WARNING: package com.sun.tools.javac.type not in jdk.compiler
> Task :errorprone:retryTest SKIPPED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.4/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 4s
6 actionable tasks: 1 executed, 5 up-to-date
10:13:51 pm: Execution finished ':errorprone:test --tests "org.mockito.errorprone.bugpatterns.MockitoMockedStaticTest" -Xshare:off --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED     --add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED'.

@ascopes
Copy link
Contributor

ascopes commented Oct 29, 2023

Think so. I believe you should get a reports directory in one of the build/ directories that tells you what ran. You can test it by breaking your test locally and checking if it then fails

@u7498708
Copy link
Author

Think so. I believe you should get a reports directory in one of the build/ directories that tells you what ran. You can test it by breaking your test locally and checking if it then fails

Well it seems to fail when I remove the semi-colon again and when it is there runs it just concerns me when it says skipped. I am currently using JDK 17.0.2 which is just what I had installed at the time. Let me know if the above warning is something I should be concerned about

"WARNING: package com.sun.tools.javac.type not in jdk.compiler"

@ascopes
Copy link
Contributor

ascopes commented Nov 1, 2023

oh is that the retry test? Think that just reruns failed tests to deflake them

public Description matchVariable(VariableTree tree, VisitorState state) {
if (tree.getModifiers().getAnnotations().stream()
.anyMatch(annotation ->
state.getSourceForNode(annotation).equals("@org.mockito.Mock")
Copy link
Contributor

Choose a reason for hiding this comment

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

if (tree.getModifiers().getAnnotations().stream()
.anyMatch(annotation ->
state.getSourceForNode(annotation).equals("@org.mockito.Mock")
&& state.getSourceForNode(tree.getType()).equals("org.mockito.MockedStatic"))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's check for both in this same check. In that case, let's name the checker MockitoIncompatibleMockTypes

import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class MockitoMockedStaticTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you missed the case where you have a @Mock private MockedStatic<Object> in the original issue description. You can use // BUG: diagnostic contains: to verify that the compilation would fail. Example: https://github.com/google/error-prone/blob/18d5cdf10ec1cc798a588d5c48a9e02a8888c6a1/core/src/test/java/com/google/errorprone/bugpatterns/ArrayAsKeyOfSetOrMapTest.java#L60

@codecov-commenter
Copy link

codecov-commenter commented Nov 7, 2023

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (a8adbf5) 85.53% compared to head (1abffcc) 85.49%.
Report is 5 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3161      +/-   ##
============================================
- Coverage     85.53%   85.49%   -0.04%     
- Complexity     2914     2917       +3     
============================================
  Files           334      335       +1     
  Lines          8861     8868       +7     
  Branches       1096     1098       +2     
============================================
+ Hits           7579     7582       +3     
- Misses          993      995       +2     
- Partials        289      291       +2     
Files Coverage Δ
...to/errorprone/bugpatterns/MockitoMockedStatic.java 42.85% <42.85%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants