Testing ActiveAndroid with Robolectric
Asked Answered
R

1

6

What can I do to get some test coverage on ActiveAndroid's ContentProvider in Robolectric? This simple test fails.

The model:

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

The 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 ok. By default, Robolectric creates the application that appears in the manifest, which in this case is com.activeandroid.Application.

So, I'm puzzled why the tableInfo in Cache is not initialized. Normal application execution works fine.

Robrobaina answered 1/5, 2013 at 19:16 Comment(4)
What IDE are you using? There is an open bug about a workaround for eclipse, but not intellij. github.com/pardom/ActiveAndroid/issues/63Spirit
It does work with Eclipse. I want to 'mvn test'.Robrobaina
Yea that'll have the same issue because maven builds into a folder other than what ActiveAndroid is expecting. It shouldn't be too hard to change ActiveAndroid to allow you to register your Models instead of finding them automatically.Spirit
Good idea. I"ll give that a shot and see how that goes.Robrobaina
R
4

To automatically scan ActiveAndroid Models automatically during maven unit tests requires a simple change to ModelInfo.scanForModel.

In that method, there is a "Robolectric fallback" which detects and scans paths containing "bin". This handles Model classes in Eclipse projects.

Maven compiles to target/classes. An additional check for "classes" in scan paths in ModelInfo does the trick.

Adding an ActiveAndroid pull request for this soon.

Robrobaina answered 31/5, 2013 at 1:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.