Testing static Activity starter methods
You probably have a few of these kinds of methods in your code. I know I do, and I wanted to learn how to test it.
I became a manager a while ago, so I’ve been using my latest app as an excuse to stay up to date with the new cool things like Kotlin. I don’t want to be the type of manager who gives my teams special code presents for them to maintain forever.
Anyway, back to the testing. It would be awesome to be able to write a unit test to cover this. You can’t do that, of course, because
Intent is an Android class, and it won’t be mocked. I also don’t want to change the the
Thankfully there are instrumentation tests that run against an emulator or real device that give you access to all of the Android classes. The first thing I noticed was that you can’t use those cool method names in instrument tests. That’s a pity, but at least you don’t have to start the method names with
I used a combination of Mockito mocks, Mockito argument captors, and Google Truth. I mocked the
Context because my main goal was to capture the
Intent that is created when the
startWithWord method calls the
context.startActivity(Intent) method. You’ll find my manager level code here:
That’s it. Hope you enjoyed reading!