java - Trying to test SQLiteOpenHelper but getWritableDatabase() throws Null -


i'm trying write junit test against apps database in android studio using sqliteopenhelper object. every time hits insert method nullpointerexception. i've gone , forth between using getcontext() , mockcontext setup no dice. have emulator , running. can please tell me i'm doing wrong?

public class locationdatabasehelpertest extends androidtestcase {      private sqlitedatabase testdb;     private sqlitedatabase readdb;     private locationdatabasehelper dbh;     private renamingdelegatingcontext mockcontext;     private contentvalues cv;        /**      *sets mock context initialize      * locationdatabasehelper object.      * @throws exception      */     public void setup() throws exception {         super.setup();           final string prefix = "test";         mockcontext = new renamingdelegatingcontext(getcontext(),prefix);         dbh = new locationdatabasehelper(mockcontext);          try {             testdb = dbh.getwritabledatabase();         } catch(exception ex){             ex.printstacktrace();         }          readdb = dbh.getreadabledatabase();         cv = new contentvalues();     }       /**      *tests asserts locationdatabasehelper object instantiates.      */     public void testlocationdatabasehelper() {         assertnotnull(dbh);     }        public void testinsert() {           string id = "seattle";         float heading = (float) 20.178545;         double longitude = 122.20;         double lat = 47.37;         float speed = (float) 65.4587;         long time = system.currenttimemillis();         locationpackage locations = new locationpackage(id,heading,longitude, lat,speed, time);           cv.put(dbh.column_location_id, (string) locations.id);         cv.put(dbh.column_location_heading, (float) locations.heading);         cv.put(dbh.column_location_lat, (double) locations.latitude);         cv.put(dbh.column_location_speed, (float) locations.speed);         cv.put(dbh.column_location_long, (double) locations.longitude);         cv.put(dbh.column_location_timestamp, (long) locations.time);            testdb.insert(dbh.table_location, null, cv);            string selectquery = "select * " + dbh.table_location;         log.i(dbh.tostring(), selectquery);         cursor c = readdb.rawquery(selectquery, null);         if(c!=null)             c.movetofirst();          string locid = c.getstring(c.getcolumnindex(dbh.column_location_id));          //log.i("locationid: ", locid);         assertequals(locid, "userid");           //asserttrue(insertid != -1);     }       public void teardown() throws exception {          super.teardown();          //dbh.cleardatabase();     }  } 

you forgot open database. dbh.open(). after don't forget close db.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -