After long time, this is third tutorial in series . I hope you all are doing good 😊 , enjoying tutorial series(Reading,Coding and improvement).

So far in the journey, in Why developers are scared to refactor code , Part 1 & Part 2 of this series, we gained why TDD is supreme, types of testing in android, where to place tests, how to write & run tests etc.

We covered Unit tests till now, next we will move towards integration testing to verify that presenter cope up with UI.

Lets create UI for login as shown in diagram (I know you will make better UI than this 😉).

Checkout below commit as for UI.

git checkout c1988a44127c5e08196f459b50902a1a794d98ef

So we will write some code related to UI. We have contract between our View and Presenter i.e LoginView as below.

Now we implement LoginView to our Activity as below.

Our presenter implementation is like as below

Checkout commit till now

git checkout 6ef35a482c72c6266ccd10319728b44668a621ce

You will get error in LoginPresenterTest as we have constructor for presenter which takes LoginView’s object .

To get rid of this error we have to pass LoginView’s object to presenter while creating presenter object. So we have to mock LoginView.Here comes Mocking 😍.

What is mocking ?

You have an object whose method you want to test, and those method are depend on some other object. You create a mock of dependency object rather than creating actual instance of that dependency . Mock object are used for Unit Testing .

For mocking we are going to use Mockito.

Add below testCompile dependency to app’s build.gradle

testCompile 'org.mockito:mockito-core:1.10.19'

Now we mock our LoginView as below

Ooho😳, 😎 we have our mocked (magical) login object that can passed to presenter. So our implementation becomes like

Run test shows green signal to go ahead.

Integration testing (Between Presenter & View)

To verify that after presenter is calling correct method of view we use verify().

In above code diagram verify method ensures that showLoginSuccessMessage() is called on view object, which ensure that our presenter is correctly integrated with view.

Other test are as follow

Checkout code till now

git checkout 97f0eff1d0eef26494dcec6872355458a6340cf7

Checkout next post

Till then, write your suggestions & inputs on this in comments.

Cheers 🍻 !!!

If you liked this post, please press ♥️ below.