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
- Host: GitHub
- URL: https://github.com/niqdev/dagger-realm-test
- Owner: niqdev
- Created: 2015-12-18T13:38:48.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-07-20T06:53:13.000Z (almost 10 years ago)
- Last Synced: 2025-03-24T19:08:16.129Z (about 1 year ago)
- Language: Java
- Homepage:
- Size: 149 KB
- Stars: 38
- Watchers: 5
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#Dagger 2 and Realm unit test
[](https://travis-ci.org/niqdev/dagger-realm-test)
[](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$