Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:08:18+00:00 2026-05-28T04:08:18+00:00

I have been struggling with a unit testing problem on Android for a while

  • 0

I have been struggling with a unit testing problem on Android for a while now. My app uses a Sqlite DB to store details on vehicles. I have recently added the ContentProvider pattern to retrieve the data (the database used to be accessed directly).

The app works fine, but my tests sporadically fail when run in ‘run’ mode on eclipse – they pass in ‘debug’ mode. In my setUp() method, I create a RenamingDelegatingContext in order to create a test version of my database. All this does is prefix “test.” to the name of my database to ensure the ‘real’ database is not touched by test code. I then pass this to a ‘data provider’ class to keep as a instance variable until the first call to getWriteableDatabase() is called:

public class VehicleProviderTest extends InstrumentationTestCase
{

RenamingDelegatingContext renamingDelegatingContext;
@Override
protected void setUp() throws Exception
{
    super.setUp();
    if(null == renamingDelegatingContext)
    {
        renamingDelegatingContext = new RenamingDelegatingContext(getInstrumentation().getTargetContext(), "test.");

    }

    Log.d("UKMPG", "Initialising UKMPGDataProvider with test context: " + renamingDelegatingContext.getClass().toString());
    MPGDataProvider.init(getTestContext(), Constants.DATABASE_NAME);
    deleteTestDatabase();
}
}

My ContentProvider’s onCreate() method does a similar job in that it also passes a Context to the data provider class:

    @Override
public boolean onCreate()
{
    Context context = getContext();
    Log.d("UKMPG", "ContentProvider.onCreate called. Context: " + context.getClass().toString());
    MPGDataProvider.init(getContext(), Constants.DATABASE_NAME);
    return true;
}

Now, the problem; when I run my tests (again, this doesn’t happen in debug mode), onCreate() in my ContentProvider is called after my setUp() method has passed the RenamingDelegatingContext to the data provider class, resulting in it being overwritten. This means the live database will be used for the tests (they will fail as the tests expect an empty database).

Here is some logcat showing the overwriting taking place with notes explaining what is happening:

ContentProvider.onCreate called and passes Context to UKMPGDataProvider:

01-11 19:22:11.404: D/UKMPG(480): ContentProvider.onCreate called. Context: class android.app.Application
01-11 19:22:11.414: D/UKMPG(480): UKMPGDataProvider.init called. Context: class android.app.Application, db name :mpg_tracker.db

Tests pass a RenamingDelegatingContext to UKMPGDataProvider:

01-11 19:22:13.234: D/UKMPG(498): Initialising UKMPGDataProvider with test context: class android.test.RenamingDelegatingContext
01-11 19:22:13.234: D/UKMPG(498): UKMPGDataProvider.init called. Context: class android.test.RenamingDelegatingContext, db name :mpg_tracker.db

ContentProvider.onCreate called again (different PID) and passes ApplicationContext to UKMPGDataProvider, overwriting the RenamingDelegatingContext:

01-11 19:22:13.254: D/UKMPG(498): ContentProvider.onCreate called. Context: class android.app.ApplicationContext
01-11 19:22:13.254: D/UKMPG(498): UKMPGDataProvider.init called. Context: class android.app.ApplicationContext, db name :mpg_tracker.db

First call to getWriteableDatabase() has happened, so database is going to be created with wrong Context:

01-11 19:22:13.265: D/UKMPG(498): database null - going to create it

Yet another call to ContentProvider.onCreate has happened! This time with a regular Context. The damage is already done however, so this doesn’t really make any difference:

01-11 19:22:13.265: D/UKMPG(498): ContentProvider.onCreate called. Context: class android.app.Application

Creating DB with wrong Context:

01-11 19:22:13.265: D/UKMPG(498): Creating DB instance with Context: class android.app.ApplicationContext

init() method called corresponding to the last ContentProvider.onCreate() call:

01-11 19:22:13.274: D/UKMPG(498): UKMPGDataProvider.init called. Context: class android.app.Application, db name :mpg_tracker.db

getWriteableDatabase() is returning a non-test database:

01-11 19:22:13.374: D/UKMPG(498): getWritableDatabase. Path of returned db: /data/data/barry.contentproviderexample/databases/mpg_tracker.db

Failed to delete database for next test as the ‘test’ database does not exist:

01-11 19:22:13.615: D/UKMPG(498): Database deleted:false

For completeness, here is the logcat from the same test run when run in debug mode:

01-11 19:37:09.514: D/UKMPG(598): ContentProvider.onCreate called. Context: class android.app.Application
01-11 19:37:09.514: D/UKMPG(598): UKMPGDataProvider.init called. Context: class android.app.Application, db name :mpg_tracker.db
01-11 19:37:11.313: D/UKMPG(616): ContentProvider.onCreate called. Context: class android.app.Application
01-11 19:37:11.313: D/UKMPG(616): UKMPGDataProvider.init called. Context: class android.app.Application, db name :mpg_tracker.db

At this point ContentProder.onCreate has been called twice with a non-test Context, but this happens before my setUp() method, so it doesn’t affect the tests:

01-11 19:37:14.173: D/UKMPG(616): Initialising UKMPGDataProvider with test context: class android.test.RenamingDelegatingContext
01-11 19:37:14.173: D/UKMPG(616): UKMPGDataProvider.init called. Context: class android.test.RenamingDelegatingContext, db name :mpg_tracker.db
01-11 19:37:14.213: D/UKMPG(616): database null - going to create it
01-11 19:37:14.213: D/UKMPG(616): Creating DB instance with Context: class android.test.RenamingDelegatingContext
01-11 19:37:14.364: D/UKMPG(616): getWritableDatabase. Path of returned db: /data/data/barry.contentproviderexample/databases/test.mpg_tracker.db
01-11 19:37:14.794: D/UKMPG(616): Database deleted:true

Can anyone help me figure this out? Am I doing something wrong, or is this a (threading?) bug in Android?

It is repeatable on devices and across Android versions 1.6 to 4.0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:08:18+00:00Added an answer on May 28, 2026 at 4:08 am

    I think you should call setContext() in your setUp():

    // warning: untested code
    protected void setUp() throws Exception
    {
        super.setUp();
        setContext(new RenamingDelegatingContext(getTargetContext(), "test.");
    
        ...
    }
    

    In that case you may need to change your base class to ProviderTestCase2 probably.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling with a nasty ANR problem with my app, which loads
I have been struggling with this problem for a few days now and I
I have been struggling with a problem for a while and so far have
I have been struggling through PHP and sqlite for a bit now and I'm
I have been struggling with a small problem for a while. It's been there
I have been struggling with this for a while now so I thought I
I have been struggling with Isotope for a while now. After a few different
Have been struggling with Javascript closure for a while trying to wrap brain around
I have been struggling to think of some decent uses for things like vectors
I have been struggling with this question for awhile now, and I haven't reached

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.