我一直在使用Robolectric進行測試,這一切都很棒。然後,我將Google Analytics(分析)應用於我的應用程序類,並且測試開始失敗。在測試過程中充氣時,失敗似乎會發生。這裏的堆棧跟蹤:Robolectric和Google Analytics測試錯誤
java.lang.NullPointerException: null
at com.google.analytics.tracking.android.AnalyticsGmsCoreClient$AnalyticsServiceConnection.onServiceConnected(AnalyticsGmsCoreClient.java:176)
at org.robolectric.shadows.ShadowApplication$2.run(ShadowApplication.java:209)
at org.robolectric.util.Scheduler$PostedRunnable.run(Scheduler.java:162)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:107)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:92)
at org.robolectric.util.Scheduler.advanceToLastPostedRunnable(Scheduler.java:68)
at org.robolectric.util.Scheduler.unPause(Scheduler.java:25)
at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:219)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:258)
at org.robolectric.shadows.ShadowViewGroup.addView(ShadowViewGroup.java:32)
at android.view.ViewGroup.addView(ViewGroup.java)
at android.view.ViewGroup.addView(ViewGroup.java:3225)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:750)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at org.robolectric.shadows.ShadowView.inflate(ShadowView.java:82)
at android.view.View.inflate(View.java)
下面是我的BaseActivity的Robolectric代碼:
@RunWith(RobolectricTestRunner.class)
public class BaseActivityTest {
ActivityController<TestActivity> activityController;
TestActivity activity;
@Before
public void setUp(){
activityController = Robolectric.buildActivity(TestActivity.class).create().start();
}
@After
public void takeDown(){
activityController.stop().destroy();
activity = null;
}
@Test
public void testOnPauseState(){
activity = activityController.resume().pause().get();
assertFalse(activity.getBus().isActive());
}
}
繼Google Analytics example,我的應用程序類實現谷歌Analytics(分析)。每當我用我的應用程序運行測試時,就會發生中斷。我嘗試過實現Robolectric的MockApplication對象,但這沒有任何改變;我必須從我的應用程序對象中刪除Google Analytics才能使用它。有沒有人有任何解決方案可以讓我使用Google Analytics運行Robolectric?
第二個選項工作。要記住鏈接中解釋的一件重要事情是,應用程序類必須位於同一個包中(並且與測試應用程序類中的「Test」具有相同的名稱)。第一種選擇儘管有意義,但在測試過程中似乎沒有註冊。 – Maxwell
第一個選項不爲我工作,由於它未能在'GoogleAnalytics.getInstance(這)'因此它永遠不會到達包含'setAppOptOut(真)行' –