I learn how to test the presenter layer of MVP architecture in android, my presenter using retrofit 2 and in my activity I used dagger 2 as dependency injection to my presenter, this is my Dagger and presenter injection looks like:
@Inject AddScreenPresenter addScreenPresenter; This is the Dagger builder :
DaggerAddScreenComponent.builder() .netComponent(((App) getApplicationContext()).getNetComponent()) .addScreenModule(new AddScreenModule(this, new ContactDatabaseHelper(this))) .build().inject(this); and this is my presenter constructor :
@Inject public AddScreenPresenter(Retrofit retrofit, AddScreenContact.View view, ContactDatabaseHelper contactDatabaseHelper) { this.retrofit = retrofit; this.view = view; this.contactDatabaseHelper = contactDatabaseHelper; } I have write the unit test class and mock the Retrofit class, but when I run it, the error appears :
Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types
This is the test class :
@RunWith(MockitoJUnitRunner.class) public class AddScreenPresenterTest { private AddScreenPresenter mAddPresenter; @Mock private Retrofit mRetrofit; @Mock private Context mContext; @Mock private AddScreenContact.View mView; @Mock private ContactDatabaseHelper mContactDatabaseHelper; String firstName, phoneNumber; Upload upload; @Before public void setup() { mAddPresenter = new AddScreenPresenter(mRetrofit, mView, mContactDatabaseHelper); firstName = "aFirstName"; phoneNumber = "998012341234"; Uri path = Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.missing); upload = new Upload(); upload.title = firstName; upload.description = "aDescription"; upload.albumId = "XXXXX"; upload.image = new File(path.getPath()); } @Test public void checkValidationTest() { verify(mAddPresenter).checkValidation(firstName, phoneNumber); } @Test public void uploadMultiPartTest() { verify(mAddPresenter).uploadMultiPart(upload); } } this is my module :
@Module public class AddScreenModule { private final AddScreenContact.View mView; private final ContactDatabaseHelper mContactDatabaseHelper; public AddScreenModule (AddScreenContact.View view, ContactDatabaseHelper contactDatabaseHelper) { this.mView = view; this.mContactDatabaseHelper = contactDatabaseHelper; } @Provides @CustomScope AddScreenContact.View providesAddScreenContactView() { return mView; } @Provides @CustomScope ContactDatabaseHelper providesContactDatabaseHelper() { return mContactDatabaseHelper; } } I know that Retrofit class is a final class, and now I stuck and don't know how to create the presenter object in my test class. Please help me, how to create the object of the presenter class with retrofit in the constructor. Feel free to ask if my question is not clear enough, and thank you very much for your help.