The Solution: Backticks

when() is a reference to the Kotlin construct.

By contrast, `when`() (so, when with backticks around it) is not. The backticks indicate that this is something else that just happens to be named when.

So, to use Mockito’s when() method, you can add the backticks to the import of that static method:

import org.mockito.Mockito.`when`

Then, you can use backticks on the call:

`when`(thisThing.isCalledWithThat()).thenReturn("something else");

If you find that syntax to be ugly, and if there is another likely name to use for the function, you could limit the backticks to just the import statement, plus use as to rename the import. This would allow you to avoid the backticks on every usage of the collision.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.