我正在使用EclEmma覆盖工具来测试我的代码覆盖率.但每当我运行我的测试用例时,它会给我以下错误.你能告诉我怎样才能解决这个错误.

我正在尝试在一个扩展SherlockActivity的Activity上运行测试.我阅读了有关使用ActionBarSherlock测试活动的解决方案的所有内容,并尝试了以下https://github.com/passy/absshadow-sample
这就是我目前正在做的事情:
定制测试运行器:
public class CustomTestRunner extends RobolectricTestRunner {
private static final int SDK_INT = Build.VERSION.SDK_INT;
public CustomTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
addClassOrPackageToInstrument("com.actionbarsherlock.app.SherlockActivity");
}
@Override
protected void bindShadowClasses() {
super.bindShadowClasses();
Robolectric.bindShadowClass(ShadowSherlockActivity.class);
}
@Override
public void beforeTest(final Method method) {
final int targetSdkVersion = robolectricConfig.getSdkVersion();
setStaticValue(Build.VERSION.class, "SDK_INT", targetSdkVersion);
}
@Override
public void afterTest(final Method method) {
resetStaticState();
}
@Override
public void resetStaticState() {
setStaticValue(Build.VERSION.class, "SDK_INT", SDK_INT);
}
}
Run Code Online (Sandbox Code Playgroud)
影子SherlockActivity:
@Implements(SherlockActivity.class)
public class ShadowSherlockActivity extends ShadowActivity {
@Implementation
public void …Run Code Online (Sandbox Code Playgroud) 我有一个Android InputMethodService的基本实现,我正在尝试编写单元测试.我的应用程序没有任何Activites,只是InputMethodService的实现.
到目前为止,我有一个ServiceTestCase的基本实现,它运行良好:
SoftKeyboardTest.java
public class SoftKeyboardTest extends ServiceTestCase<SoftKeyboard> {
@Override
protected void setUp() throws Exception {
super.setUp();
bindService(new Intent(this.getContext(), SoftKeyboard.class));
}
public void testShowKeyboard() {
this.getService().ShowKeyboard();
assertTrue(this.getService().GetKeyboardIsVisible());
}
public void testInsertText() {
String text = "Hello, world";
this.getService().InsertText(text);
assertEquals(this.getService().ReadText(text.length()), text);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想测试一些使用getCurrentInputConnection()将文本插入当前焦点的EditText的功能:
SoftKeyboard.java
public void InsertText(String sentence) {
getCurrentInputConnection().commitText(sentence, 1);
}
public void ReadText(int chars) {
getCurrentInputConnection().getTextBeforeCursor(chars, 0);
}
Run Code Online (Sandbox Code Playgroud)
显然在这种情况下我得到一个NullPointerException,因为实际上没有任何聚焦的EditText.
如何让我的测试应用程序启动我的服务,以某种方式专注于EditText,然后启动我的测试用例,以便我可以正确测试我的服务方法?
当我对onCreate()方法检查某个特定服务是否正在运行的活动运行测试时,我得到一个NPE.在测试的设置方法中,我调用:
ActivityController<MyActivity> ac = Robolectric.buildActivity(MyActivity.class).create();
mActivity = ac.get();
Run Code Online (Sandbox Code Playgroud)
在活动的onCreate()方法中,我调用isServiceRunning()
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
if(manager != null) {
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (service.service != null && "MyService".equals(service.service.getClassName())) {
return true;
}
}
}
return false;
Run Code Online (Sandbox Code Playgroud)
执行此操作时,NPE失败:
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
Run Code Online (Sandbox Code Playgroud)
manager不是null,但NPE在android库中的ActivityManager.getRunningServices(int)中抛出.
如果我需要这种方法通过,我该怎么办?我正在使用Roboelectric 2.2-20130612.001729-6-jar-with-dependencies.jar但我现在看到问题,即使是最新版本.
我使用Android SDK版本19,但我现在切换到18,我仍然看到问题.
有任何想法吗?
堆栈跟踪:
java.lang.NullPointerException
at android.app.ActivityManager.getRunningServices(ActivityManager.java:1106)
at com.example.MyActivity.isServiceRunning(MyActivity.java:151)
at com.example.MyActivity.onCreate(MyActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:116)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:257)
at org.robolectric.util.ActivityController.create(ActivityController.java:111)
at org.robolectric.util.ActivityController.create(ActivityController.java:123)
at com.example.test.MyActivityTest.setUp(MyActivityTest.java:44)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) …Run Code Online (Sandbox Code Playgroud) Method annotated with @Test inside class extending junit3 testcase使用ActivityInstrumentationTestCase2Espresso 2.0附带的新类时,我收到了一个奇怪的警告.
我的课程看起来就像谷歌提供的那样:
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static org.hamcrest.Matchers.notNullValue;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MyCoolActivityTests extends ActivityInstrumentationTestCase2<MyCoolActivity> {
private MyCoolActivity mActivity;
public MyCoolActivityTests() {
super(MyCoolActivity.class);
}
@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity = getActivity();
}
@Test
public void checkPreconditions() {
assertThat(mActivity, notNullValue());
// Check that Instrumentation was correctly injected in …Run Code Online (Sandbox Code Playgroud) 使用带有Robolectic 3.0-rc3的库时获取ResourceNotFoundException.资源在build.gradle中使用compile'net.danlew:android.joda:2.8.0'声明.具体来说,这是Joda-Time的Android端口.
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f0501da
at org.robolectric.shadows.ShadowResources.checkResName(ShadowResources.java:343)
at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:333)
at org.robolectric.shadows.ShadowResources.openRawResource(ShadowResources.java:382)
at android.content.res.Resources.openRawResource(Resources.java)
at net.danlew.android.joda.ResourceZoneInfoProvider.openResource(ResourceZoneInfoProvider.java:120)
at net.danlew.android.joda.ResourceZoneInfoProvider.<init>(ResourceZoneInfoProvider.java:39)
Run Code Online (Sandbox Code Playgroud)
申请类:
@Override
public void onCreate() {
super.onCreate();
JodaTime.init(this);
}
Run Code Online (Sandbox Code Playgroud)
我的考试班:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,
sdk = 21)
public class MyTest {
@Before
public void setup() {
}
@Test
public void myTest() {
//Test my stuff
}
Run Code Online (Sandbox Code Playgroud)
}
我是Roboelectric测试的新手.我试图按照这篇 文章来测试片段.我使用以下作为依赖
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile "org.robolectric:robolectric:3.0"
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.google.code.gson:gson:2.3'
compile 'com.android.support:support-v4:22.1.0'
compile 'com.google.android.gms:play-services:7.5.0'
Run Code Online (Sandbox Code Playgroud)
}
但是这个导入给了我编译问题,我无法解决符号SupportFragmentTestUtil的问题.请指导我,我错过了什么?
org.robolectric.shadows.support.v4.SupportFragmentTestUtil
Run Code Online (Sandbox Code Playgroud) 任何人都可以解释什么是android中的测试分片意味着完成?如果有人可以分享任何教程将是非常有帮助的.
单词shard意味着整体的一小部分.如何基于一个数字执行分片,以及在什么基础上我应该指定shardIndex?
在开发人员文档中定义.
测试分片
测试运行器支持将单个测试套件拆分为多个分片,因此您可以在同一个Instrumentation实例下轻松地将属于同一分片的测试作为一个组一起运行.每个分片都由索引号标识.运行测试时,使用-e numShards选项指定要创建的单独分片数,使用-e shardIndex选项指定要运行的分片.
例如,要将测试套件拆分为10个分片并仅运行在第二个分片中分组的测试,请使用以下命令:
adb shell am instrument -w -e numShards 10 -e shardIndex 2
如何IntentService在不使用弃用的情况下在android中测试ServiceTestCase?
从ServiceTestCase的文档:
此类在API级别24中已弃用.请改用ServiceTestRule.应使用Android测试支持库编写新测试.
但ServiceTestRule文档说它不支持IntentServices:
注意:此规则不支持IntentService,因为当onHandleIntent(android.content.Intent)完成所有未完成的命令时,它会自动销毁.因此无法保证及时建立成功的连接.
我该如何测试IntentService呢?
android unit-testing intentservice android-testing android-espresso
我正在尝试使用Robolectric来测试我的活动,但是当我尝试使用Robolectric设置Activity时,我总是得到一个NPE.我遵循了robolectric.org网站上的指南.
这是我的代码:
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP, manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
@Ignore
public class MainActivityUnitTest {
private MainActivity activity;
@Before
public void setup() {
activity = Robolectric.setupActivity(MainActivity.class);
}
@Test
public void dummyTest() {
assertTrue(true);
}
}
Run Code Online (Sandbox Code Playgroud)
其他一些信息:我在项目中使用Dagger2作为DI.我得到的例外:
java.lang.NullPointerException
at org.robolectric.internal.ShadowExtractor.extract(ShadowExtractor.java:5)
at org.robolectric.Shadows.shadowOf(Shadows.java:1165)
at org.robolectric.shadows.CoreShadowsAdapter.getMainLooper(CoreShadowsAdapter.java:42)
at org.robolectric.util.ComponentController.<init>(ComponentController.java:31)
at org.robolectric.util.ActivityController.<init>(ActivityController.java:35)
at org.robolectric.util.ActivityController.of(ActivityController.java:27)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
at com.me.myapplication.MainActivityUnitTest.setup(MainActivityUnitTest.java:32)
Run Code Online (Sandbox Code Playgroud) android ×10
android-testing ×10
java ×4
robolectric ×4
jodatime ×1
junit ×1
testing ×1
unit-testing ×1