Robolectric给出了一个例外

时间:2016-07-06 06:50:41

标签: android junit robolectric

我使用Robolectric工具为我的应用程序创建了一个简单的junit测试。

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertNotNull;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,sdk = 21)
public class MainActivityTest {
    MainActivity activity;
    @Before
    public void setUp(){
      activity= Robolectric.setupActivity(MainActivity.class);
     }

    @Test
    public void shouldNotBeNull(){
     assertNotNull(activity);
    }
 }

基本上这个测试应该通过,因为活动不是null。但是我得到了这个奇怪的例外。

java.lang.IllegalArgumentException: path must be convex

at android.graphics.Outline.setConvexPath(Outline.java:216)
at android.graphics.drawable.GradientDrawable.getOutline(GradientDrawable.java:1441)
at android.view.ViewOutlineProvider$1.getOutline(ViewOutlineProvider.java:38)
at android.view.View.rebuildOutline(View.java:11116)
at android.view.View.onAttachedToWindow(View.java:12960)
at android.view.ViewGroup.onAttachedToWindow(ViewGroup.java:3815)
at android.view.View.dispatchAttachedToWindow(View.java:13406)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2707)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2714)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1292)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:30)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:95)
at org.robolectric.util.Scheduler$ScheduledRunnable.run(Scheduler.java:269)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:179)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:160)
at org.robolectric.util.Scheduler.advanceBy(Scheduler.java:143)
at org.robolectric.util.Scheduler.unPause(Scheduler.java:54)
at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:266)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:306)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ActivityController.visible(ActivityController.java:173)
at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
at team.football.ael.MainActivityTest.shouldNotBeNull(MainActivityTest.java:24)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
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.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

任何想法发生了什么?

谢谢,

西奥。

1 个答案:

答案 0 :(得分:2)

这是Robolectric 3.0中的一个已知错误(请参阅此处的问题https://github.com/robolectric/robolectric/issues/1810),已在版本3.1中修复,方法是将setConvexPath()添加到Outline类的阴影中。