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

Fixes mockito#2311 #2320

Merged
merged 4 commits into from Jun 26, 2021
Merged

Fixes mockito#2311 #2320

merged 4 commits into from Jun 26, 2021

Conversation

saurabh7248
Copy link
Contributor

@saurabh7248 saurabh7248 commented Jun 9, 2021

Prints fully qualified class name when the simple names of arguments match.

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

Prints fully qualified class name when the simple names of arguments match.
@@ -55,8 +55,18 @@ public String toStringWithType() {
return "(" + wanted.getClass().getSimpleName() + ") " + describe(wanted);
}

@Override
public String toStringWithFullName() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not the biggest fan of adding another method here. Especially as it is very similar to the toStringWithType(), but the only difference is the name retrieval.

What if instead we get the wanted object and perform the computation in the MatchersPrinter? E.g. avoid introductions of new methods and pass in the desired name into toStringWithType(String className)

/**
* Suspiciously not matching arguments are those that don't match, and the classes have same simple name.
*/
public static Integer[] getNotMatchingArgsWithSameNameIndexes(
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than returning Integers, can we instead return all names that have duplicates? Then we can do the lookup on the name. E.g. return a Set<String> here, where the set contains all names that occur at least twice in arguments

Made changes to Equals.toStringWithType by reusing same method to achieve both cases by sending a boolean as an input, based on which either simple name or fully qualified name will be used for describing the type.

Also in the method ArgumentMatchingTool.getNotMatchingArgsWithSameName return Set<String> which return the simple names of classes having more than one classes with different FQCN.
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.

We are almost there!

Lastly, can we add another test to https://github.com/mockito/mockito/blob/6f9108b833323345bd926f10914224d3e0a11b80/src/test/java/org/mockitousage/verification/DescriptiveMessagesWhenVerificationFailsTest.java which verifies that if a method takes two of the same arguments the output is correct? I think if you take should_print_method_when_matcher_used as a starting point, it should be relatively straightforward to add more tests with the following cases:

  1. 2 arguments with the same simple type name
  2. 2 arguments with different simple type names
  3. 3 arguments of which the first and last have the same simple type name, but the middle one has a different one.

In Equals have passed qualified class name instead of the boolean.
Also removed getWantedClass and instead made getWanted public and used it.
Have also added 3 test cases for cases where some arguments have same simple class name.
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.

Nice work and nice tests! Thanks for getting through the review comments and applying them 😄

@@ -386,6 +388,118 @@ public void should_never_break_method_string_when_no_args_in_method() throws Exc
}
}

@Test
public void should_print_fully_qualified_name_when_arguments_classes_have_same_simple_name() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am loving these tests, thanks!

@TimvdLippe
Copy link
Contributor

Oh, you need to run ./gradlew :spotlessApply locally to fix the formatter issues. Do you mind running that?

Fixed the style issues, by running gradlew :spotlessApply

Also needed to handle the case where argument is null, which gave null pointer while getting class name from the object.
@saurabh7248
Copy link
Contributor Author

Sorry, missed to run the gradle check before pushing.

@codecov-commenter
Copy link

codecov-commenter commented Jun 26, 2021

Codecov Report

Merging #2320 (a84e177) into main (92cbac2) will increase coverage by 0.22%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #2320      +/-   ##
============================================
+ Coverage     84.65%   84.88%   +0.22%     
- Complexity     2768     2794      +26     
============================================
  Files           328      328              
  Lines          8428     8480      +52     
  Branches       1011     1025      +14     
============================================
+ Hits           7135     7198      +63     
+ Misses         1015     1004      -11     
  Partials        278      278              
Impacted Files Coverage Δ
...ain/java/org/mockito/internal/matchers/Equals.java 86.66% <100.00%> (ø)
...ockito/internal/matchers/text/MatchersPrinter.java 100.00% <100.00%> (ø)
.../org/mockito/internal/reporting/PrintSettings.java 100.00% <100.00%> (ø)
...a/org/mockito/internal/reporting/SmartPrinter.java 100.00% <100.00%> (ø)
...ication/argumentmatching/ArgumentMatchingTool.java 100.00% <100.00%> (ø)
...erification/checkers/MissingInvocationChecker.java 88.00% <100.00%> (+1.04%) ⬆️
...to/internal/util/concurrent/WeakConcurrentMap.java 37.37% <0.00%> (-2.03%) ⬇️
.../creation/bytebuddy/SubclassBytecodeGenerator.java 80.79% <0.00%> (+0.08%) ⬆️
...on/bytebuddy/InlineDelegateByteBuddyMockMaker.java 68.53% <0.00%> (+2.80%) ⬆️
...ockito/internal/util/reflection/InstanceField.java 43.33% <0.00%> (+6.66%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 92cbac2...a84e177. Read the comment docs.

@TimvdLippe
Copy link
Contributor

100% test coverage on the diff. Nice job! Merging this 🎉

@TimvdLippe TimvdLippe merged commit 1ad8235 into mockito:main Jun 26, 2021
@saurabh7248
Copy link
Contributor Author

Nice!!!
Thanks @TimvdLippe for the reviews.

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

3 participants