0

While trying to develop some unit tests for the project that I'm working on I hit a wall where I need to test DB-behaviour. I'm trying to create a in-memory DB(SQLite)

While I thought that this rather basic test would do as asked, it always returns a NULL database, which is not what I expected.

package be.wgkovl.evdt.Util;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.test.mock.MockContext;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith( PowerMockRunner.class )
public class ClassForStack {


    SQLiteDatabase db;
    Context context;

    private Cursor test() {
        if (db != null) {
            return db.rawQuery("Select * from myTable", new String[]{});
        } else {
            System.out.println("Db is null :-(");
        }
        return null;
    }

    private String[] emtpyArgument = new String[]{};


    @Before
    public void setup() {
        System.out.println("Setting up");
        context = new MockContext();

        SQLiteOpenHelper dummy = new SQLiteOpenHelper(context, "SomeName.memory", null, 1) {
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
            }
        };


        String basicInitQuery = "CREATE TABLE IF NOT EXISTS myTable(id INTEGER PRIMARY KEY DESC, value TEXT)";
        String basicInsertQuery = "INSERT INTO myTable(id, value) VALUES (1,?)";
        db = dummy.getWritableDatabase();

    }

    @Test
    public void baseDbTest() {
        Cursor c = test();
        if (c != null) {
            System.out.println(c.getColumnNames());
            System.out.println(c.getColumnCount());
            System.out.println("Starting test");
        }
    }
}

I have looked at several different solutions on Stackoverflow, but none seemed to be able to point me to the right solution. They are listed here

getWritableDatabase returns null
Android Sqlite always null even after calling getWritableDatabase()
Create SQLite database in android
SQLite getReadableDatabase() returns NULL
SQLiteOpenHelper failing to call onCreate?

What am I doing wrong that I get a null db, but not any kind of error or exception?

ShadowFlame
  • 2,996
  • 5
  • 26
  • 40
  • Just to confirm, is this a connected test defined in `/androidTest`? – Michael Dodd Jul 12 '18 at 14:31
  • what do you mean connected test? It is currently in the designated test folder in my structure. – ShadowFlame Jul 12 '18 at 16:01
  • There are two test folders under `src/` - `src/test` which contains JUnit tests that are [run on your local machine](https://developer.android.com/training/testing/unit-testing/local-unit-tests), and `src/androidTest` which runs unit tests [on a connected Android device](https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests). You cannot use Android-specific classes in the `src/test` folder (in your case, `Cursor`, `Context` and `SQLiteOpenHelper`). I suspect this is why you're getting `null` in return. – Michael Dodd Jul 12 '18 at 16:10
  • it should be able to run those tests locally. What use is a mockContext if not for local tests? – ShadowFlame Jul 12 '18 at 16:12
  • But your entire test is dependent on `SQLiteOpenHelper`, is it not? – Michael Dodd Jul 12 '18 at 16:12
  • that is indeed the case. is there a local alternative for that one?(is cursor device only? I thought that was just a DB result) – ShadowFlame Jul 12 '18 at 16:14
  • For now, see if you can recreate this test [as an instrumented test](https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests) and see what happens when you run it on a device or emulator. – Michael Dodd Jul 12 '18 at 16:15

1 Answers1

0

Did you forget to create the table inside onCreate()?

  db.execSQL(basicInitQuery);
  • as far as I see, the onCreate() isn't triggered. It does not show my logging in any case.. – ShadowFlame Jul 12 '18 at 16:00
  • how else would the table be created? –  Jul 12 '18 at 16:11
  • in a function that you call later. if you need to define the entirety of your database in the onCreate, it should give an exception if you do nothing there, instead of a null database object. – ShadowFlame Jul 12 '18 at 16:13
  • I have tried it yes, it didn't seem to work. Since I'm at home now, It'll have to wait for tomorrow untill I can try againt – ShadowFlame Jul 12 '18 at 16:52