昨年(2011年)の8月に開催された
Androidテスト祭りで@ussy00さんの発表を聞いてRenamingDelegatingContextの存在を
知ってから結構経ってしまったけど、ようやく仕事でDBを使う機会が出来たので使ってみた。
なにが出来るの?
RenamingDelegatingContextを使うことでテスト用のプレフィックスのついたSQLiteファイルが用意され、
毎回クリーンなデータベース環境が手に入る。
使ってみる
一応テスト対象のクラス等も紹介しておく。
Employee.java(DTO的な何か。必要に応じてシリアライズ可能に。)
※ ゲッター/セッター、import省略
- public class Employee {
-
- public static class EmployeeColumns {
-
- public static final String ID = "_id";
-
- public static final String NAME = "name";
-
- public static final String DEPARTMENT = "department";
- }
-
- public static final String TABLE_NAME = "employee";
-
- private int id;
- private String name;
- private String department;
- }
public class Employee {
public static class EmployeeColumns {
public static final String ID = "_id"; // 社員ID
public static final String NAME = "name"; // 社員名
public static final String DEPARTMENT = "department"; // 部署
}
public static final String TABLE_NAME = "employee";
private int id;
private String name;
private String department;
}
テーブル構成も簡単にしてみた。
DatabaseHelper.java(ヘルパー)
※ import省略
- public class DatabaseHelper extends SQLiteOpenHelper {
-
- private static final String DATABASE_NAME = "company.db";
- private static final int DATABASE_VERSION = 1;
-
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE " + Employee.TABLE_NAME + "(" +
- Employee.EmployeeColumns.ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
- Employee.EmployeeColumns.NAME + " TEXT NOT NULL," +
- Employee.EmployeeColumns.DEPARTMENT + " TEXT NOT NULL" +
- ")");
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- }
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "company.db";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + Employee.TABLE_NAME + "(" +
Employee.EmployeeColumns.ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Employee.EmployeeColumns.NAME + " TEXT NOT NULL," +
Employee.EmployeeColumns.DEPARTMENT + " TEXT NOT NULL" +
")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
EmployeeDao.java(データベースアクセスクラス。今回のテスト対象クラス。)
※ import省略
- public class EmployeeDao {
-
- private DatabaseHelper mHelper;
-
- public EmployeeDao(Context context) {
- mHelper = new DatabaseHelper(context);
- }
-
-
-
-
-
-
-
- public List<Employee> getEmployeeByDepartment(String department) {
- List<Employee> employees = new ArrayList<Employee>();
- SQLiteDatabase db = mHelper.getReadableDatabase();
-
- try {
- Cursor c = db.query(Employee.TABLE_NAME,
- new String[]{ EmployeeColumns.ID,
- EmployeeColumns.NAME,
- EmployeeColumns.DEPARTMENT },
- EmployeeColumns.DEPARTMENT + " = ?",
- new String[]{ department }, null, null, null);
- c.moveToFirst();
- while (!c.isAfterLast()) {
- Employee employee = new Employee();
- employee.setId(c.getInt(c.getColumnIndex(EmployeeColumns.ID)));
- employee.setName(c.getString(c.getColumnIndex(EmployeeColumns.NAME)));
- employee.setDepartment(c.getString(c.getColumnIndex(EmployeeColumns.DEPARTMENT)));
- employees.add(employee);
- c.moveToNext();
- }
- c.close();
- } finally {
- db.close();
- }
- return employees;
- }
- }
public class EmployeeDao {
private DatabaseHelper mHelper;
public EmployeeDao(Context context) {
mHelper = new DatabaseHelper(context);
}
/**
* 部署を条件に社員一覧を取得する。
* 順不同
* @param department
* @return 社員一覧 0件の場合は空のリスト
*/
public List<Employee> getEmployeeByDepartment(String department) {
List<Employee> employees = new ArrayList<Employee>();
SQLiteDatabase db = mHelper.getReadableDatabase();
try {
Cursor c = db.query(Employee.TABLE_NAME,
new String[]{ EmployeeColumns.ID,
EmployeeColumns.NAME,
EmployeeColumns.DEPARTMENT },
EmployeeColumns.DEPARTMENT + " = ?",
new String[]{ department }, null, null, null);
c.moveToFirst();
while (!c.isAfterLast()) {
Employee employee = new Employee();
employee.setId(c.getInt(c.getColumnIndex(EmployeeColumns.ID)));
employee.setName(c.getString(c.getColumnIndex(EmployeeColumns.NAME)));
employee.setDepartment(c.getString(c.getColumnIndex(EmployeeColumns.DEPARTMENT)));
employees.add(employee);
c.moveToNext();
}
c.close();
} finally {
db.close();
}
return employees;
}
}
そして今回のポイントとなるテストクラス。
EmployeeDaoTest.java
※ import省略
- public class EmployeeDaoTest extends AndroidTestCase {
-
- private static final String TEST_PREFIX = "test_";
- private DatabaseHelper mHelper;
- private RenamingDelegatingContext mContext;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- mContext = new RenamingDelegatingContext(getContext(), TEST_PREFIX);
-
- mHelper = new DatabaseHelper(mContext);
- }
-
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
- mHelper.close();
- }
-
-
-
-
- public void testGetEmployeeByDepartment() {
-
- EmployeeDao dao = new EmployeeDao(mContext);
- List<Employee> employees = dao.getEmployeeByDepartment("人事部");
- assertNotNull(employees);
-
- }
- }
public class EmployeeDaoTest extends AndroidTestCase {
private static final String TEST_PREFIX = "test_";
private DatabaseHelper mHelper;
private RenamingDelegatingContext mContext;
@Override
protected void setUp() throws Exception {
super.setUp();
mContext = new RenamingDelegatingContext(getContext(), TEST_PREFIX);
// テストメソッド毎に空のテスト用DBを用意
mHelper = new DatabaseHelper(mContext);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
mHelper.close();
}
/**
* getEmployeeByDepartmentのテスト
*/
public void testGetEmployeeByDepartment() {
// RenamingDelegatingContextを渡してテスト用DBを使用する
EmployeeDao dao = new EmployeeDao(mContext);
List<Employee> employees = dao.getEmployeeByDepartment("人事部");
assertNotNull(employees);
// これ以降のテストは省略
}
}
ソースを見ればわかると思うけど手順は簡単で
- Context と文字列を渡してRenamingDelegatingContextを生成
- 生成したRenamingDelegatingContextを渡してヘルパーを生成
以上。
これを実行すると。。。
テスト用のDBがちゃんとあるね。