Mocking up WifiManager for Android Unit Testing

I’m trying to implement some unit tests for a couple of classes that rely on WifiManager and the returned ScanResults. What I’d like to do is be able to control the ScanResults that I’m receiving in order to test a variety of different conditions.

Unfortunately it’s been quite difficult for me to successfully mock up WifiManager (though I suppose I can pass its constructor null references in my MockWifiManager). This will only be my first problem as once I have a MockWifiManager to play with (if this even works!) I will have to successfully create my test ScanResults which does not have a public constructor (Imagine it’s created by some factory somewhere).

With it not having a public constructor can I even extend it?

Am I going about this all wrong? I often get asked questions about how to do a specific task but really they’re trying to solve a different problem the wrong way, maybe that’s what I’m doing here?

I’m very new to android so having to mock up all of this functionality has been trying to say the least.

Thanks for your inputs!

I’m having a hell of a time instantiating a MockWifiManager as well. The constructor for wifi manager is expecting an IWifiManager a type which does not appear to exist in the Android SDK.


Create an abstraction around WifiManager. Use this for your mocking. Mocking stuff you don’t own is hard and brittle. If done right you should be able to switch the internals, plus you’ll end up with a better mockable API.

For your testing you can stub/fake the manager to you hearts content. For production you’ll pass in a concrete instance.

With regards to your point about changing your code just to make it testable that is incorrect. Firstly you should mock roles not types as discussed in the paper below. Google for more info.

Secondly creating an abstraction around third party code is a best practice as stated by the dependency inversion principle in SOLID. You should always depend on abstractions rather than concrete implementations whether you are unit testing or not.