package br.com.liveo.mvp.screen.home;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import br.com.liveo.mvp.model.domain.UserResponse;
import io.reactivex.Observable;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * This class makes tests for {@link HomePresenter}
 *
 * @author Rudson Lima
 * @since 09/16/17
 */
@RunWith(JUnit4.class)
public class HomePresenterTest {

    @InjectMocks
    public HomePresenter mPresenter;

    @Mock
    private HomeInteractor mInteractor;

    @Mock
    public HomeContract.View mView;

    @Mock
    private UserResponse mUserResponse;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);

        when(mInteractor.fetchUsers(2)).thenReturn(Observable.just(mUserResponse));
        mPresenter.attach(mView);
    }

    @Test
    public void fetchUsers_sucess(){
        mInteractor.fetchUsers(2);
        verify(mInteractor).fetchUsers(2);
    }

    @Test
    public void fetchUsers_returningFailing_forView() {
        Throwable throwable = new Throwable();
        when(mInteractor.fetchUsers(2)).thenReturn(Observable.error(throwable));

        mInteractor.fetchUsers(2);

        mView.onLoading(false);
        mView.onError(throwable.getMessage());

        verify(mView).onLoading(false);
        verify(mView).onError(throwable.getMessage());
    }

    @Test
    public void fetchUsers_returningSuccess_forView() {
        mView.onLoading(true);
        mInteractor.fetchUsers(2);
        mView.onLoading(false);

        verify(mView).onLoading(true);
        verify(mView).onLoading(false);

        mView.onUserResponse(mUserResponse);
        verify(mView).onUserResponse(mUserResponse);
    }

    @Test
    public void attach_isNotNUll_sucess(){
        Assert.assertNotNull(mPresenter.getView());
    }

    @Test
    public void detachView_isNUll_sucess(){
        Assert.assertNotNull(mPresenter.getView());

        mPresenter.detachView();
        Assert.assertNull(mPresenter.getView());
    }
}

@@ -28,24 +28,48 @@
 reen.home;%0A%0A
+import org.junit.After;%0A
 import org.j
@@ -372,24 +372,106 @@
 bservable;%0A%0A
+import static org.mockito.Mockito.never;%0Aimport static org.mockito.Mockito.times;%0A
 import stati
@@ -1106,32 +1106,111 @@
 (mView);%0A    %7D%0A%0A
+    @After%0A    public void destroy() %7B%0A        mPresenter.detachView();%0A    %7D%0A%0A
     @Test%0A    pu
@@ -1301,16 +1301,26 @@
 teractor
+, times(1)
 ).fetchU
@@ -1385,15 +1385,15 @@
 ning
-Failing
+Success
 _for
@@ -1413,129 +1413,31 @@
     
-Throwable throwable = new Throwable();%0A        when(mInteractor.fetchUsers(2)).thenReturn(Observable.error(throwable)
+mView.onLoading(true
 );%0A
-%0A
     
@@ -1467,17 +1467,16 @@
 ers(2);%0A
-%0A
         
@@ -1499,24 +1499,25 @@
 false);%0A
+%0A
         
 mView.on
@@ -1512,48 +1512,49 @@
     
-mView.onError(throwable.getMessage()
+verify(mView, times(1)).onLoading(true
 );%0A
-%0A
     
@@ -1561,32 +1561,42 @@
     verify(mView
+, times(1)
 ).onLoading(fals
@@ -1603,60 +1603,163 @@
 e);%0A
-        verify(mView).onError(throwable.getMessage()
+%0A        mView.onUserResponse(mUserResponse);%0A        verify(mView, times(1)).onUserResponse(mUserResponse);%0A        verify(mView, never()).onError(%22Error%22
 );%0A 
@@ -1802,39 +1802,39 @@
 hUsers_returning
-Success
+Failing
 _forView() %7B%0A   
@@ -1838,39 +1838,137 @@
         
-mView.onLoading(true
+Throwable throwable = new Throwable();%0A        when(mInteractor.fetchUsers(2)).thenReturn(Observable.error(throwable)
 );%0A
+%0A
         
@@ -1986,32 +1986,33 @@
 .fetchUsers(2);%0A
+%0A
         mView.on
@@ -2023,33 +2023,32 @@
 ing(false);%0A
-%0A
         
 verify(mView
@@ -2039,39 +2039,48 @@
     
-verify(
 mView
-)
 .on
-Loading(true
+Error(throwable.getMessage()
 );%0A
+%0A
     
@@ -2091,24 +2091,34 @@
 verify(mView
+, times(1)
 ).onLoading(
@@ -2121,33 +2121,32 @@
 ing(false);%0A
-%0A
         
 mView.onUser
@@ -2137,42 +2137,62 @@
     
-mView.onUserResponse(mUserResponse
+verify(mView, times(1)).onError(throwable.getMessage()
 );%0A 
@@ -2210,16 +2210,25 @@
 fy(mView
+, never()
 ).onUser

Change unit test HomePresenterTest