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

Allow androidx_test to use Mockito 2 #243

Closed
wants to merge 1 commit into from

Conversation

copybara-androidxtest
Copy link
Collaborator

Allow androidx_test to use Mockito 2

@googlebot
Copy link
Collaborator

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@@ -269,7 +269,7 @@ public Void call() throws Exception {
when(mockRemoteInteraction.createRemoteCheckCallable(
any(Matcher.class),
any(Matcher.class),
argThat(
MockitoHamcrest.argThat(

Choose a reason for hiding this comment

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

Why don't we use static import for org.mockito.hamcrest.MockitoHamcrest.argThat to make code shorter?

Choose a reason for hiding this comment

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

This is the remnant of the upcoming migration to migrate away from MockitoHamcrest. If we don't differentiate between argThat invocations that take a Matcher and an ArgumentMatcher, we can't make future migrations. E.g. in this file, if you only only use Matcher, you can statically import. But if you also pass in ArgumentMatcher, you can't and you have to differentiate.

We took the conservative route of migrating to the verbose MockitoHamcrest.argThat to allow future migrations to simply import argThat if they were using an ArgumentMatcher.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just for me to fully understand and learn:
If you import both MockitoHamcrest.argThat and ArgumentMatcher.argThat and overload resolution will decide which one is which.

(explicit, no conflicts)

import org.hamcrest.Matchers;
import org.mockito.ArgumentMatchers;
import org.mockito.hamcrest.MockitoHamcrest;
import org.mockito.internal.matchers.StartsWith;

when(mock.method(
    MockitoHamcrest.argThat(Matchers.startsWith("foo")),
    ArgumentMatchers.argThat(new StartsWith("foo"))
)).then...

vs.

(implicit, works w/ overload resolution)

import static org.hamcrest.Matchers.startsWith;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
import org.mockito.internal.matchers.StartsWith;

when(mock.method(
    argThat(startsWith("foo")),
    argThat(new StartsWith("foo"))
)).then...

Is the problem that only looking at the stubbing setup in just the source code it's hard to know (in an automated way) which one is which, because of the double import?

Choose a reason for hiding this comment

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

If I am not mistaken, that is disallowed by the specification:

If a compilation unit contains both a single-static-import declaration that imports a type whose simple name is n, and a single-type-import declaration (§7.5.1) that imports a type whose simple name is n, a compile-time error occurs, unless the two types are the same type, in which case the duplicate declaration is ignored.

(https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.5.3)

Since we have to import two different static methods from two different classes with two different signatures, that would not compile. E.g. for now it would work, as both methods accept Matcher, but once we make the switch, ArgumentMatchers.argThat will take an ArgumentMatcher, whereas MockitoHamcrest.argThat will take a Matcher.

Choose a reason for hiding this comment

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

(FYI I am speaking in terms of the state of the internal repository. Once we are all on Mockito 2, these statements might no longer be true)

Copy link
Contributor

Choose a reason for hiding this comment

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

Aaaah, Mockito 2, that's where I tried, no error. Thanks for coming back to me.

You're right about the signature change, I forgot about that since we migrated.
https://static.javadoc.io/org.mockito/mockito-core/1.10.19/org/mockito/Matchers.html#argThat(org.hamcrest.Matcher)
https://static.javadoc.io/org.mockito/mockito-core/2.25.1/org/mockito/ArgumentMatchers.html#argThat-org.mockito.ArgumentMatcher-
https://static.javadoc.io/org.mockito/mockito-core/2.25.1/org/mockito/hamcrest/MockitoHamcrest.html#argThat-org.hamcrest.Matcher-
Note MockitoHamcrest is a Mocktio 2.1+ class.


Re JLS, that clause of 7.5.3 talks about "type whose simple name" (type as opposed to member/method/field), so it's referring to this:

import java.lang.Thread.State;
import static java.lang.Thread.State; // valid according to last sentence
import static javafx.concurrent.Worker.State; // compile-time error according to first sentence

Importing same-name methods from different types with same/different signatures is fine. It's the usage that'll blow up when it tries to do the overload resolution (if there are multiple with same signature). Having two methods in different type with same name, different signature behaves similarly to two methods in same type with same name, different signature.

@copybara-androidxtest copybara-androidxtest force-pushed the piper_238033734 branch 6 times, most recently from 4d2f157 to 83947c8 Compare March 18, 2019 19:04
@TimvdLippe
Copy link

I am unable to inspect the build results on this PR, but the presubmit internally was passing.

@TimvdLippe
Copy link

Even though I can't inspect the build results, I think this is failing because of not having linkedin/dexmaker#138 released.

@copybara-androidxtest copybara-androidxtest force-pushed the piper_238033734 branch 2 times, most recently from 6e6f583 to c9d9039 Compare March 19, 2019 12:10
@brettchabot
Copy link
Collaborator

note the github CI is currently broken at head, I'm working on it.

@TimvdLippe
Copy link

All right. Let me know when it is fixed so I can retrigger the CI build 😄

@copybara-androidxtest copybara-androidxtest force-pushed the piper_238033734 branch 7 times, most recently from 0f5885c to f269179 Compare March 22, 2019 20:04
PiperOrigin-RevId: 238033734
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants