I am receiving a NullPointerException
when I try to create an AppCompatImageView
with a mock Context
in a test. Doing the same with a
When you turn to the source code of the class listed in the stack trace (ResourcesWrapper), you find:
public ResourcesWrapper(Resources resources) {
super(resources.getAssets(), resources.getDisplayMetrics(),
And that line 46 is the one with super().
Further looking into the classes in your stack trace, you could come accross:
private TintContextWrapper(@NonNull final Context base) {
super(base);
...
mResources = new VectorEnabledTintResources(this, base.getResources());
So, long story short, yes, you are providing a non null mock object to new AppCompatImageView()
in your code. But then the code you are calling is calling methods on that mocked object. Sure, that is why you created a mock in the first place. But guess what; by default, the mocking framework will return null for any method call.
In other words: you have to understand which calls will happen on that mock; so that you can prepare the mock to return something non null too!
To be precise: I am not saying that exactly that line from TintContextWrapper() causes this NPE; I am mainly saying: when you give a mocked object into other code, you have to prepare that mock to return reasonable results on those method calls that will happen. That could very well mean that you have to create more mocks; so that something like mockedContext.getResources()
does return a non-null result.
In other words: you have to
Beyond that: more likely, the real answer is to use Android specific mocking frameworks. Preparing your mocks to get them "do the right thing" could easily turn into a lot of work.
Maybe the simple answer is to use the deep stubbing from mockito, by simply writing
@Mock (answer = Answers.RETURNS_DEEP_STUBS)
But you need to read/try that; I haven't used that myself.
And given your latest: you need to configure your mock, like
when(context.getResources()).thenReturn(someOtherMock);
for example! That is the whole point of mocks: you can control what happens when methods are called!