An open API service indexing awesome lists of open source software.

https://github.com/niqdev/dagger-realm-test

Android tests: Dagger 2 and Realm
https://github.com/niqdev/dagger-realm-test

Last synced: about 1 year ago
JSON representation

Android tests: Dagger 2 and Realm

Awesome Lists containing this project

README

          

#Dagger 2 and Realm unit test

[![Build Status](https://travis-ci.org/niqdev/dagger-realm-test.svg?branch=master)](https://travis-ci.org/niqdev/dagger-realm-test)
[![Coverage Status](https://coveralls.io/repos/github/niqdev/dagger-realm-test/badge.svg?branch=master)](https://coveralls.io/github/niqdev/dagger-realm-test?branch=master)

:tada: Updated with Realm v1.x!! <-- Robolectric/Realm [issue](https://github.com/robolectric/robolectric/issues/1389)

Sample Android application of [Dagger 2](http://google.github.io/dagger/) and [Realm](https://realm.io/docs/java/latest/) tested with [Robolectric](http://robolectric.org/), [Mockito](http://mockito.org/) and [PowerMockito](https://github.com/jayway/powermock).

### Example
```java
@RunWith(RobolectricGradleTestRunner.class)
@Config(application = CustomApplicationTest.class, constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*"})
@PrepareForTest({Injector.class})
public class MessageRepositoryTest {

@Rule
public PowerMockRule rule = new PowerMockRule();

@Inject
MessageRepository messageRepository;

@Inject
DatabaseRealm databaseRealm;

@Before
public void setupDagger() {
ApplicationComponentTest applicationComponentTest = DaggerApplicationComponentTest.builder()
.applicationContextModuleTest(new ApplicationContextModuleTest())
.repositoryModuleTest(new RepositoryModuleTest(false))
.build();

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

((ApplicationComponentTest) Injector.getApplicationComponent()).inject(this);
}

@Test
public void messageRepository_add() {
String MESSAGE_UUID = "UUID";
String MESSAGE_CONTENT = "CONTENT";
String MESSAGE_INFO = "INFO";
MessageModel message = new MessageModel();
message.setUuid(MESSAGE_UUID);
message.setContent(MESSAGE_CONTENT);
message.setInfo(MESSAGE_INFO);

when(databaseRealm.add(message)).thenReturn(message);

TestSubscriber tester = new TestSubscriber<>();
messageRepository.add(message).subscribe(tester);

verify(databaseRealm).add(message);

tester.assertValue(MESSAGE_UUID);
tester.assertCompleted();
tester.assertNoErrors();
}

@Test
public void messageRepository_findAll() {
MessageModel message1 = MessageModel.newBuilder().content("CONTENT1").build();
MessageModel message2 = MessageModel.newBuilder().content("CONTENT2").build();
List messages = Arrays.asList(message1, message2);

when(databaseRealm.findAll(MessageModel.class)).thenReturn(messages);

TestSubscriber> tester = new TestSubscriber<>();
messageRepository.findAll().subscribe(tester);

tester.assertValue(messages);
tester.assertCompleted();
tester.assertNoErrors();
}

}
```

Setup [Robolectric](http://robolectric.org/getting-started/)

- select test directory
- Build > Select Build Variant
- Test Artifact: Unit Tests

for Linux and Mac users:

- Edit Configurations > Working directory = $MODULE_DIR$