Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for BasePresenter #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions app/src/test/java/io/intrepid/skeleton/base/BasePresenterTest.java
Original file line number Diff line number Diff line change
@@ -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<BasePresenter<BaseContract.View>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real comment here, but that is quite a line of source code there. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is hard 😄

@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();
Copy link
Contributor

@gdaniels gdaniels Feb 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this again, I'd actually split this into two tests, presenterWithoutViewDoesNotCallOnViewUnbound() (or whatever) and unbind_onlyOnce().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good call. I was bouncing around a bit in how I split these up since there's a bit of duplication due to lifecycle necessity, but this can definitely be split in two.


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<BaseContract.View> {

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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Example1Presenter> {
public class Example1PresenterTest extends PresenterTestBase<Example1Presenter> {

@Mock
Example1Contract.View mockView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Example2Presenter> {
public class Example2PresenterTest extends PresenterTestBase<Example2Presenter> {

@Mock
Example2Contract.View mockView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.intrepid.skeleton.settings.UserSettings;
import io.reactivex.schedulers.TestScheduler;

public class BasePresenterTest<P extends BasePresenter> {
public class PresenterTestBase<P extends BasePresenter> {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

Expand Down