2013-08-20 69 views
3

首先,我想聲明我對Android缺乏適當的測試基礎結構感到沮喪......或者我可能會被推遲,我不知道...
I最終成功設置了android-test-plugin,並且我爲一個活動編寫了一些測試,其中包括一個CustomMapFragment,這是我寫的延伸SupportMapFragment的課程。
我所有的測試,試圖讓使用此活動的一個實例:
在Android Studio中使用Robolectric問題進行Android測試

activity = Robolectric.buildActivity(MyActivity.class).create().get(); 

失敗:

java.lang.NoClassDefFoundError: com/google/android/gms/maps/GoogleMap$OnInfoWindowClickListener

在活動的setContentView

活動的XML如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context=".MyActivity"> 

    <fragment 
      android:id="@+id/customMapFragment" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      class="com.myproject.CustomMapFragment"/> 
</RelativeLayout> 

有沒有一種解決方法/解決方案? 任何幫助,將不勝感激。

+1

我分享你的無奈。我有同樣的問題,但我無法運行任何測試,因爲當Robolectric嘗試加載類時,我在地圖類上得到了NoClassDefFoundError,所以即使是assertTrue(true)測試也會失敗。這是我的問題與我得到的錯誤http://stackoverflow.com/questions/18434217/robolectric-fails-to-run-tests-with-google-play-services-maps – alex

+0

我認爲這是一個普遍問題,不直接連接到地圖,因爲我在某些使用Google雲消息的類中遇到了同樣的錯誤。很高興知道有人理解你:)以防有人在這裏回答我會告訴你。 –

+0

你是如何添加谷歌播放服務依賴項?使用谷歌資料庫或以另一種方式? – alex

回答

2

我有臨時解決方案。問題似乎是android-test-plugin無法讀取aar文件。我的解決辦法是依賴添加到jar文件,而不是隻testCompile,這樣

testCompile files('YOUR_ANDROID_SDK_FOLDER/extras/google/google_play_services/libproject/google-play-services_lib/libs/google-play-services.jar') 

像這樣我能夠運行一個簡單的測試,例如創建一個經緯度和檢查,如果它不爲空,無錯誤。希望能幫助到你。

編輯

更好的解決方案由於SuperJugy GitHub上是應用在此修復程序提交https://github.com/SuperJugy/gradle-android-test-plugin/commit/d5b7836cf7278dc9d0fe765b635de60c33911d72

+0

只要我有一些時間來測試這個,我會接受你的答案。無論如何,非常感謝@alex。 –

+0

好吧,我放棄了......我實際上設法克服了這些「NoClassDefFoundErrors」,但現在我得到「java.lang.RuntimeException:Stub!」就好像Robolectric是在android.jar之前的類路徑中添加的一樣。無論如何,構建最新版本的android-test-plugin似乎已解決了以前的問題,因此我將接受您的答案。謝謝。 –

+0

呃,我也在爭論一個又一個問題,我現在放棄了寫測試。由於風格問題,我必須使用robolectric的快照版本,並且此版本未找到資源文件... – alex

相關問題