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

Add back InvocationOnMock.getArgument<T>(int, Class<T>) #1609

Closed
TimvdLippe opened this issue Feb 5, 2019 · 7 comments
Closed

Add back InvocationOnMock.getArgument<T>(int, Class<T>) #1609

TimvdLippe opened this issue Feb 5, 2019 · 7 comments

Comments

@TimvdLippe
Copy link
Contributor

In #365 we replaced the existing getArgumentAt<T>(int, Class<T>) API with the current getArgument(int) API. However, when integrating this change within Google, we ran into numerous issues.

An example:

invocation.getArgumentAt(0, Runnable.class).run()

gets rewritten to

invocation.getArgument(0).run()

This does not compile, as the generic parameter is erasured to Object and thus does not have the run method.

Therefore, I would propose adding back an overload to getArgument that allows you to specify the Class. In general, users would not need this inside a variable assignment, but do need it in all other cases.

@TimvdLippe
Copy link
Contributor Author

For more information see the error-prone bugpattern: https://errorprone.info/bugpattern/TypeParameterUnusedInFormals

@TimvdLippe
Copy link
Contributor Author

@mockitoguy WDYT? I think it makes sense to have a simple API for local assignments or in return statements, but we would still need the Class to be explicit.

@mockitoguy
Copy link
Member

Looking...

@mockitoguy
Copy link
Member

Good idea! Few suggestions:

  • document clearly the use case in the new method (compact, readable tests / one-liner invocations).
  • add NotExtensible annotation to InvocationOnMock

Nice catch.

@TimvdLippe
Copy link
Contributor Author

Awesome, thanks for the quick response! Will open a PR today 😄

TimvdLippe added a commit that referenced this issue Mar 4, 2019
Also add `@NotExtensible` to several of our interfaces to document they
are not intended to be subclassed.

Fixes #1609
@Stephan202
Copy link
Contributor

Instead of using the reintroduced getArgument<T>(int, Class<T>), IIUC the issue described above can also be solved as follows:

invocation.<Runnable>getArgument(0).run()

That's admittedly less obvious, but does keep the API smaller. Has this been considered?

@TimvdLippe
Copy link
Contributor Author

While that does work as well, I think consistency with the old API as well as the readability of the former vs the latter would still lead me to conclude that reintroducing is the appropriate solution.

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

No branches or pull requests

3 participants