2015-05-31 3 views
6

Ich versuche, einen Junit-Test für meine App-Datenbank in Android Studio mit meinem SQLiteOpenHelper-Objekt zu schreiben. Jedes Mal, wenn es die Einfügemethode trifft, erhalte ich eine NullPointerException. Ich bin hin und her gegangen zwischen getContext() und einem mockContext ich Setup aber keine Würfel. Ich habe meinen Emulator eingerichtet. Kann mir bitte jemand sagen, was ich falsch mache?Versuch, SQLiteOpenHelper zu testen, aber GetWritableDatabase() löst Null aus

public class LocationDatabaseHelperTest extends AndroidTestCase { 

    private SQLiteDatabase testDB; 
    private SQLiteDatabase readDB; 
    private LocationDatabaseHelper dbh; 
    private RenamingDelegatingContext mockContext; 
    private ContentValues cv; 



    /** 
    *Sets up a mock context to initialize the 
    * 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 that 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 * FROM " + 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(); 
    } 

} 
+1

Haben Sie eine Lösung gefunden? –

Antwort

0

Sie haben vergessen, Ihre Datenbank zu öffnen. Führen Sie dbh.open() aus. Danach vergessen Sie nicht, Ihre Datenbank zu schließen.

+1

Ich dachte, das ist, was testDB = dbh.getWritableDatabase() tun soll? Das Hinzufügen von dbh.onOpen (testDB) oder dbh.onCreate (testDB) trifft ebenfalls auf Null. – MDM82

Verwandte Themen