Mockito - Nullpointerexception When Stubbing Method

Null pointer exception when stubbing

Mockito advices against Mocking classes you don't own. So an Application mock is a bad idea.
Junit can give you the application context needed : https://developer.android.com/training/testing/junit-runner

For other info about stubbing fail, Mockito fails on stubbing : it tries to execute the function that should be stubbed

Mockito when() method not working and getting null pointer exception

You have classes calling each others methods so it is better to use Mockito.RETURNS_DEEP_STUBS

In your Case:

A is calling B and B is calling C

Just replace:

 @InjectMock
A a;

@Mock
B b;
C c;

With :

A a = Mockito.mock(A.class, Mockito.RETURNS_DEEP_STUBS);
B b = Mockito.mock(B.class, Mockito.RETURNS_DEEP_STUBS);
C c = Mockito.mock(C.class, Mockito.RETURNS_DEEP_STUBS);


Related Topics



Leave a reply



Submit