Android测试项目无法在Android工作室中运行

时间:2017-09-11 10:09:43

标签: android android-studio junit

我尝试在Android studio 2.3上运行这个项目

public class ImplicitTest extends ActivityInstrumentationTestCase2<ActivityLoaderActivity> {
     private Solo solo;

     public ImplicitTest() {
         super(ActivityLoaderActivity.class);
     }

     public void setUp() throws Exception {
         solo = new Solo(getInstrumentation(),getActivity());
     }

     @Override
     public void tearDown() throws Exception {
         solo.finishOpenedActivities();
     }

     // Executes the ImplicitTest
     public void testRun() {

         int delay = 2000;

         // =================== Section One =====================

         // Wait for activity: 'course.labs.intentslab.ActivityLoaderActivity'
         assertTrue(
            "ImplicitTest:" +
            "Section One:" +
            "ActivityLoaderActivity did not load correctly", solo.waitForActivity(ActivityLoaderActivity.class,delay));

         solo.sleep(delay);

         // Click on Implicit Activation Button
         solo.clickOnView(solo
            .getView(R.id.implicit_activation_button));


         // Wait for activity: 'com.android.internal.app.ChooserActivity'
         assertTrue(
            "ImplicitTest:" +
            "Section One:" +
            "ChooserActivity was not launched correctly",
            solo.waitForActivity("ChooserActivity",delay));

         solo.sleep(delay);

         assertTrue(
            "ImplicitTest:" +
            "Section One:" +
            "MyBrowser was not found",
            solo.searchText("MyBrowser", true));
     }
 }

但是我得到了这个例外:

    java.lang.NullPointerException
    at com.robotium.solo.ActivityUtils.setupActivityMonitor(ActivityUtils.java:107)
    at com.robotium.solo.ActivityUtils.<init>(ActivityUtils.java:59)
    at com.robotium.solo.Solo.<init>(Solo.java:137)
    at com.robotium.solo.Solo.<init>(Solo.java:96)
    at course.labs.intentslab.ImplicitTest.setUp(ImplicitTest.java:18)
    at junit.framework.TestCase.runBare(TestCase.java:139)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

java.lang.ExceptionInInitializerError
    at com.robotium.solo.Solo.<init>(Solo.java:138)
    at com.robotium.solo.Solo.<init>(Solo.java:96)
    at course.labs.intentslab.ImplicitTest.setUp(ImplicitTest.java:18)
    at junit.framework.TestCase.runBare(TestCase.java:139)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: android.view.WindowManagerImpl
    at com.robotium.solo.ViewFetcher.<clinit>(ViewFetcher.java:441)
    ... 21 more
Caused by: java.lang.ClassNotFoundException: android.view.WindowManagerImpl
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.robotium.solo.ViewFetcher.<clinit>(ViewFetcher.java:438)
    ... 21 more

另外,我在build.gradle中添加了这一行,但它不起作用:

androidTestCompile 'com.jayway.android.robotium:robotium:5.3.1'                
testCompile 'junit:junit:4.12'                                               
compile 'com.jayway.android.robotium:robotium-solo:5.3.1'

0 个答案:

没有答案