If I make a simple test case like
@Test
public void myTest() throws Exception {
Spanned word = new SpannedString("Bird");
int length = word.length();
}
an exception is thrown
java.lang.RuntimeException: Method length in android.text.SpannableStringInternal not mocked. See http://g.co/androidstudio/not-mocked for details.
This is explained in the link above as
The android.jar file that is used to run unit tests does not contain any actual code - that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default). This is to make sure your unit tests only test your code and do not depend on any particular behaviour of the Android platform (that you have not explicitly mocked e.g. using Mockito).
So how do you set up Mockito in an Android project in order to mock classes like this?
I want to learn, so I am going to add my answer below Q&A style.