2

我正在为我的 android 应用程序开发数据库层,我想为此使用测试用例,但我不知道如何在 android 开发环境中解决这个问题。

我有这个 DBHelper 类

public class DBHelper extends OrmLiteSqliteOpenHelper{

    private static final String DATABASE_NAME = "pdixattach.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TAG = DBHelper.class.getSimpleName();
    private static DBHelper _helperInstance;

    private Dao<Attachment, Integer> attachmentDao = null;
    private Dao<User, Integer> userDao = null;
    private Dao<Comment, Integer> commentDao = null;
    private Dao<Job, Integer> jobDao = null;
    private Dao<Target, Integer> targetDao = null;


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

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource source) {
        Log.i(TAG, "onCreate");
        try{
            TableUtils.createTable(source, Attachment.class);
            TableUtils.createTable(source, User.class);
            TableUtils.createTable(source, Comment.class);
            TableUtils.createTable(source, Target.class);
            TableUtils.createTable(source, Job.class);

        } catch (Exception e){
            Log.e(TAG, "error while creating tables "  + e.getMessage());
            throw new RuntimeException(e);
        }

    }

    @Override
    public void onUpgrade(final SQLiteDatabase db, final ConnectionSource connectionSource, final int oldVersion, final int newVersion) {
        Log.i(TAG, "onUpgrade");
        try {
            TableUtils.dropTable(connectionSource, Attachment.class, true);
            TableUtils.dropTable(connectionSource, User.class, true);
            TableUtils.dropTable(connectionSource, Target.class, true);
            TableUtils.dropTable(connectionSource, Job.class, true);
            TableUtils.dropTable(connectionSource, Comment.class, true);
        } catch (SQLException e) {
            Log.e(TAG, "error while upgrading tables " + e.getMessage());
            throw new RuntimeException(e);
        }

        // after we drop the old databases, we create the new ones
        onCreate(db, connectionSource);

    }



    public Dao<Attachment, Integer> getAttachmentDao() throws SQLException {
        if (this.attachmentDao == null) {
            this.attachmentDao = getDao(Attachment.class);
        }
        return this.attachmentDao;
    }

    public Dao<User, Integer> getUserDao() throws SQLException {
        if (this.userDao == null) {
            this.userDao = getDao(User.class);
        }
        return this.userDao;
    }

    public Dao<Comment, Integer> getCommentDao() throws SQLException {
        if (this.commentDao == null) {
            this.commentDao = getDao(User.class);
        }
        return this.commentDao;
    }

    public Dao<Target, Integer> getTargetDao() throws SQLException {
        if (this.targetDao == null) {
            this.targetDao = getDao(User.class);
        }
        return this.targetDao;
    }

    public Dao<Job, Integer> getJobDao() throws SQLException {
        if (this.jobDao == null) {
            this.jobDao = getDao(User.class);
        }
        return this.jobDao;
    }


    /**
     * Close the database connections and clear any cached DAOs.
     */
    @Override
    public void close() {
        super.close();
        _helperInstance = null;
        this.attachmentDao = null;
        this.commentDao = null;
        this.jobDao = null;
        this.targetDao = null;
        this.userDao = null;
    }

使用这个 DBManager:

public class DBManager {

    private Dao<User,Integer> userDao;
    private Dao<Attachment,Integer> attachmentDao;
    private Dao<Target,Integer> targetDao;
    private Dao<Comment,Integer> commentDao;
    private Dao<Job,Integer> jobDao;
    private DBHelper helper;

    private static DBManager uniqueInstance;
    private static final String TAG = DBManager.class.getSimpleName();

    public DBManager(Context context) {
        helper = new DBHelper(context);
    }


    public static void init(Context context){
        if (uniqueInstance == null) {
            uniqueInstance = new DBManager(context);
        }
    }


    public static synchronized DBManager getInstance(){
        return uniqueInstance;
    }


    private void injectDBHelper(DBHelper dbhelper) {            
        if (this.helper == null)
            this.helper = dbhelper;
        else
            Log.d(TAG, "DBHelper already available in DBManager");
    }


    public boolean addUser(User u){
        boolean retVal = false;
        if (u == null){
            throw new IllegalArgumentException("user must not be null");
        }

        try {                                                               
            helper.getUserDao().create(u);
            retVal = true;
        } catch (SQLException e) {
            Log.e(TAG, "error while adding user to db " + e.getMessage());
        }

        return retVal;
    }



    public boolean addServiceEndpoint(String endpoint) {
        boolean retVal = false;
        if (endpoint == null){
            throw new IllegalArgumentException("endpoint must not be null");
        }

        try {
            Target t = new Target(endpoint);                
            int result = helper.getTargetDao().create(t);
            retVal = (result == 1);

        } catch (SQLException e) {
            Log.e(TAG, "error while adding target to db, with service endpoint " + endpoint + "error" + e.getMessage());
        }

        return retVal;

    }

我想为 addUser 方法生成测试用例,有人可以帮助我吗?如何在 android 开发环境中实现这一点?

谢谢

4

0 回答 0