我正在使用JUnit
. 这是我第一次JUnit
进行真实测试,所以我不知道我应该遵循的路径。现在我正在测试对联系人列表的查询。许多方法只需要 aRaw Contact ID
或 aContact ID
作为参数,其中大多数要么返回 a JSON Object
,JSON Array
要么返回 a ArrayList
。
我的问题是,我应该如何将我的预期结果与实际结果进行比较?
现在我正在像这样测试它们:
public void testGetRelationship() {
JSONArray jsonArrayActual = new JSONArray();
JSONArray jsonArrayExpected = new JSONArray();
try {
jsonArrayExpected = contact.getRelationship(RAW_CONTACT_ID);
} catch (JSONException e) {
e.printStackTrace();
fail("Failed when calling the getRelationship method. " + e.getMessage());
}
Cursor cursor = getContext().getContentResolver().query(Data.CONTENT_URI,
new String[]{Relation.NAME, Relation.TYPE, Relation._ID, Relation.LABEL},
Relation.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?",
new String[]{RAW_CONTACT_ID, Relation.CONTENT_ITEM_TYPE},
null);
if(cursor.moveToFirst())
{
do{
try {
final JSONObject jsonRelationshipObject = new JSONObject();
final String name = cursor.getString(0);
final Integer type = cursor.getInt(1);
final Integer id = cursor.getInt(2);
final String label = cursor.getString(3);
jsonRelationshipObject.put("id", id);
jsonRelationshipObject.put("type", type);
jsonRelationshipObject.put("label", label);
jsonRelationshipObject.put("name", name);
jsonArrayActual.put(jsonRelationshipObject);
} catch (JSONException e) {
e.printStackTrace();
fail("Failed while adding the values to the JSONObject. " + e.getMessage());
}
} while(cursor.moveToNext());
}
else
{
cursor.close();
fail("RawContact not found! ID: " + RAW_CONTACT_ID);
}
cursor.close();
try {
JSONAssert.assertEquals(jsonArrayExpected, jsonArrayActual, true);
} catch (JSONException e) {
e.printStackTrace();
cursor.close();
fail("JSONAssert exception: " + e.getMessage());
}
}
基本上我正在调用我的真实方法并且几乎重新编码我的(正在测试的)方法。我觉得这没有效率,或者至少很无聊和乏味(再次编码我刚刚编码的内容)。我很确定有更好的方法来执行JUnit
测试。我想自己查询联系人列表数据库并手动构建我的对象进行比较,如下所示:
public void testGetRelationship() {
JSONArray jsonArrayActual = new JSONArray();
JSONArray jsonArrayExpected = new JSONArray();
try {
jsonArrayExpected = contact.getRelationship(RAW_CONTACT_ID);
} catch (JSONException e) {
e.printStackTrace();
fail("Failed when calling the getRelationship method. " + e.getMessage());
}
jsonRelationshipObject.put("id", 6);
jsonRelationshipObject.put("type", 1);
jsonRelationshipObject.put("label", "A label");
jsonRelationshipObject.put("name", "the name");
jsonArrayActual.put(jsonRelationshipObject);
try {
JSONAssert.assertEquals(jsonArrayExpected, jsonArrayActual, true);
} catch (JSONException e) {
e.printStackTrace();
cursor.close();
fail("JSONAssert exception: " + e.getMessage());
}
}
编码速度更快,但缺点是我需要从手机手动下载数据库(对我来说没什么大不了的)并手动查询它以获取那里的数据(也不是什么大不了的事)但事实并非如此如此通用,如果数据库发生更改,我猜所有测试都会失败。但是我可以获取数据库的快照并保存它,然后始终在该快照上执行测试。
有什么建议么?改进?