title: Using Junit to test writing and reading to SQLite
body: <p>I have a dbManager as follows: </p>
  
  <pre><code>//TABLE_AUTH Column names
      public static final String AUTH_CODE = "AuthCode";
      public static final String SESSION_ID = "SessionId";
      public static final String AUTH_TIME = "LastAuthorized";
      public static final String ENCRYPT_KEY = "EncryptKey";    
  
  //TABLE_AUTH Create Statement
      public static final String CREATE_AUTH_TABLE = "CREATE TABLE "
              + TABLE_AUTH + "("
              + AUTH_CODE + " TEXT, "
              + SESSION_ID + " TEXT, "
              + AUTH_TIME + " DATETIME, "
              + ENCRYPT_KEY
              +" TEXT" +")";
  
  public DBManager(Context ctx) {
      super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  
  @Override
  public void onCreate(SQLiteDatabase db) {
     db.execSQL(CREATE_AUTH_TABLE);
  
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_AUTH);
      onCreate(db);
  }
  
  public static DBManager getInstance(Context ctx){
      if(instance==null){
          instance = new DBManager(ctx);
      }
      return instance;
  }
  </code></pre>
  
  <p>I wanted to test the writing and reading to this db with android test cases so I developed it is follows:</p>
  
  <pre><code>import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.test.InstrumentationTestCase;
  import android.util.Log;
  
  import com.etlie.stockclient.DBManager.DBManager;
  
  public class SQLiteTest extends InstrumentationTestCase {
  
      DBManager databaseManager;
      SQLiteDatabase dbWrite;
      SQLiteDatabase dbRead;
  
      ContentValues contentValues;
  
      protected void setUp() throws Exception{
          super.setUp();
  
          databaseManager = DBManager.getInstance(getInstrumentation().getContext());
  
          //TABLE AUTH TEST
          dbWrite = databaseManager.getWritableDatabase();
          dbRead = databaseManager.getReadableDatabase();
          contentValues = new ContentValues();
      }
  
      public void testReadWriteAuthTable() throws Exception{
  
          contentValues.put(databaseManager.AUTH_CODE, "auth code");
          contentValues.put(databaseManager.SESSION_ID, "session id");
          contentValues.put(databaseManager.AUTH_TIME, System.currentTimeMillis());
          contentValues.put(databaseManager.ENCRYPT_KEY, "encrypt key");
  
          dbWrite.insert(databaseManager.TABLE_AUTH, null, contentValues);
  
          String selectQuery = "SELECT * FROM " + databaseManager.TABLE_AUTH;
  
          Log.i(databaseManager.LOG_TAG, selectQuery);
  
          Cursor c = dbRead.rawQuery(selectQuery, null);
  
          if(c!=null)
              c.moveToFirst();
  
          String authCode = c.getString(c.getColumnIndex(databaseManager.AUTH_CODE));
          String sessionId = c.getString(c.getColumnIndex(databaseManager.SESSION_ID));
          String encryptKey = c.getString(c.getColumnIndex(databaseManager.ENCRYPT_KEY));
  
          Log.i("AUTH CODE: ", authCode);
          Log.i("SESSION ID: ", sessionId);
          Log.i("ENCRYPT KEY: ", encryptKey);
  
          assertEquals(authCode, "auth code");
          assertEquals(sessionId, "session id");
          assertEquals(encryptKey, "encrypt key");
      }
  }
  </code></pre>
  
  <p>However, when I ran the test suite it crashed where I tried to initialise the dbWrite</p>
  
  <pre><code>dbWrite = databaseManager.getWritableDatabase();
  </code></pre>
  
  <p>and I got the error "error code = 14, unable to open db". I had thought that it was something wrong with my db create code etc. </p>
  
  <p>I then tested in a different way by creating a test activity:</p>
  
  <pre><code>public class TestActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.test_activity_layout);
  
          Button button = (Button) findViewById(R.id.button);
  
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  DBManager dbManager = DBManager.getInstance(getApplicationContext());
                  SQLiteDatabase sqLiteDatabase = dbManager.getWritableDatabase();
                  SQLiteDatabase sqLiteDatabase1 = dbManager.getReadableDatabase();
  
                  ContentValues contentValues = new ContentValues();
                  contentValues.put(dbManager.AUTH_CODE, "auth code");
                  contentValues.put(dbManager.SESSION_ID, "session id");
                  contentValues.put(dbManager.AUTH_TIME, System.currentTimeMillis());
                  contentValues.put(dbManager.ENCRYPT_KEY, "encrypt key");
  
                  sqLiteDatabase.insert(dbManager.TABLE_AUTH, null, contentValues);
  
  
                  String selectQuery = "SELECT * FROM " + dbManager.TABLE_AUTH;
                  Cursor c = sqLiteDatabase1.rawQuery(selectQuery, null);
                  if(c!=null)
                      c.moveToFirst();
  
                  String authCode = c.getString(c.getColumnIndex(dbManager.AUTH_CODE));
                  String sessionId = c.getString(c.getColumnIndex(dbManager.SESSION_ID));
                  String encryptKey = c.getString(c.getColumnIndex(dbManager.ENCRYPT_KEY));
  
                  Log.i("AUTH CODE: ", authCode);
                  Log.i("SESSION ID: ", sessionId);
                  Log.i("ENCRYPT KEY: ", encryptKey);
              }
          });   
      }
  }
  </code></pre>
  
  <p>This logged each entry in the db no problem so I know that data was written and read successfully.  </p>
  
  <p>Can anyone see what might be wrong with the test suite code?  I want to write test cases for this project going forward and if I can't figure this out I can't do so...</p>