问题描述
我很高兴听到机器人工作室做了一些更新,使单元测试简单的机器人工作室做的,所以我一直在试图建立roboelectric但我不能让过去这个错误,下面的方法的过程中发生。
I was excited to hear that android studio had made some updates to make unit testing simple to do in android studio so I have been trying to set up roboelectric but I cant get past this error that occurs during the following method.
MainActivity = Robolectric.buildActivity(MainActivity.class).create().get();
与以下堆栈跟踪
产生的原因:了java.lang.RuntimeException:java.lang.NoSuchMethodException:java.lang.Object.attach(android.content.Context,android.app.ActivityThread,android.app.Instrumentation,android.os.IBinder, INT,android.app.Application,android.content.Intent,android.content.pm.ActivityInfo,java.lang.CharSequence中,android.app.Activity,java.lang.String中,android.app.Activity $ NonConfigurationInstances,机器人。 content.res.Configuration)
在org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:248)
在org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:138)
... 36多个
这是为什么发生,我能做些什么让过去呢?
Why this is occurring and what I can do to get past it?
推荐答案
如果你写单元测试
,那么你肯定可以使用间谍
和模拟
从的Mockito库小号
If you writing unit tests
, then you definitely can use spy
's and mock
's from Mockito library.
-
添加库的Mockito依赖性:
Add Mockito library dependency:
testCompile'org.mockito:的Mockito核心:2.0.3-β
创建间谍活动
在您的测试方法:
Create spy Activity
in your test method:
活动活动=间谍(Activity.class);
您可以阅读更多有关并的更好地了解他们的目的。
You can read more about spy and mock objects for better understanding of their purposes.
这篇关于Robolectric 3.0 java.lang.NoSuchMethodException上创建活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!