Android Unit Tests Requiring Context

后端 未结 8 1778
抹茶落季
抹茶落季 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;
    }}
    

提交回复
热议问题