mercredi 20 septembre 2017

How use mockito for testing DAO?

I've started to discovered Mockito library, but i dont know test it. If I want to test for example a DAO class then I need to create what ? For example, how do I test the CreateUtil method? Please help me.

public class UtilDAO {
    public static final String TAG = "UtilDAO";
    private SQLiteDatabase mDatabase;
    private DatabaseHelper mDatabasehelper;
    private Context mContext;
    private String[] mAllColumns = {mDatabasehelper.COLUMN_Util_ID,mDatabasehelper.COLUMN_Util_Util};

    public UtilDAO(Context context) {
        this.mContext = context;
        try {
            open(context);
        } catch (SQLException e) {
            Log.e(TAG, "SQLException on openning database " + e.getMessage());
            e.printStackTrace();
        }
    }
    public void open(Context context) throws SQLException {
        this.mDatabasehelper = DatabaseHelper.getInstance(context);
        mDatabase = mDatabasehelper.getWritableDatabase();
    }
    public void close() {
        mDatabasehelper.close();
    }
    public Util createUtil(int id, String Util) {
        ContentValues values = new ContentValues();
        values.put(mDatabasehelper.COLUMN_Util_ID, id);
        values.put(mDatabasehelper.COLUMN_Util_Util, Util);
        long insertId = mDatabase.insert(mDatabasehelper.TABLE_Util, null, values);
        Cursor cursor = mDatabase.query(mDatabasehelper.TABLE_Util, mAllColumns,mDatabasehelper.COLUMN_Util_ID + " = " + insertId, null, null,null, null);
        cursor.moveToFirst();
        Util newUtil = cursorToUtil(cursor);
        cursor.close();
        return newUtil;
    }
    public Util getUtilById(int id) {
        Cursor cursor = mDatabase.query(mDatabasehelper.TABLE_Util, mAllColumns,mDatabasehelper.COLUMN_Util_ID + " = ?",new String[]{String.valueOf(id)}, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
        }
        Util company = cursorToUtil(cursor);
        cursor.close();
        return company;
    }
    protected Util cursorToUtil(Cursor cursor) {
        Util Util = new Util();
        Util.setId_Util(cursor.getInt(0));
        Util.setM_Util(cursor.getString(1));
        return Util;
    }

    public void deleteAll() {
        mDatabase.delete(mDatabasehelper.TABLE_Util, null, null);
    }
}

I've already seen examples here in stack overflow, but none of them help me.

Aucun commentaire:

Enregistrer un commentaire