1. 程式人生 > 其它 >android 自動化測試 Junit 中使用intent

android 自動化測試 Junit 中使用intent

技術標籤:Android

在Android中使用junit測試時,

在local test中,

不需要測試activity,但是類裡使用到intent的時候,可以這樣子取得intent

專案的junit,robolectric,請自行設定。

@RunWith(RobolectricTestRunner.class)
public class SteraPaymentUtilTest {

@Test
public void test01() {
    //如此定義,即可取得intent
    Intent intent = new Intent(Robolectric.application.getApplicationContext(),     Activity.class);

}

}

另外在顯示測試結果的時候,有時候會有亂碼

android studio 亂碼

只需要在檔案(C:\Users\使用者名稱\.AndroidStudio4.0\config\studio64.exe.vmoptions)里加上編碼設定即可。

編碼設定追加方法,選擇選單「Help>Edit Custom VM Options..」

android studio 亂碼

會開啟以下檔案,如果檔案不存在會自動建立。

貼上【-Dfile.encoding=UTF-8】即可

android studio 亂碼

重啟Android studio之後,顯示正常了。

android studio 亂碼