android - Robolectric 和 Google Analytics 测试错误

标签 android google-analytics robolectric

我一直在使用 Robolectric 运行测试,一切都很好。然后我在我的应用程序类中实现了 Google Analytics,测试开始失败。当我在测试期间膨胀 View 时,似乎会发生故障。这是堆栈跟踪:

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 之后,我的应用程序类实现了 Google Analytics。每当我用我的应用程序运行测试时,就会发生中断。我试过实现 Robolectric 的 MockApplication 对象,但没有任何改变;我必须从我的应用程序对象中删除 Google Analytics 才能让它工作。谁有任何解决方案可以让我使用 Google Analytics 运行 Robolectric?

最佳答案

如果前面的帖子不起作用,还有另一种解决方案。获取影子应用程序的句柄并将其配置为忽略分析开始 Intent 绑定(bind)。在您的测试设置中执行此操作。

@Before
public void setup() {
    ShadowApplication shadowApplication = Robolectric.shadowOf(Robolectric.application);
    shadowApplication.declareActionUnbindable("com.google.android.gms.analytics.service.START");
}

关于android - Robolectric 和 Google Analytics 测试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19731798/

相关文章:

java - Robolectric 和外部 jar

android - 运行时异常运行 robolectric 测试 IntelliJ

android - Robolectric HTTP 测试因 NoSuchMethodError : org. apache.http.impl.client.DefaultRequestDirector 失败

android - 自动完成功能无法正常工作的地方

java - 进度条 setVisibility VISIBLE

google-analytics - 谷歌分析文件跟踪

javascript - 将 Google Analytics UTM 跟踪添加到 Google 表格中的单元格

android - 某些 Bootstrap3 字形图标在 phonegap android webview 上未正确显示

java - 视频完成后调用新 Activity

google-analytics - 多个 GA 跟踪器获取彼此的变量