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

  • Home
  • SEARCH
  • 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 6670841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:19:00+00:00 2026-05-26T03:19:00+00:00

I’ve implemented access to a database using SQLiteOpenHelper from the android.database package within some

  • 0

I’ve implemented access to a database using SQLiteOpenHelper from the android.database package within some classes (with pattern DAO).

I wrote some junit tests for these classes using an AndroidTestCase but this causes the tests to use the same database as the application.

I read that the ProviderTestCase2 or RenamingDelegatingContext can be used to test the database separately. Unluckily I couldn’t find any nice tutorial/example that shows how to test a database with ProviderTestCase2/RenamingDelegatingContext.

Can anyone point me somewhere OR give me some tip OR share some code for database testing?!

Cheeerrrrsss!!
Giorgio

  • 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-26T03:19:00+00:00Added an answer on May 26, 2026 at 3:19 am

    Both the ProviderTestCase and RenamingDelegatingContext will destroy the database if one already exists before opening it within it’s context, so in that sense they both have the same low-level approach towards opening a SQLite database.

    You leverage this by opening the database in your fixture in setUp(), which will then ensure that your working with a fresh database before each test case.

    I would suggest that you go for writing content providers rather than creating database adapters. You can use a common interface for accessing data, be it stored in the DB or somewhere over the network, the design of content providers can be accommodated to access such data at the cost of a bit of IPC overhead involved that most of us shouldn’t have to care about.

    If you did this for accessing a SQLite database, the framework would completely manage the database connection for you in a separate process. As added beef, the ProviderTestCase2<ContentProvider> completely bootstraps a test context for your content provider without you having to a write a single line of code.

    But, that’s not said it isn’t such a huge effort to do the bootstrapping yourself. So supposing you had a database adapter as such; we’ll just focus on open() for getting write access to our database, nothing fancy:

    public class MyAdapter {
    
        private static final String DATABASE_NAME = "my.db";
        private static final String DATABASE_TABLE = "table";
        private static final int DATABASE_VERSION = 1;
    
    
        /**
         * Database queries
         */
        private static final String DATABASE_CREATE_STATEMENT = "some awesome create statement";
    
        private final Context mCtx;
        private SQLiteDatabase mDb;
        private DatabaseHelper mDbHelper;
    
        private static class DatabaseHelper extends SQLiteOpenHelper {
    
            public DatabaseHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
    
            @Override
            public void onCreate(SQLiteDatabase db) {
                db.execSQL(DATABASE_CREATE_STATEMENT);  
            }
    
            @Override
            public void onUpgrade(SQLiteDatabase db, int a, int b) {
                // here to enable this code to compile
            }
        }
    
        /**
         * Constructor - takes the provided context to allow for the database to be
         * opened/created.
         * 
         * @param context the Context within which to work.
         */
        public MyAdapter(Context context) {
            mCtx = context;
        }
    
        /**
            * Open the last.fm database. If it cannot be opened, try to create a new
            * instance of the database. If it cannot be created, throw an exception to
            * signal the failure.
            * 
            * @return this (self reference, allowing this to be chained in an
            *         initialization call)
            * @throws SQLException if the database could be neither opened or created
            */
        public MyAdapter open() throws SQLException {
            mDbHelper = new DatabaseHelper(mCtx);
            mDb = mDbHelper.getWritableDatabase();
            return this;
        }
    
        public void close() {
                mDbHelper.close();
            }
    
    }
    

    Then you could write your test as such:

    public final class MyAdapterTests extends AndroidTestCase {
    
        private static final String TEST_FILE_PREFIX = "test_";
    private MyAdapter mMyAdapter;
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();
    
        RenamingDelegatingContext context 
            = new RenamingDelegatingContext(getContext(), TEST_FILE_PREFIX);
    
        mMyAdapter = new MyAdapter(context);
        mMyAdapter.open();
    }
    
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    
        mMyAdapter.close();
        mMyAdapter = null;
    }
    
    public void testPreConditions() {
        assertNotNull(mMyAdapter);
    }
    
    }
    

    So what’s happening here is that the context implementation of RenamingDelegatingContext, once MyAdapter(context).open() is called, will always recreate the database. Each test you write now will be going against the state of the database after MyAdapter.DATABASE_CREATE_STATEMENT is called.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.