简体   繁体   中英

Robotium - Perform tasks before executing test cases

I have just started learning Robotium and I have written a test case which resets recorded statistics in my app. I was wondering if there was a way I could automate the creation of statistics without having to create them myself manually each time I run the test.

Thanks

EDIT:

I've now added some random values into the database before running the first test. However this results in a NullPointerException.

This is the method I use to create some stats:

private void createStats() {
    Context context = getInstrumentation().getTargetContext();
    mDbAdapter = new PlayDbAdapter(context);
    mDbAdapter.updateViews(8, 5);
    mDbAdapter.updateViews(7, 3);
    mDbAdapter.updatePrompts(7, 2);
    mDbAdapter.updateCompletions(8, 1);
    mDbAdapter.close();
}

This method is called as I run my first test. The updateViews method is below:

public boolean updateViews(long rowId, int views) {
    ContentValues args = new ContentValues();
    args.put(KEY_VIEWS, views);
    return mDb.update(DB_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}

The NullPointer is happening at the return statement in the above method. Any ideas why this is?

I cant see all your code but I am guessing you get this error because you are not opening your database properly.

If it isn't that I will need to see more code to get an idea of whats happening.

I found out why I was getting the NullPointer. I forgot to open my adapter before accessing the database. Such a silly error!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM