From 63acc5c988929fbbb463c064f66f067f67bd235d Mon Sep 17 00:00:00 2001 From: Russell Wolf Date: Thu, 1 Feb 2018 15:01:07 -0500 Subject: [PATCH 1/3] Add test case for BasePresenter --- .../skeleton/base/BasePresenterTest.java | 85 +++++++++++++++++++ .../example1/Example1PresenterTest.java | 4 +- .../example2/Example2PresenterTest.java | 4 +- ...senterTest.java => PresenterTestBase.java} | 2 +- 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java rename app/src/test/java/io/intrepid/skeleton/testutils/{BasePresenterTest.java => PresenterTestBase.java} (95%) diff --git a/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java b/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java new file mode 100644 index 0000000..43d33fd --- /dev/null +++ b/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java @@ -0,0 +1,85 @@ +package io.intrepid.skeleton.base; + +import android.support.annotation.NonNull; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +import io.intrepid.skeleton.testutils.PresenterTestBase; +import io.reactivex.Observable; +import io.reactivex.disposables.Disposable; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class BasePresenterTest extends PresenterTestBase> { + @Mock + private BaseContract.View view; + + @Mock + private Runnable onBind; + + @Mock + private Runnable onUnbind; + + private Disposable disposable; + + @Before + public void setUp() { + presenter = new TestPresenter(view, testConfiguration); + } + + @Test + public void bind_onlyOnce() throws Exception { + presenter.bindView(view); + presenter.bindView(view); + assertEquals(view, presenter.view); + verify(onBind, times(1)).run(); + } + + @Test + public void unbind_onlyOnce() throws Exception { + presenter.unbindView(); + verify(onUnbind, never()).run(); + + presenter.bindView(view); + presenter.unbindView(); + presenter.unbindView(); + verify(onUnbind, times(1)).run(); + } + + @Test + public void unbind_clearsDisposables() { + presenter.bindView(view); + assertFalse(disposable.isDisposed()); + + presenter.unbindView(); + assertTrue(disposable.isDisposed()); + } + + private class TestPresenter extends BasePresenter { + + TestPresenter(@NonNull BaseContract.View view, + @NonNull PresenterConfiguration configuration) { + super(view, configuration); + } + + @Override + protected void onViewBound() { + onBind.run(); + disposable = Observable.never().subscribe(); + disposables.add(disposable); + } + + @Override + protected void onViewUnbound() { + onUnbind.run(); + } + } + +} diff --git a/app/src/test/java/io/intrepid/skeleton/screens/example1/Example1PresenterTest.java b/app/src/test/java/io/intrepid/skeleton/screens/example1/Example1PresenterTest.java index 69ae48e..701dfa5 100644 --- a/app/src/test/java/io/intrepid/skeleton/screens/example1/Example1PresenterTest.java +++ b/app/src/test/java/io/intrepid/skeleton/screens/example1/Example1PresenterTest.java @@ -4,11 +4,11 @@ import org.junit.Test; import org.mockito.Mock; -import io.intrepid.skeleton.testutils.BasePresenterTest; +import io.intrepid.skeleton.testutils.PresenterTestBase; import static org.mockito.Mockito.verify; -public class Example1PresenterTest extends BasePresenterTest { +public class Example1PresenterTest extends PresenterTestBase { @Mock Example1Contract.View mockView; diff --git a/app/src/test/java/io/intrepid/skeleton/screens/example2/Example2PresenterTest.java b/app/src/test/java/io/intrepid/skeleton/screens/example2/Example2PresenterTest.java index 8db4bbd..ff6e876 100644 --- a/app/src/test/java/io/intrepid/skeleton/screens/example2/Example2PresenterTest.java +++ b/app/src/test/java/io/intrepid/skeleton/screens/example2/Example2PresenterTest.java @@ -5,13 +5,13 @@ import org.mockito.Mock; import io.intrepid.skeleton.models.IpModel; -import io.intrepid.skeleton.testutils.BasePresenterTest; +import io.intrepid.skeleton.testutils.PresenterTestBase; import io.reactivex.Single; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -public class Example2PresenterTest extends BasePresenterTest { +public class Example2PresenterTest extends PresenterTestBase { @Mock Example2Contract.View mockView; diff --git a/app/src/test/java/io/intrepid/skeleton/testutils/BasePresenterTest.java b/app/src/test/java/io/intrepid/skeleton/testutils/PresenterTestBase.java similarity index 95% rename from app/src/test/java/io/intrepid/skeleton/testutils/BasePresenterTest.java rename to app/src/test/java/io/intrepid/skeleton/testutils/PresenterTestBase.java index 2e129a2..e689ac4 100644 --- a/app/src/test/java/io/intrepid/skeleton/testutils/BasePresenterTest.java +++ b/app/src/test/java/io/intrepid/skeleton/testutils/PresenterTestBase.java @@ -11,7 +11,7 @@ import io.intrepid.skeleton.settings.UserSettings; import io.reactivex.schedulers.TestScheduler; -public class BasePresenterTest

{ +public class PresenterTestBase

{ @Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); From 12ec64d5cef0b1e55ea6b39c9fa78bb6d8b993d3 Mon Sep 17 00:00:00 2001 From: Russell Wolf Date: Wed, 7 Feb 2018 11:26:52 -0500 Subject: [PATCH 2/3] Split unbind_onlyOnce() into two test methods --- .../io/intrepid/skeleton/base/BasePresenterTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java b/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java index 43d33fd..d934900 100644 --- a/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java +++ b/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java @@ -44,15 +44,18 @@ public void bind_onlyOnce() throws Exception { @Test public void unbind_onlyOnce() throws Exception { - presenter.unbindView(); - verify(onUnbind, never()).run(); - presenter.bindView(view); presenter.unbindView(); presenter.unbindView(); verify(onUnbind, times(1)).run(); } + @Test + public void unbind_notBeforeBind() throws Exception { + presenter.unbindView(); + verify(onUnbind, never()).run(); + } + @Test public void unbind_clearsDisposables() { presenter.bindView(view); From 5c438d398e8e84f613547bcafd39e54fa84d8797 Mon Sep 17 00:00:00 2001 From: Russell Wolf Date: Wed, 7 Feb 2018 11:27:17 -0500 Subject: [PATCH 3/3] Convert BasePresenterTest to use Mockito.spy() --- .../skeleton/base/BasePresenterTest.java | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java b/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java index d934900..7c15866 100644 --- a/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java +++ b/app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java @@ -14,6 +14,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -21,17 +22,11 @@ public class BasePresenterTest extends PresenterTestBase { @Override protected void onViewBound() { - onBind.run(); disposable = Observable.never().subscribe(); disposables.add(disposable); } - - @Override - protected void onViewUnbound() { - onUnbind.run(); - } } }