2015-12-14 13 views
9

मैं एंड्रॉइड ऐप विकास के लिए नया हूं और मैंने अभी एक नोट ऐप बनाया है। मैं डेटाबेस के लिए insertNote, readNote और अद्यतन नोट विधियों के लिए यूनिट परीक्षण करना चाहता हूं। यह कैसे करना है? यह मेरे डेटाबेस के लिए कोड है। धन्यवाद।एंड्रॉइड एसक्यूएलएट डाटाबेस यूनिट परीक्षण

public class DatabaseManager extends SQLiteOpenHelper { 


public static final String Database_Name = "Notes Database"; 
public static final String Table_Name = "notes"; 

public static final String Column_id = "textId"; 
public static final String Column_title = "textTitle"; 
public static final String Column_body = "textBody"; 

public DatabaseManager(Context context){ 
    super(context, Database_Name, null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase db){ 
    db.execSQL("CREATE TABLE " + Table_Name + " (" + Column_id + 
      " INTEGER PRIMARY KEY AUTOINCREMENT, " + Column_title + 
      " TEXT, " + Column_body + " TEXT)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 
    db.execSQL("DROP TABLE IF EXISTS" + Table_Name); 
    onCreate(db); 
} 

public void insertNote(Note note){ 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(Column_title, note.getTextTitle()); 
    values.put(Column_body, note.getTextBody()); 

    boolean result = db.insert(Table_Name, null, values) > 0; 
    if (result == true) 
     Log.d("Create", "Data Has Been Saved"); 
    db.close(); 
} 

public Cursor readNote(int id){ 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.rawQuery("SELECT * FROM " + Table_Name + " WHERE _ROWID_ = " + id, null); 

    if (cursor != null){ 
     cursor.moveToFirst(); 
    } 

    Note myNote = new Note(); 
    myNote.textId = cursor.getInt(cursor.getColumnIndex(Column_id)); 
    myNote.textTitle = cursor.getString(cursor.getColumnIndex(Column_title)); 

    return cursor; 
} 

public ArrayList<String> getNoteList(){ 
    ArrayList<String> noteList = new ArrayList<>(); 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("SELECT * FROM " + Table_Name, null); 
    cursor.moveToFirst(); 

    if (cursor.moveToFirst()){ 
     do{ 
      //noteList.add(cursor.getInt(cursor.getColumnIndex(Column_id))); 
      noteList.add(cursor.getString(cursor.getColumnIndex(Column_title))); 
     } 
     while (cursor.moveToNext()); 
    } 

    return noteList; 
} 

public int updateNote(Note note){ 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put("textTitle", note.getTextTitle()); 
    values.put("textBody", note.getTextBody()); 

    int update = db.update(Table_Name, values, Column_id + " = ?", new String[]{Integer.toString(note.getTextId())}); 
    return update; 
} 

public Integer deleteNote(int id){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.delete(Table_Name, Column_id + " = ?", new String[]{Integer.toString(id)}); 
} 

public int getCount(){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery("SELECT * FROM " + Table_Name, null); 
    cursor.close(); 

    return cursor.getCount(); 
} 

}

उत्तर

5

कोशिश कुछ ट्यूटोरियल खोजने के लिए, जावा में इकाई परीक्षण SQLite डेटाबेस के बारे में लेख। जावा और एंड्रॉइड में यूनिट परीक्षण निश्चित रूप से वही है।

यद्यपि आप ढेर पर इन विषयों मिलेगा:

How to test Android sqlite with junit?

How to test methods that deal with SQLite database in android?

या इस अनुच्छेद:

Android Easy SQLite With Unit Tests

JUnit साथ परीक्षण SQLite databese के अनुसार, इस जाँच :

Android JUnit test for SQLiteOpenHelper

जहाँ आप इस समाधान खोजने होगा:

एक सरल DatabaseHandler के लिए:

<!-- language: java --> 

    public class DatabaseTest extends AndroidTestCase { 
     private MyDatabase db; 

     @Override 
     public void setUp() throws Exception { 
      super.setUp(); 
      RenamingDelegatingContext context = new RenamingDelegatingContext(getContext(), "test_"); 
      db = new MyDatabase(context); 
     } 

     @Override 
     public void tearDown() throws Exception { 
      db.close(); 
      super.tearDown(); 
     } 

     //According to Zainodis annotation only for legacy and not valid with gradle>1.1: 
     //@Test 
     public void testAddEntry(){ 
      // Here i have my new database wich is not connected to the standard database of the App 
     } 
    } 

पढ़ें भी:

<!-- language: java --> 

    public class MyDatabase extends SQLiteOpenHelper { 
     private static final String DATABASE_NAME = "database.db"; 
     private static final int DATABASE_VERSION = 1; 

     public MyDatabase(Context context){ 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db){ 
      // some code 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // some code 
     } 
    } 

मैं एक AndroidTestCase बनाया:

Using Junit to test writing and reading to SQLite

Testing SQLiteOpenHelper subclass with JUnit

14

JUnit इकाई परीक्षण के साथ SQLite टेस्ट को लागू करने की गुप्त के अधिकांश वर्ग org.junit.Assert में ज़ोर तरीकों के उपयोग में है। इस पाठ में मैं इस वर्ग में कौन सी जोरदार विधियां उपलब्ध हैं, इस पर एक नज़र डालेंगे।

एंड्रॉइड परीक्षण पैकेज में InstrumentationRegistry का उपयोग करने का सबसे आसान तरीका है।

एक उदाहरण:

import android.support.test.InstrumentationRegistry; 
import android.support.test.runner.AndroidJUnit4; 
import android.test.suitebuilder.annotation.LargeTest; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

import java.util.List; 

import static junit.framework.Assert.assertNotNull; 
import static junit.framework.Assert.assertTrue; 
import static org.hamcrest.CoreMatchers.is; 
import static org.hamcrest.MatcherAssert.assertThat; 

@RunWith(AndroidJUnit4.class) 
@LargeTest 
public class SQLiteTest { 

    private RateDataSource mDataSource; 

    @Before 
    public void setUp(){ 
     mDataSource = new RateDataSource(InstrumentationRegistry.getTargetContext()); 
     mDataSource.open(); 
    } 

    @After 
    public void finish() { 
     mDataSource.close(); 
    } 

    @Test 
    public void testPreConditions() { 
     assertNotNull(mDataSource); 
    } 

    @Test 
    public void testShouldAddExpenseType() throws Exception { 
     mDataSource.createRate("AUD", 1.2); 
     List<Rate> rate = mDataSource.getAllRates(); 

     assertThat(rate.size(), is(1)); 
     assertTrue(rate.get(0).toString().equals("AUD")); 
     assertTrue(rate.get(0).getValue().equals(1.2)); 
    } 

    @Test 
    public void testDeleteAll() { 
     mDataSource.deleteAll(); 
     List<Rate> rate = mDataSource.getAllRates(); 

     assertThat(rate.size(), is(0)); 
    } 

    @Test 
    public void testDeleteOnlyOne() { 
     mDataSource.createRate("AUD", 1.2); 
     List<Rate> rate = mDataSource.getAllRates(); 

     assertThat(rate.size(), is(1)); 

     mDataSource.deleteRate(rate.get(0)); 
     rate = mDataSource.getAllRates(); 

     assertThat(rate.size(), is(0)); 
    } 

    @Test 
    public void testAddAndDelete() { 
     mDataSource.deleteAll(); 
     mDataSource.createRate("AUD", 1.2); 
     mDataSource.createRate("JPY", 1.993); 
     mDataSource.createRate("BGN", 1.66); 

     List<Rate> rate = mDataSource.getAllRates(); 
     assertThat(rate.size(), is(3)); 

     mDataSource.deleteRate(rate.get(0)); 
     mDataSource.deleteRate(rate.get(1)); 

     rate = mDataSource.getAllRates(); 
     assertThat(rate.size(), is(1)); 
    } 
} 
में उदाहरण है
संबंधित मुद्दे