Skip to content

Commit

Permalink
add realm test findAll
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Jul 19, 2016
1 parent c9473e6 commit 699f63b
Showing 1 changed file with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,40 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.RealmResults;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(RobolectricGradleTestRunner.class)
@Config(application = CustomApplicationTest.class, constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.robolectric.*", "android.*"})
@PrepareForTest({Realm.class, Injector.class})
@PrepareForTest({Realm.class, Injector.class, RealmQuery.class, RealmResults.class})
public class DatabaseRealmTest {

@Rule
Expand Down Expand Up @@ -80,23 +88,22 @@ public void setupRealm() {
.databaseModuleTest(new DatabaseModuleTest())
.build();

PowerMockito.mockStatic(Injector.class);
PowerMockito.when(Injector.getApplicationComponent()).thenReturn(applicationComponentTest);
mockStatic(Injector.class);
when(Injector.getApplicationComponent()).thenReturn(applicationComponentTest);

((DatabaseComponentTest) Injector.getApplicationComponent()).inject(this);

realmMock = PowerMockito.mock(Realm.class);
PowerMockito.mockStatic(Realm.class);

realmMock = mock(Realm.class);
mockStatic(Realm.class);
when(Realm.getDefaultInstance()).thenReturn(realmMock);
doNothing().when(realmMock).beginTransaction();
doNothing().when(realmMock).commitTransaction();
}

@Test
public void test_add() {
MessageModel message = new MessageModel();

doNothing().when(realmMock).beginTransaction();
doNothing().when(realmMock).commitTransaction();
databaseRealm.add(message);

assertThat(Realm.getDefaultInstance(), is(realmMock));
Expand All @@ -107,18 +114,32 @@ public void test_add() {

@Test
public void test_findAll() {
// MessageModel message1 = new MessageModel();
// MessageModel message2 = new MessageModel();
// List<MessageModel> messages = new ArrayList<>();
// messages.add(message1);
// messages.add(message2);
//
// assertThat(Realm.getDefaultInstance(), is(realmMock));
// RealmQuery<MessageModel> query = RealmQuery.createQuery(realmMock, MessageModel.class);
// when(realmMock.where(MessageModel.class)).thenReturn(query);
// when(query.findAll()).thenReturn((RealmResults)messages);
//
// List<MessageModel> result = databaseRealm.findAll(MessageModel.class);
MessageModel message1 = new MessageModel();
MessageModel message2 = new MessageModel();
List<MessageModel> messageList = Arrays.asList(message1, message2);
RealmResults<MessageModel> messages = mockRealmResults();

RealmQuery<MessageModel> query = mockRealmQuery();
when(realmMock.where(MessageModel.class)).thenReturn(query);
when(query.findAll()).thenReturn(messages);
when(messages.iterator()).thenReturn(messageList.iterator());
when(messages.size()).thenReturn(messageList.size());

List<MessageModel> result = databaseRealm.findAll(MessageModel.class);

assertThat(Realm.getDefaultInstance(), is(realmMock));
verify(realmMock.where(MessageModel.class), times(1)).findAll();
assertEquals("invalid size", 2, result.size());
}

@SuppressWarnings("unchecked")
private <T extends RealmObject> RealmQuery<T> mockRealmQuery() {
return mock(RealmQuery.class);
}

@SuppressWarnings("unchecked")
private <T extends RealmObject> RealmResults<T> mockRealmResults() {
return mock(RealmResults.class);
}

}
Expand Down

0 comments on commit 699f63b

Please sign in to comment.