jelies
jelies

Reputation: 9290

Robolectric: Testing with ormlite

I'm trying to test ORMLite DAOs with robolectric, but database behaviour is not the same as when it's used from my android app. My DAOs are working perfectly well on the android application.

Reading about robolectric shadows and debugging code, I encountered ShadowSQLiteOpenHelper (code here).

Does anyone know if this Shadow is enough to test ormlite daos? Or I have to create my own shadow to achieve that? Any clue/tip/suggestion/example here?

Thanks in advance.


Extra info:

Test method:

@Test
public void basicTest() throws SQLException {
    assertNotNull(randomStringResource); // Injection of an android resource: OK
    assertThat(randomStringResource, equalTo("Event")); // With correct value: OK
    assertNotNull(eventDao); // Dao injection: OK
    assertThat(eventDao.countOf(), equalTo(0L)); // Table empty: OK

    Event e1 = new Event("e1", new Date());
    eventDao.create(e1);

    assertNotNull(e1.getId()); // ID generated by OrmLite: OK
    assertThat(eventDao.countOf(), equalTo(1L)); // Table not empty: OK
    assertThat("e1", equalTo(eventDao.queryForId(e1.getId()).getName())); // Query for inserted event: Throws exception
}

Some of the problems encountered running this test:

Versions used:

Upvotes: 3

Views: 1416

Answers (1)

flav
flav

Reputation: 593

Sorry for resurrecting your topic but I ran into the same problem.

I'm using OrmLite 4.45 and Robolectric 2.1.

In ShadowSQLiteCursor.java, cacheColumnNames method calls toLowerCase on each column name. So I decided to extend ShadowSQLiteCursor with my own (which doesn't call toLowerCase):

/**
* Simulates an Android Cursor object, by wrapping a JDBC ResultSet.
*/
@Implements(value = SQLiteCursor.class, inheritImplementationMethods = true)
public class ShadowCaseSensitiveSQLiteCursor extends ShadowSQLiteCursor {

private ResultSet resultSet;

public void __constructor__(SQLiteCursorDriver driver, String editTable, SQLiteQuery query) {}

/**
 * Stores the column names so they are retrievable after the resultSet has closed
 */
private void cacheColumnNames(ResultSet rs) {
    try {
        ResultSetMetaData metaData = rs.getMetaData();
        int columnCount = metaData.getColumnCount();
        columnNameArray = new String[columnCount];
        for(int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            String cName = metaData.getColumnName(columnIndex);
            this.columnNames.put(cName, columnIndex - 1);
            this.columnNameArray[columnIndex - 1] = cName;
        }
    } catch(SQLException e) {
        throw new RuntimeException("SQL exception in cacheColumnNames", e);
    }
}
}

My answer obviously comes too late but may help others!

Upvotes: 6

Related Questions