13

So I have a Database class that has a dependency that extends SQLOpenHelper and I am testing using Android instrumented tests and Mockito to mock dependencies. We are noticing that Mockito isn't actually mocking the implementations when doing Mockito.mock(class) instead the actual code is running and throwing null pointer exceptions as we would expect. On API 19 devices the tests run as expected and the mocks work correctly.

Does anyone have any suggestions or things to try? I've tried real devices and emulators with similar results. Could this be a Mockito issue, or a dex-maker issue? I haven't found any information while searching for it.

After some more testing any class I mock with Mockito actually just uses the implementation. Here are my dependencies

androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'

Note: This is also in a library module if that makes any difference.

Stampede10343
  • 844
  • 1
  • 6
  • 21
  • Please add some example code of your tests. Also, I assume you are talking about InstrumentedUnitTests (androidTest), right? – David Rawson Jan 06 '17 at 18:52
  • That's correct. I'll get some sample code. But regardless of what I try to mock Mockito creates the instance but executes the actual code, but only on >=API21 and only for this project, other apps Mockito seems to work fine. – Stampede10343 Jan 06 '17 at 23:03
  • An [MCVE](http://stackoverflow.com/help/mcve) will probably help – David Rawson Jan 07 '17 at 03:15
  • Just to clarify, have you tried the steps in [this question](https://stackoverflow.com/questions/12267572/mockito-dexmaker-on-android)? – David Rawson Jan 07 '17 at 09:04
  • I did try that solution and that had no effect. I'm going to work on getting a MCVE together – Stampede10343 Jan 09 '17 at 21:39
  • @DavidRawson I am currently unable to reproduce this in a simple new project. It seems to be only in our app. I tried setting it up like it was, with a library module and the instrumented tests in there and Mockito works as expected. There's something I'm missing about this project config but nothing I'm trying is working. – Stampede10343 Jan 11 '17 at 17:00
  • It would for the upvoters to clarify if they are experiencing the same problem – David Rawson Jan 11 '17 at 17:48
  • Also perhaps you can add your build.gradle – David Rawson Jan 11 '17 at 19:11

1 Answers1

7

So getting rid of our old Mockito dependencies and dexmaker and using the new: Mockito-android

Fixed our issues

Stampede10343
  • 844
  • 1
  • 6
  • 21