Testing ActiveAndroid with Robolectric

What can I do to get coverage for testing ActiveAndroid ContentProvider in Robolectric? This simple test fails.

Model:

@Table(name = "Things") public class Thing extends Model { public Thing() { super(); } } 

Test:

 @RunWith(RobolectricTestRunner.class) public class ContentProviderTest { @Test public void itShouldQuery() throws Exception { new Thing().save(); ContentResolver cr = new MainActivity().getContentResolver(); assertNotNull( cr.query(Uri.parse("content://org.example/things"), null, null, null, null)); } } 

The resulting stack trace:

 java.lang.NullPointerException: null at com.activeandroid.Cache.getTableInfo(Unknown Source) at com.activeandroid.Model.<init>(Unknown Source) at org.example.Thing.<init>(Thing.java:9) at org.example.ProviderTest.itShouldQuery(ProviderTest.java:25) 

The application context should be in order. By default, Robolectric creates an application that appears in the manifest, which in this case is com.activeandroid.Application.

So, I am puzzled by why tableInfo in the cache is not initialized. Normal application execution works fine.

+6
source share
1 answer

To automatically scan ActiveAndroid models automatically during maven unit tests, a simple modification to ModelInfo.scanForModel is required .

This method has a "Robolectric fallback" that detects and scans paths containing "bin". This handles model classes in Eclipse projects.

Maven compiles for goals / classes. An extra check for the “classes” in the scan paths in ModelInfo does the trick.

Adding an ActiveAndroid activation request for this soon.

+4
source

Source: https://habr.com/ru/post/944069/


All Articles