Home » Android » unit testing – Mock FingerprintManager in Android tests

unit testing – Mock FingerprintManager in Android tests

Posted by: admin May 14, 2020 Leave a comment

Questions:

Is there a way to simulate or mock FingerprintManager.authenticate()? I want to write instrumented tests for my fingerprint authenticator.

I’m fine if there’s a solution with a restriction that the tests can be run either on an emulator or a device. I use JUnit 4.12.

How to&Answers:

I might suggest that you don’t mock it, in the spirit of not mocking things you don’t own. What I might suggest instead (from the above link):

The prescription implied by “don’t mock what you don’t own” is to introduce your own shim/wrapper/adapter around it. This effectively cordons off the dependency to a single place in your codebase and contextualizes it in the consistent and easy-to-use style you’re trying to promote within your codebase. If there’s anything awkward about how one needs to invoke the dependency (maybe a chaining API, multi-step invocation, repetitive default configuration, etc.), it can be swept under the rug into that common adapter.

This feels like an instance where the humble object pattern might be appropriate.