12

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.

Juuso Ohtonen
  • 6,643
  • 7
  • 54
  • 89
  • Did you see [this](https://stackoverflow.com/q/35335892/6950238) question and answers? – Andrii Omelchenko Aug 03 '18 at 13:00
  • Try to use `PowerMock`, it is able to mock static methods: https://github.com/powermock/powermock/wiki/MockStatic. – Katona Aug 10 '18 at 11:12
  • I often find the use of PowerMock as a test smell or architecture smell. More thoughts from others here: https://stackoverflow.com/a/30163045/242582 – carpeliam Jan 27 '19 at 00:02

1 Answers1

1

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.

carpeliam
  • 6,306
  • 2
  • 35
  • 41