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’m an activity starter, twisted activity starter

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.

Don’t be like this

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 startWithWord method.

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 test_ anymore.

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!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.