Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

How to use it to do unit testing

Paranoid Android edited this page Sep 12, 2018 · 3 revisions

It is quite simple.

  • Make your test to extend RealmTesterBase
  • Include the class or classes where your realm is declared at@PrepareForTest annotation
  • Prepare Realm-Tester onto the @before method
  • Provide fields with annotations as shown below. This is optional.
  • Make each test method to have Mocking-Realm clear its data
  • good luck
@PrepareForTest({TestRealmApp.class})
public class TestingWithRealm extends RealmTesterBase {

    @Before
    public void before() throws Exception {

        RealmTester.prepare();

        //please let Mocking-Realm know about your model's annotations
        //reflection doesn't allow to figure each realmModel's primary, ignored or indexed fields.
        RealmTester.addAnnotations( RealmAnnotation.build(Address.class)
                .primaryField(AddressFields.ADDRESSID)
                .indexedFields(AddressFields.NAME, AddressFields.DATEUPDATED, AddressFields.TIMESVISITED));
    }

    @Test
    public void testAddressProvider(){
        //clear the data stored before each test
        RealmTester.clearData();
    } 
}
Clone this wiki locally