我目前有點困惑Intent
演員。 我知道所謂的臨時演員是Bundle
而Bundle
在內部是一張地圖。簡單的意圖 - jUnit測試失敗
如果我在這張地圖上放了一些東西,用intent.putExtra("someName", "someValue");
,我預計現在有一個名爲「someName」的關鍵字出現在地圖上。但根據我非常簡單的單元測試,這不是。
public void testIntent(){
assertTrue("true != true", true);
assertFalse("false != false", false);
final String extraName = "IamAnExtra";
final String extraValue = "IamAValue";
Intent intent = new Intent();
intent.putExtra(extraName, extraValue);
assertTrue("hasExtra==false", intent.hasExtra(extraName));
}
現在的部份結果如下:
junit.framework.AssertionFailedError: hasExtra==false
什麼樣的神奇正在發生的意向代碼後面? 我知道它正常工作。僅當我發送意圖時,地圖是否被緩存並寫入 ?先謝謝你!
編輯:
我增加了行assertNotNull("extras == null", intent.getExtras());
這也將失敗。
我查從方法putExtra
和getExtras
,這是代碼:
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
和
public Bundle getExtras() {
return (mExtras != null)
? new Bundle(mExtras)
: null;
}
因此,如果成員mExtras爲空,則返回null爲getExtras。 但putExtra將爲mExtras
創建一個新的Bundle實例,因此 mExtras
應該不爲空。我很困惑。
您是將它作爲_unit test_還是_instrumented test_運行?你在用什麼框架,JUnit 4? – josemigallas
單元測試。我使用了一個正常的TestCase。 – JacksOnF1re
我認爲使用普通的Intent可以和好的老單元一起使用,不是嗎?我不會嘗試發送它或其他東西。 – JacksOnF1re