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 all commits
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
77 changes: 77 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,77 @@
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.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class BasePresenterTest extends PresenterTestBase<BasePresenter<BaseContract.View>> {
@Mock
private BaseContract.View view;

private Disposable disposable;

@Before
public void setUp() {
presenter = spy(new TestPresenter(view, testConfiguration));
}

@Test
public void bind_onlyOnce() throws Exception {
presenter.bindView(view);
presenter.bindView(view);
assertEquals(view, presenter.view);
verify(presenter, times(1)).onViewBound();
}

@Test
public void unbind_onlyOnce() throws Exception {
presenter.bindView(view);
presenter.unbindView();
presenter.unbindView();
verify(presenter, times(1)).onViewUnbound();
}

@Test
public void unbind_notBeforeBind() throws Exception {
presenter.unbindView();
verify(presenter, never()).onViewUnbound();
}

@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() {
disposable = Observable.never().subscribe();
disposables.add(disposable);
}
}

}
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