风味A的Robolectric测试案例通过而风味B失败

时间:2015-11-11 23:22:06

标签: android android-gradle robolectric

我的应用程序包含两种风格的A和B.当我运行debug时,所有风味A的测试都会通过而风味B会失败。我还有两种构建类型release\my-project ---- \app -------- \src ------------ \debug ------------ \flavor_A ------------ \main ------------ \flavor_B ------------ \test ---------------- \java -------------------- \ com.my.main.package ------------------------ \ My_test_classes.java -------------------- \ TestApplication.java

我的架构是这样的:

TestApplication.java

Application会覆盖已在人@RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21, packageName = "com.my.main.package") public class WelcomeActivityTest { Activity activity; Button btnSignUp; TextView btnSkip; @Before public void setUp() { activity = Robolectric.buildActivity(WelcomeActivity.class).create().get(); // << line 32 btnSignUp = (Button) activity.findViewById(R.id.dialog_welcome_sign_up); btnSkip = (TextView) activity.findViewById(R.id.dialog_welcome_next_time); } @Test public void shouldNotBeNull() throws Exception { assertNotNull(activity); } @Test public void shouldHaveSignUp_SkipButtons() throws Exception { assertViewIsVisible(btnSignUp); assertViewIsVisible(btnSkip); } public static void assertViewIsVisible(View view) { assertNotNull(view); assertThat(view.getVisibility(), equalTo(View.VISIBLE)); } } as explained here中定义的某些方法。

我的测试课很简单。

java.util.ConcurrentModificationException
    at java.util.ArrayList.sort(ArrayList.java:1456)
    at java.util.Collections.sort(Collections.java:141)
    at org.robolectric.res.ResBundle.put(ResBundle.java:32)
    at org.robolectric.res.ResBunch.put(ResBunch.java:12)
    at org.robolectric.res.ValueResourceLoader.processNode(ValueResourceLoader.java:36)
    at org.robolectric.res.XpathResourceXmlLoader.processResourceXml(XpathResourceXmlLoader.java:22)
    at org.robolectric.res.ValueResourceLoader.processResourceXml(ValueResourceLoader.java:19)
    at org.robolectric.res.XmlLoader.processResourceXml(XmlLoader.java:46)
    at org.robolectric.res.DocumentLoader.loadResourceXmlFile(DocumentLoader.java:47)
    at org.robolectric.res.DocumentLoader.loadFile(DocumentLoader.java:40)
    at org.robolectric.res.DocumentLoader.load(DocumentLoader.java:30)
    at org.robolectric.res.PackageResourceLoader.loadEverything(PackageResourceLoader.java:32)
    at org.robolectric.res.PackageResourceLoader.doInitialize(PackageResourceLoader.java:19)
    at org.robolectric.res.XResourceLoader.initialize(XResourceLoader.java:29)
    at org.robolectric.res.XResourceLoader.getValue(XResourceLoader.java:52)
    at org.robolectric.res.PackageResourceLoader.getValue(PackageResourceLoader.java:5)
    at org.robolectric.res.RoutingResourceLoader.getValue(RoutingResourceLoader.java:31)
    at org.robolectric.shadows.ShadowAssetManager.resolveStyle(ShadowAssetManager.java:343)
    at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:286)
    at org.robolectric.shadows.ShadowResources$ShadowTheme.applyStyle(ShadowResources.java:461)
    at android.content.res.Resources$Theme.applyStyle(Resources.java)
    at android.app.ContextImpl.getTheme(ContextImpl.java:838)
    at android.content.ContextWrapper.getTheme(ContextWrapper.java:121)
    at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:135)
    at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:85)
    at org.robolectric.shadows.ShadowActivity.setThemeFromManifest(ShadowActivity.java:85)
    at org.robolectric.shadows.CoreShadowsAdapter$1.setThemeFromManifest(CoreShadowsAdapter.java:35)
    at org.robolectric.util.ActivityController.attach(ActivityController.java:58)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:121)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
    at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
    at org.robolectric.util.ActivityController.create(ActivityController.java:118)
    at org.robolectric.util.ActivityController.create(ActivityController.java:129)
    at com.passenger.WelcomeActivityTest.setUp(WelcomeActivityTest.java:32)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


Process finished with exit code 255

最后,这是我得到的日志:

{{1}}

令我感到困惑的是我对口味的测试没有不同,所以为什么在flavorB失败的情况下测试flavorA会通过。

2 个答案:

答案 0 :(得分:1)

似乎我解决了我的问题。

我的问题是缺乏对构建变体的深刻理解。我想如果我在TestApplication.java文件夹下有test,那么其内容适用于debugrelease,但它只适用于release

我将其名称更改为TestDebugApplication并复制到debug文件夹并修复了问题。

答案 1 :(得分:0)

这是多个线程访问资源的问题,导致Robolectric尝试同时加载资源。

我在https://github.com/robolectric/robolectric/pull/2353修正了此问题。如果您使用最新快照,它应该可以使用,或者您可以等待3.1版本。

作为一种临时措施,如果您只是强制加载Application#onCreate()中的资源或在创建任何其他线程之前运行的其他方法,它将确保在多个线程尝试使用之前已加载资源它们。

我怀疑你所做的一切都会导致这种情况发生,这会掩盖错误,因为这与构建变体无关。