我是android应用程序测试的初学者。我尝试使用Espresso测试一个简单的hello world应用程序。它显示了Java没有找到活动的异常。
我的测试类
import android.test.ActivityInstrumentationTestCase2;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
/**
* Created by Ruthwik on 26-Aug-16.
*/
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
getActivity();
}
public void demoTest(){
onView(withId(R.id.helostr)).check(matches(withText("Hello World!")));
}
}
,我的Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.example.ruthwik.demotest"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'
testCompile 'junit:junit:4.12'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
}
android {
packagingOptions {
exclude 'LICENSE.txt'
}
}
我不知道如何解决这个问题。
这是我的logcat
08-26 02:43:17.161 7005-7005/? D/AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-26 02:43:17.162 7005-7005/? D/AndroidRuntime: CheckJNI is OFF
08-26 02:43:17.164 7005-7005/? W/linker: libhoudini.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
[ 08-26 02:43:17.171 7005: 7005 W/ ]
Failed to bind-mount /system/lib/x86/cpuinfo as /proc/cpuinfo: No such file or directory
08-26 02:43:17.186 7005-7005/? E/memtrack: Couldn't load memtrack module (No such file or directory)
08-26 02:43:17.186 7005-7005/? E/android.os.Debug: failed to load memtrack module: -2
08-26 02:43:17.201 7005-7005/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
--------- beginning of system
08-26 02:43:17.203 777-794/system_process I/ActivityManager: Force stopping com.example.ruthwik.demotest appid=10106 user=0: from pid 7005
08-26 02:43:17.203 7005-7005/? D/AndroidRuntime: Shutting down VM
08-26 02:43:17.205 7005-7011/? I/art: Debugger is no longer active
08-26 02:43:17.550 7025-7025/? D/AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-26 02:43:17.551 7025-7025/? D/AndroidRuntime: CheckJNI is OFF
08-26 02:43:17.555 7025-7025/? W/linker: libhoudini.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
[ 08-26 02:43:17.561 7025: 7025 W/ ]
Failed to bind-mount /system/lib/x86/cpuinfo as /proc/cpuinfo: No such file or directory
08-26 02:43:17.578 7025-7025/? E/memtrack: Couldn't load memtrack module (No such file or directory)
08-26 02:43:17.578 7025-7025/? E/android.os.Debug: failed to load memtrack module: -2
08-26 02:43:17.595 7025-7025/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
08-26 02:43:17.597 777-1125/system_process I/ActivityManager: Force stopping com.example.ruthwik.demotest.test appid=10107 user=0: from pid 7025
08-26 02:43:17.598 7025-7025/? D/AndroidRuntime: Shutting down VM
08-26 02:43:17.603 7025-7031/? I/art: Debugger is no longer active
08-26 02:43:17.889 7040-7040/? D/AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-26 02:43:17.890 7040-7040/? D/AndroidRuntime: CheckJNI is OFF
08-26 02:43:17.893 7040-7040/? W/linker: libhoudini.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
[ 08-26 02:43:17.900 7040: 7040 W/ ]
Failed to bind-mount /system/lib/x86/cpuinfo as /proc/cpuinfo: No such file or directory
08-26 02:43:17.924 7040-7040/? E/memtrack: Couldn't load memtrack module (No such file or directory)
08-26 02:43:17.924 7040-7040/? E/android.os.Debug: failed to load memtrack module: -2
08-26 02:43:17.948 7040-7040/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
08-26 02:43:17.952 777-793/system_process I/ActivityManager: Force stopping com.example.ruthwik.demotest appid=10106 user=0: start instr
08-26 02:43:17.969 7054-7054/? I/art: Late-enabling -Xcheck:jni
08-26 02:43:17.972 777-793/system_process I/ActivityManager: Start proc com.example.ruthwik.demotest for added application com.example.ruthwik.demotest: pid=7054 uid=10106 gids={50106, 9997} abi=x86
08-26 02:43:17.986 7054-7063/? E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
08-26 02:43:17.986 7054-7063/? I/art: Debugger is no longer active
08-26 02:43:17.996 7054-7054/? I/MonitoringInstrumentation: Instrumentation Started!
08-26 02:43:17.996 7054-7054/? I/MonitoringInstrumentation: Setting context classloader to 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.example.ruthwik.demotest.test-2/base.apk", zip file "/data/app/com.example.ruthwik.demotest-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]]', Original: 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.example.ruthwik.demotest.test-2/base.apk", zip file "/data/app/com.example.ruthwik.demotest-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]]'
08-26 02:43:17.999 7054-7054/? I/MonitoringInstrumentation: No JSBridge.
08-26 02:43:18.002 7054-7072/? D/InfraTrack: Tracking disabled due to lack of internet permissions
08-26 02:43:18.002 7054-7072/? I/UsageTrackerFacilitator: Usage tracking disabled
08-26 02:43:18.007 7054-7072/? D/TestExecutor: Adding listener android.support.test.internal.runner.listener.LogRunListener
08-26 02:43:18.007 7054-7072/? D/TestExecutor: Adding listener android.support.test.internal.runner.listener.InstrumentationResultPrinter
08-26 02:43:18.007 7054-7072/? D/TestExecutor: Adding listener android.support.test.internal.runner.listener.ActivityFinisherRunListener
08-26 02:43:18.007 7054-7072/? I/TestRunner: run started: 1 tests
08-26 02:43:18.008 7054-7072/? I/TestRunner: started: warning(junit.framework.TestSuite$1)
08-26 02:43:18.009 7054-7054/? I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
08-26 02:43:18.010 7054-7072/? I/TestRunner: failed: warning(junit.framework.TestSuite$1)
08-26 02:43:18.010 7054-7072/? I/TestRunner: ----- begin exception -----
08-26 02:43:18.010 7054-7072/? I/TestRunner: junit.framework.AssertionFailedError: No tests found in com.example.ruthwik.demotest.MainActivityTest
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:103)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
08-26 02:43:18.010 7054-7072/? I/TestRunner: ----- end exception -----
08-26 02:43:18.010 7054-7072/? I/TestRunner: finished: warning(junit.framework.TestSuite$1)
08-26 02:43:18.014 7054-7054/? I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
08-26 02:43:18.014 7054-7072/? I/TestRunner: run finished: 1 tests, 1 failed, 0 ignored
08-26 02:43:18.020 7054-7072/? I/MonitoringInstrumentation: waitForActivitiesToComplete() took: 0ms
08-26 02:43:18.020 7054-7054/? I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
08-26 02:43:18.024 7040-7040/? D/AndroidRuntime: Shutting down VM
08-26 02:43:18.027 7040-7046/? I/art: Debugger is no longer active
08-26 02:43:18.259 777-1125/system_process I/ActivityManager: Force stopping com.example.ruthwik.demotest appid=10106 user=0: finished inst
08-26 02:43:18.259 777-1125/system_process I/ActivityManager: Killing 7054:com.example.ruthwik.demotest/u0a106 (adj 0): stop com.example.ruthwik.demotest
08-26 02:43:18.289 777-2683/system_process W/ActivityManager: Spurious death for ProcessRecord{34e9075 7054:com.example.ruthwik.demotest/u0a106}, curProc for 7054: null
08-26 02:43:18.320 7073-7073/? D/AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-26 02:43:18.321 7073-7073/? D/AndroidRuntime: CheckJNI is OFF
08-26 02:43:18.324 7073-7073/? W/linker: libhoudini.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
[ 08-26 02:43:18.330 7073: 7073 W/ ]
Failed to bind-mount /system/lib/x86/cpuinfo as /proc/cpuinfo: No such file or directory
08-26 02:43:18.346 7073-7073/? E/memtrack: Couldn't load memtrack module (No such file or directory)
08-26 02:43:18.346 7073-7073/? E/android.os.Debug: failed to load memtrack module: -2
08-26 02:43:18.357 7073-7073/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
08-26 02:43:18.360 777-1294/system_process I/ActivityManager: Force stopping com.example.ruthwik.demotest appid=10106 user=0: from pid 7073
08-26 02:43:18.360 7073-7073/? D/AndroidRuntime: Shutting down VM
答案 0 :(得分:1)
将@RunWith(AndroidJUnit4.class)放在类定义之上。 把@Before放在setUp声明之上 将@Test放在测试用例之上(虽然是可选的)