Escaping Keywords

In other words, why does when() become `when`()?

The Scenario: Mockito

Mockito is a popular mocking framework for Java and Kotlin. You can use it in unit tests (and Android instrumentation tests) to create mock implementations of objects, where you can have your tests control how those objects respond to various function calls.

A core function for that is Mockito.when(), used to teach a Mockito mock how to respond to a particular Java method call, such as in this Java snippet:

Mockito.when(thisThing.isCalledWithThis()).thenReturn("something");

…or, if you use a static import of when():

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

Prev Table of Contents Next

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