Android Unit Tests Requiring Context

后端 未结 8 1744
抹茶落季
抹茶落季 2020-12-13 23:47

I am writing my first Android database backend and I\'m struggling to unit test the creation of my database.

Currently the problem I am encountering is obtaining a va

相关标签:
8条回答
  • 2020-12-14 00:04

    First Create Test Class under (androidTest).

    Now use following code:

    public class YourDBTest extends InstrumentationTestCase {
    
    private DBContracts.DatabaseHelper db;
    private RenamingDelegatingContext context;
    
    @Override
    public void setUp() throws Exception {
        super.setUp();
        context = new RenamingDelegatingContext(getInstrumentation().getTargetContext(), "test_");
        db = new DBContracts.DatabaseHelper(context);
    }
    
    @Override
    public void tearDown() throws Exception {
        db.close();
        super.tearDown();
    }
    
    @Test
    public void test1() throws Exception {
        // here is your context
        context = context;
    }}
    
    0 讨论(0)
  • 2020-12-14 00:05

    Extending AndroidTestCase and calling AndroidTestCase:getContext() has worked fine for me to get Context for and use it with an SQLiteDatabase.

    The only niggle is that the database it creates and/or uses will be the same as the one used by the production application so you will probably want to use a different filename for both

    eg.

      public static final String    NOTES_DB      = "notestore.db";
      public  static final String   DEBUG_NOTES_DB = "DEBUG_notestore.db";
    
    0 讨论(0)
  • 2020-12-14 00:06

    Your test is not a Unit test!!!

    When you need

    • Context
    • Read or Write on storage
    • Access Network
    • Or change any config to test your function

    You are not writing a unit test.

    You need to write your test in androidTest package

    0 讨论(0)
  • 2020-12-14 00:08

    You can derive from MockContext and return for example a MockResources on getResources(), a valid ContentResolver on getContentResolver(), etc. That allows, with some pain, some unit tests.

    The alternative is to run for example Robolectric which simulates a whole Android OS. Those would be for system tests: It's a lot slower to run.

    0 讨论(0)
  • 2020-12-14 00:10

    You can use InstrumentationRegistry methods to get a Context:

    InstrumentationRegistry.getTargetContext() - provides the application Context of the target application.

    InstrumentationRegistry.getContext() - provides the Context of this Instrumentation’s package.


    For AndroidX use InstrumentationRegistry.getInstrumentation().getTargetContext() or InstrumentationRegistry.getInstrumentation().getContext().

    0 讨论(0)
  • 2020-12-14 00:14

    You might try switching to AndroidTestCase. From looking at the docs, it seems like it should be able to provide you with a valid Context to pass to SQLiteOpenHelper.

    Edit: Keep in mind that you probably have to have your tests setup in an "Android Test Project" in Eclipse, since the tests will try to execute on the emulator (or real device).

    0 讨论(0)
提交回复
热议问题