簡體   English   中英

使用反射測試asyncTask中的doInBackground()方法

[英]Using reflection to test doInBackground() method in asyncTask

AsyncTask.class中的doInBackground方法的方法簽名如下所示:

protected Boolean doInBackground(MyObject... params);

在帶有反射的測試中,它看起來像這樣:

try {
Method doInBackgroundMethod = asyncTaskInstance.getClass().getDeclaredMethod("doInBackground", MyObject[].class);
            doInBackgroundMethod.setAccessible(true);
            boolean success = (boolean) doInBackgroundMethod.invoke(asyncTaskInstance, myObjectInstance);
            Assert.assertTrue(success);
} catch ...

但我收到以下錯誤:

java.lang.IllegalArgumentException: argument type mismatch, on line "doInBackgroundMethod.invoke(asyncTaskInstance, myObjectInstance);"

是的,我知道有兩種簽名相同的方法,所以我正在尋找解決方案。

代替MyObject[].class而是使用Object[].class因為doInBackground在初始化期間使用set類型,並在調用doInBackground時自動轉換為該類型,但是當您手動執行時-您還需要手動進行轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM