0

不久前开始使用 Robolectric,我目前在创建自己的阴影对象时遇到了一些问题。我编写了一些代码来测试我的自定义 SQLiteOpenHelper 类的行为,该类基本上只调用我的数据库表的 onCreate 方法。

@RunWith(SharedDatabaseHelperTestRunner.class)
public class SharedDatabaseHelperTest {

    private SharedDatabaseHelper helper;

    @Before
    public void setUp() 
            throws Exception {
        helper = new SharedDatabaseHelper(Robolectric.application);


    @Test
    public void shouldCallOnCreateOfDatabaseTables() 
            throws Exception {
        helper.onCreate(null);

        assertTrue(ShadowTweetTable.onCreateCalled);
    }

    @Test 
    public void shouldCallOnUpdateOfDatabaseTables() 
            throws Exception {
        helper.onUpgrade(null, 0, 0);

        assertTrue(ShadowTweetTable.onUpgradeCalled);
    }

数据库表的影子对象也只设置了一些布尔变量,确保调用 onCreate:

@Implements(TweetTable.class)

public class ShadowTweetTable {

    public static boolean onCreateCalled = false;
    public static boolean onUpgradeCalled = false;

    @Implementation
    public static void onCreate(SQLiteDatabase database) {
        onCreateCalled = true;
    }

    @Implementation
    public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        onUpgradeCalled = true;
    }

当我运行测试时,RobolectricTestRunner 告诉我 TweetTable 没有影子字段,尽管我的自定义测试运行程序绑定了我的影子类:

公共类 SharedDatabaseHelperTestRunner 扩展 RobolectricTestRunner {

public SharedDatabaseHelperTestRunner(Class<?> testClass)
        throws InitializationError {
    super(testClass);
}

@Override
protected void bindShadowClasses() {
    Robolectric.bindShadowClass(ShadowTweetTable.class);
}

}

4

0 回答 0