Android [intermediate tutorial] Chapter 4 unit test androidtestcase

Source: Internet
Author: User

I believe that many of you will feel inconvenient to operate SQLite, and you cannot clearly see the problem in database operations. Here we will take the SQLite operation helper class in the previous chapter for unit testing. OK, let's take a look at the class code: databasehelper. java

Import android. content. context; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqliteopenhelper; public class databasehelper extends sqliteopenhelper {// database name Private Static final string db_name = "sqlitedemo. DB "; // database version Private Static final int db_version = 1; // table name public static final string table_name =" Demo "; private Static final string db_create = "create table" + table_name + "(_ id integer primary key autoincrement, name varchar (20), number varchar (10 ))"; public databasehelper (context) {super (context, db_name, null, db_version);}/*** create table */@ overridepublic void oncreate (sqlitedatabase db1_mongodb.exe csql (db_create );} /*** update table */@ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// db.exe csql ("Drop table if exists" + table_name ); // oncreate (db );}}

Next, go to the data operation auxiliary class databaseserver. Java

Import android. content. contentvalues; import android. content. context; import android. database. cursor; import android. database. SQLite. sqlitedatabase; public class databaseserver {private databasehelper dbhelper; Public databaseserver (context) {This. dbhelper = new databasehelper (context);}/***** insert data ** @ Param name * @ Param Number * Data * @ return returns true if the call succeeds, otherwise, false */Public Boolean insert (Str Ing name, string number) {// create or open the database sqlitedatabase DB = dbhelper. getwritabledatabase (); contentvalues CV = new contentvalues (); cv. put ("name", name); cv. put ("Number", number); // insert data, return the insert data idlong id = dB. insert (dbhelper. table_name, null, CV); If (ID! = 0) {return true;} return false ;} /***** update data ** @ Param ID * data column _ id * @ Param Number * quantity * @ return true if the operation succeeds, otherwise, false */Public Boolean Update (int id, string number) {sqlitedatabase DB = dbhelper. getwritabledatabase (); // The contetvalues that comes with Android. Similar to map, the put (string key, xxx value) method is provided to store data contentvalues CV = new contentvalues (); cv. put ("Number", number); // update the data table through contentvalues, and return the updated ID value int result = dB. updat E (dbhelper. table_name, CV, "_ id =? ", New string [] {string. valueof (ID)}); If (result! = 0) {return true;} return false;}/*** delete data ** @ Param ID * data column _ id * @ return */Public Boolean Delete (int id) {sqlitedatabase DB = dbhelper. getwritabledatabase (); // Delete the specified id value int result = dB. delete (dbhelper. table_name, "_ id =? ", New string [] {string. valueof (ID)}); If (result! = 0) {return true;} return false;}/*** query data ** @ return data list */Public cursor fetchall () {sqlitedatabase DB = dbhelper. getreadabledatabase (); // query all fields in the data table. query (dbhelper. table_name, null, "_ id DESC"); If (cursor! = NULL) {return cursor;} return NULL ;}}

Both classes have been created, but we need to test whether the two classes we created work normally, whether they can be inserted, updated, deleted, or queried. Here we use testcase for testing.

Sqlitedemotest. Java

Import android. database. cursor; import android. test. androidtestcase; public class sqlitedemotest extends androidtestcase {private databaseserver dbserver;/*** test data insertion */Public void testinsert () {dbserver = new databaseserver (this. getcontext (); Boolean flag = dbserver. insert ("Sha Seng", "10"); system. out. println (FLAG);}/*** test query data */Public void testfetchall () {dbserver = new databaseserver (this. getcontext (); cursor = dbserver. fetchall (); While (cursor. movetonext () {system. out. println (cursor. getstring (1); system. out. println (cursor. getstring (2) ;}}/*** test data update */Public void testupdate () {dbserver = new databaseserver (this. getcontext (); Boolean flag = dbserver. update (1, "20"); system. out. println (FLAG);}/*** test data deletion */Public void testdelete () {dbserver = new databaseserver (this. getcontext (); Boolean flag = dbserver. delete (1); system. out. println (FLAG );}}

Of course, do not forget to add the Test Library file to androidmanifest. xml.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.kang.button_demo" android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="10" /><application android:icon="@drawable/icon" android:label="@string/app_name"><uses-library android:name="android.test.runner" /><activity android:label="@string/app_name" android:name=".SQLiteDemo"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application><instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.kang.button_demo" android:label="Tests for My App" /></manifest>

Here is a <uses-library> Test Library, which must be added, and <instrumentation Android: Name = "android. Test. instrumentationtestrunner"
Android: targetpackage = "com. kang. button_demo "Android: Label =" tests for my app "/> must also be added. This is the test package. Your test file must be placed under the root package, here, my root package is com. kang. button_demo

Okay, you can run it to see if blue bars appear, instead of red bars,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.