I am trying to write a Unit Test (Not instrumented) for Android using JUnit and Mockito.
Some context: I am testing a class that depends heavily on a view and I cannot / don't want to actually inflate the view during the test. At one point, my class-to-test wants to use the width of the view and I have defined a (public) method that the class is using to get the width at runtime (getWidth()
).
In my test, I want to use Mockito to mock the getWidth()
method - only. (While having the rest of the class behave the same way). Just to clarify: One method in my Class calls getWidth()
and I want it to return a mocked value during testing.
So I tried instantiating the Class using the Mockito.spy()
method, but I don't know if this is the correct approach (it doesn't work) or if there is something else I should do.
My current code:
mGraph = Mockito.spy(new Graph(xAxis, leftAxis, null, false, new Graph.Style(), curve));
Mockito.when(graph.getGraphWidth()).thenReturn(400);
I get the following error message, though I don't know if it's relevant or just another error:
java.lang.IllegalArgumentException: dexcache == null (and no default could be found; consider setting the 'dexmaker.dexcache' system property)