Android IllegalStateException未注册任何检测!必须在注册检测下运行
我正在努力让这款浓缩咖啡在安卓系统上进行功能测试。我的应用程序是一个Multdex应用程序,因此我遵循以下链接中的说明:() 我已经像这样配置了build.gradle:Android IllegalStateException未注册任何检测!必须在注册检测下运行,android,android-studio,functional-testing,android-espresso,Android,Android Studio,Functional Testing,Android Espresso,我正在努力让这款浓缩咖啡在安卓系统上进行功能测试。我的应用程序是一个Multdex应用程序,因此我遵循以下链接中的说明:() 我已经像这样配置了build.gradle: apply plugin: 'com.android.application' apply plugin: 'android-apt' def AAVersion = '3.3.2' buildscript { repositories { mavenCentral() } depen
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.3.2'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'br.com.foo'
}
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "br.com.foo"
minSdkVersion 15
targetSdkVersion 21
android.enforceUniquePackageName = false
multiDexEnabled true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
testApplicationId "br.com.foo.test"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
dexOptions {
preDexLibraries = false
javaMaxHeapSize "4g"
}
packagingOptions {
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
compile 'com.github.PhilJay:MPAndroidChart:v2.1.0'
compile 'com.github.daimajia:AndroidViewAnimations:v1.1.3'
compile 'com.makeramen:roundedimageview:2.1.1'
compile 'com.github.ACRA:acra:acra-4.6.2'
compile 'com.github.rahatarmanahmed:circularprogressview:2.3.2'
compile 'com.github.jhy:jsoup:jsoup-1.8.3'
compile project(':cacscore')
compile project(':croplib')
compile project(':zxingandroid')
compile project(':paho')
compile project(':urlImageViewHelper')
compile project(':facebookSDK')
compile project(':googleDateTimePickers')
compile 'com.android.support:support-v4:22.0.0'
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
compile 'com.android.support:multidex:1.0.0'
compile 'org.apache.commons:commons-lang3:3.4'
compile files('libs/httpmime-4.1.jar')
compile files('libs/jackson-all-1.9.11.jar')
compile files('libs/jaxrs-api-2.2.1.GA.jar')
compile files('libs/spring-android-core-1.0.1.RELEASE.jar')
compile files('libs/spring-android-rest-template-1.0.1.RELEASE.jar')
compile 'com.melnykov:floatingactionbutton:1.2.0'
compile files('libs/trace.jar')
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.2'
testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.2'
testCompile 'org.powermock:powermock-module-junit4:1.6.2'
androidTestCompile 'junit:junit:4.12'
// androidTestCompile('com.android.support.test:testing-support-lib:0.1') {
// exclude group: 'junit' // junit:junit-dep conflicts with junit:unit
// }
androidTestCompile 'com.android.support.test:runner:0.4'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.4'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Set this dependency to build and run UI Automator tests
// androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') {
exclude group: 'com.android.support', module: 'multidex'
}
}
我从Android Studio配置了运行配置文件,将testInstrumentationRunner类设置为com.Android.test.runner.MultiDexTestRunner
当我执行命令“adb shell pm list instrumentation”时,我有以下输出:
instrumentation:br.com.doe.test/com.android.test.runner.MultiDexTestRunner (target=br.com.doe)
instrumentation:br.com.foo.test/com.android.test.runner.MultiDexTestRunner (target=br.com.foo)
instrumentation:br.com.foo/com.android.test.runner.MultiDexTestRunner (target=br.com.foo)
但当我尝试执行测试时,仍然会收到此输出:
Testing started at 17:23 ...
Installing br.com.foo
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/br.com.foo"
pkg: /data/local/tmp/br.com.foo
Success
Uploading file
local path: F:\Android\CACS\cacs\cacs\build\outputs\apk\cacs-debug-androidTest-unaligned.apk
remote path: /data/local/tmp/br.com.foo.test
Installing br.com.foo.test
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/br.com.foo.test"
pkg: /data/local/tmp/br.com.foo.test
Success
Running tests
Test running started
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
at br.com.bagger.inicio.MapaEnderecoTest.setUp(MapaEnderecoTest.java:35)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Finish
这是我尝试运行的唯一测试:
package br.com.doe.inicio;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import br.com.foo.R;
@RunWith(AndroidJUnit4.class)
public class MapaEnderecoTest extends ActivityInstrumentationTestCase2<MapaEndereco>
{
private MapaEndereco mActivity;
public MapaEnderecoTest()
{
super(MapaEndereco.class);
}
@Override
@Before
protected void setUp() throws Exception
{
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity = getActivity();
}
@Test
public void testeAbertura()
{
Espresso.onView(ViewMatchers.withId(R.id.map)).perform(ViewActions.click());
}
}
package br.com.doe.inicio;
导入android.support.test.InstrumentationRegistry;
导入android.support.test.espresso.espresso;
导入android.support.test.espresso.action.ViewActions;
导入android.support.test.espresso.matcher.ViewMatchers;
导入android.support.test.runner.AndroidJUnit4;
导入android.test.ActivityInstrumentationTestCase2;
导入org.junit.Before;
导入org.junit.Test;
导入org.junit.runner.RunWith;
进口br.com.foo.R;
@RunWith(AndroidJUnit4.class)
公共类MapaenDecoreTest扩展了ActivityInstrumentationTestCase2
{
私人MapaEndereco活动;
公共地图加密测试()
{
超级(MapaEndereco.class);
}
@凌驾
@以前
受保护的void setUp()引发异常
{
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity=getActivity();
}
@试验
公共图书馆
{
Espresso.onView(ViewMatchers.withId(R.id.map)).perform(ViewActions.click());
}
}
错误发生在setUp方法的super.setUp()调用之后
我已将检测注册到AndroidManifest文件中:
<instrumentation
android:label="Multdex"
android:name="com.android.test.runner.MultiDexTestRunner"
android:targetPackage="br.com.foo"
android:functionalTest="true"/>
我面临的问题与这里描述的不同问题在于这些渐变线:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
您需要一个MultiDexTestRunner
,但该类是从InstrumentationTestRunner
派生的。您的代码需要一个从AndroidJUnitRunner
解决方案是创建一个自定义的TestRunner
,它扩展了AndroidJUnitRunner
,并实现了多索引代码:
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.support.test.runner.AndroidJUnitRunner;
public class TestRunner extends AndroidJUnitRunner
{
@Override
public void onCreate(Bundle arguments)
{
MultiDex.install(getTargetContext());
super.onCreate(arguments);
}
}
您只需要添加一条引用自定义
testInstrumentationRunner
的渐变线。这将确保生成系统生成使用此运行程序运行测试所需的AndroidManifest.xml
属性。我也有类似的错误,并且一直在努力修复它。虽然被接受的答案帮助了OP,但它并没有解决我的问题,所以我决定在这里记录我的解决方案是好的,希望它也能帮助其他人
我的问题是我混合了AndroidJUnit4
、InstrumentationRegistry
、ApplicationProvider
和AndroidJUnitRunner
版本/包。确保他们都是同一代人。这些课程让我可以运行所有这些课程:
androidx.test.runner.AndroidJUnitRunner
androidx.test.platform.app.InstrumentationRegistry
androidx.test.ext.junit.runners.AndroidJUnit4
androidx.test.core.app.ApplicationProvider
依赖项部分中包含以下内容
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
当然是正确的
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
在我的build.gradle的defaultConfig
中,而不是:
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
我不得不使用不同的图书馆:
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
我也面临同样的问题。你找到解决方案了吗?你在使用哪个版本的android gradle插件?我在使用这个插件:classpath'com.android.tools.build:gradle:1.3.0'@RicardoBelchior@ThanasisPetsas遗憾的是,我还没有解决。我一直在考虑调试Android,看看会发生什么,你还需要两个TestInstantRunner吗?还有manifest.xml中的it声明?不,您只需要自定义TestRunner。我只是在我的版本中声明的。构建时,在Application/build/intermediates/manifest/androidTest/debug/
中生成清单文件,其中包含声明。Gradle工具集解决了这一问题。不需要创建自定义TestRunner请提供指向相同代码的链接,我们可以看到使用了TestRunner
类的位置@Kenvanhoeyland完美的答案,谢谢!,糟糕的是,谷歌文档本身并没有提供这些信息。我不建议这样做,相反,每个人都应该尽可能使用新的Android X库。我也不建议这样做,但在某些情况下,迁移并不实用。OP仍在使用支持库。尝试将这些与AndroidX的依赖性混合在一起导致了这个问题。我在Robolectric上遇到了这个问题。你也面对过吗?[Robolectric]警告:Android SDK 29需要Java 9(有Java 8)。除非明确要求,否则测试不会在SDK 29上运行。java.lang.UnsupportedOperationException:未能创建机器人沙盒:Android SDK 29需要java 9(拥有java 8)@android6p是完全不同的问题。我建议我们不要在这里讨论这个问题,而是从另一个问题开始。总的来说,也许我能帮上忙。