无法使我的任何仪器测试正常工作,为什么?

时间:2019-08-30 21:46:09

标签: android junit

我刚开始在Android Studio上进行测试,因此我的项目即将完成。我想测试添加到Firebase等中的用户,但首先我需要先让我的简单测试开始工作...我的简单测试主要是我试图检查页面是否已打开或是否是用户已登录。我下面的代码是我尝试过但无法使用的代码。我也发布了堆栈跟踪。

@RunWith(AndroidJUnit4.class)
    public class ExampleInstrumentedTest {

    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.example.seansandroid.dementiaapp", appContext.getPackageName());
    }

    @Rule
    public ActivityTestRule <SignInActivity> SignInActivityTestRule =
            new ActivityTestRule<SignInActivity>(SignInActivity.class);


    @Test
    public void testLaunchOfSignInActivityOnButtonClick() {

        Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(
                SignUpActivity.class.getCanonicalName(), null, true);

        //check that the signin button exists
        assertNotNull(onView(withId(R.id.button_sign_in)));
        //click the signin button
        onView(withId(R.id.button_sign_in)).perform(click());
        //
        Activity signInActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 30000);
        //check that signin activity exists
        assertNotNull(signInActivity);
        //test completed
        signInActivity.finish();

    }


    @Test
    public void testIfAPageIsLaunched(){

        Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(
                SignUpActivity.class.getCanonicalName(), null, true);

        //Do something, for example press a focused button
        assertNotNull(onView(withId(R.id.text_view_register)));

        onView(withId(R.id.text_view_register)).perform(click());

        assertTrue(getInstrumentation().checkMonitorHit(monitor, 1));

    }


    @Test
    public void testIfUserIsLoggedInOrNot() {

        Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(
                SignInActivity.class.getCanonicalName(), null, true);

        assertNotNull(onView(withId(R.id.button_sign_in)));

        onView(withId(R.id.button_sign_in)).perform(click());

        Activity signInActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 50000);

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        assertNotNull(signInActivity);

        if(user != null){
            System.out.println("User is Signed In");
        } else {
            System.out.println("There is no one Signed In");
        }

        signInActivity.finish();

    }




}

Testing started at 22:30 ...

08/30 22:30:07: Launching 'testIfAPageIsLaunc...()' on Nexus 5X API 28.
Running tests

$ adb shell am instrument -w -r   -e debug false -e class 'com.example.seansandroid.dementiaapp.ExampleInstrumentedTest#testIfAPageIsLaunched' com.example.seansandroid.dementiaapp.test/androidx.test.runner.AndroidJUnitRunner
Waiting for process to come online...
Connected to process 6136 on device 'emulator-5554'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/oid.dementiaap: The ClassLoaderContext is a special shared library.
I/oid.dementiaap: The ClassLoaderContext is a special shared library.
W/oid.dementiaap: ClassLoaderContext classpath size mismatch for position 0. expected=0, found=4 (PCL[] | PCL[/system/framework/android.test.runner.jar*3846605392:/system/framework/android.test.mock.jar*2077058553:/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk*531578862:/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk!classes2.dex*2270080478])
I/oid.dementiaap: The ClassLoaderContext is a special shared library.
I/oid.dementiaap: The ClassLoaderContext is a special shared library.
W/oid.dementiaap: ClassLoaderContext size mismatch. expected=1, actual=2 (PCL[] | PCL[/system/framework/android.test.runner.jar*3846605392:/system/framework/android.test.mock.jar*2077058553:/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk*531578862:/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk!classes2.dex*2270080478];PCL[/system/framework/android.test.runner.jar*3846605392:/system/framework/android.test.mock.jar*2077058553:/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk*531578862:/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk!classes2.dex*2270080478:/data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk*1430460639:/data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk!classes2.dex*3544289899])
W/oid.dementiaap: Found duplicate classes, falling back to extracting from APK : /data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk
    NOTE: This wastes RAM and hurts startup performance.
    Found duplicated class when checking oat files: 'Landroid/support/v4/graphics/drawable/IconCompatParcelizer;' in /data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk and /data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk
W/oid.dementiaap: Accessing hidden method Landroid/app/Instrumentation;->execStartActivity(Landroid/content/Context;Landroid/os/IBinder;Landroid/os/IBinder;Landroid/app/Activity;Landroid/content/Intent;ILandroid/os/Bundle;)Landroid/app/Instrumentation$ActivityResult; (light greylist, linking)
    Accessing hidden method Landroid/app/Instrumentation;->execStartActivity(Landroid/content/Context;Landroid/os/IBinder;Landroid/os/IBinder;Ljava/lang/String;Landroid/content/Intent;ILandroid/os/Bundle;)Landroid/app/Instrumentation$ActivityResult; (light greylist, linking)
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
D/FirebaseApp: com.google.firebase.iid.FirebaseInstanceId is not linked. Skipping initialization.
D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
D/FirebaseApp: com.google.android.gms.measurement.AppMeasurement is not linked. Skipping initialization.
I/FirebaseInitProvider: FirebaseApp initialization successful
I/MonitoringInstr: Instrumentation started!
I/MonitoringInstr: Setting context classloader to 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk", zip file "/data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/lib/x86, /data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/lib/x86, /system/lib]]]', Original: 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk", zip file "/data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/lib/x86, /data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/lib/x86, /system/lib]]]'
I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
    [FirebaseAuth:] Preparing to create service connection to gms implementation
I/MonitoringInstr: No JSBridge.
I/MonitoringInstr: Setting context classloader to 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk", zip file "/data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/lib/x86, /data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/lib/x86, /system/lib]]]', Original: 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/base.apk", zip file "/data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.seansandroid.dementiaapp.test-OG5IvQ4AkCH8oS5QzPjwgw==/lib/x86, /data/app/com.example.seansandroid.dementiaapp-coMhNuhl4d2MwFAxZUzwNQ==/lib/x86, /system/lib]]]'
I/UsageTrackerFacilitator: Usage tracking enabled
D/TestExecutor: Adding listener androidx.test.internal.runner.listener.LogRunListener
    Adding listener androidx.test.internal.runner.listener.InstrumentationResultPrinter
    Adding listener androidx.test.internal.runner.listener.ActivityFinisherRunListener
I/TestRunner: run started: 1 tests
I/TestRunner: started: testIfAPageIsLaunched(com.example.seansandroid.dementiaapp.ExampleInstrumentedTest)
I/MonitoringInstr: Activities that are still in CREATED to STOPPED: 0

Started running tests
W/ActivityTestRule: getActivityIntent() returned null using default: Intent(Intent.ACTION_MAIN)
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@3c404fa
D/LifecycleMonitor: Lifecycle status change: com.example.seansandroid.dementiaapp.Login.SignInActivity@25544a1 in: PRE_ON_CREATE
W/oid.dementiaap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/oid.dementiaap: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
D/LifecycleMonitor: Lifecycle status change: com.example.seansandroid.dementiaapp.Login.SignInActivity@25544a1 in: CREATED
D/LifecycleMonitor: Lifecycle status change: com.example.seansandroid.dementiaapp.Login.SignInActivity@25544a1 in: STARTED
D/LifecycleMonitor: Lifecycle status change: com.example.seansandroid.dementiaapp.Login.SignInActivity@25544a1 in: STOPPED
D/LifecycleMonitor: Lifecycle status change: com.example.seansandroid.dementiaapp.Login.SignInActivity@25544a1 in: DESTROYED
W/oid.dementiaap: Verification of org.hamcrest.Matcher org.hamcrest.Matchers.hasToString(java.lang.String) took 108.349ms
D/EventInjectionStrategy: Creating injection strategy with input manager.
W/oid.dementiaap: Accessing hidden method Landroid/hardware/input/InputManager;->getInstance()Landroid/hardware/input/InputManager; (light greylist, reflection)
W/oid.dementiaap: Accessing hidden method Landroid/hardware/input/InputManager;->injectInputEvent(Landroid/view/InputEvent;I)Z (light greylist, reflection)
    Accessing hidden field Landroid/hardware/input/InputManager;->INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH:I (light greylist, reflection)
W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@d6b32d4
W/oid.dementiaap: Accessing hidden method Landroid/view/ViewConfiguration;->getDoubleTapMinTime()I (light greylist, reflection)
W/oid.dementiaap: Accessing hidden method Landroid/os/MessageQueue;->next()Landroid/os/Message; (light greylist, reflection)
W/oid.dementiaap: Accessing hidden field Landroid/os/MessageQueue;->mMessages:Landroid/os/Message; (light greylist, reflection)
    Accessing hidden method Landroid/os/Message;->recycleUnchecked()V (light greylist, reflection)
W/RootViewPicker: No activities found - waiting: 10ms for one to appear.
W/RootViewPicker: No activities found - waiting: 50ms for one to appear.
W/RootViewPicker: No activities found - waiting: 150ms for one to appear.
W/oid.dementiaap: Unsupported class loader
W/oid.dementiaap: Skipping duplicate class check due to unsupported classloader
I/DynamiteModule: Considering local module com.google.android.gms.flags:3 and remote module com.google.android.gms.flags:3
    Selected local version of com.google.android.gms.flags
W/RootViewPicker: No activities found - waiting: 250ms for one to appear.
W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@d6b32d4
W/oid.dementiaap: Accessing hidden field Landroid/app/Activity;->mResultCode:I (light greylist, reflection)
    Accessing hidden field Landroid/app/Activity;->mResultData:Landroid/content/Intent; (light greylist, reflection)
E/TestRunner: failed: testIfAPageIsLaunched(com.example.seansandroid.dementiaapp.ExampleInstrumentedTest)
    ----- begin exception -----
E/TestRunner: java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
        at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:176)
        at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:88)
        at androidx.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
        at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.provideRootView(ViewInteractionModule_ProvideRootViewFactory.java:37)
        at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:27)
        at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
        at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:63)
        at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:223)
        at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:65)
        at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:158)
        at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:155)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    ----- end exception -----

java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:176)
at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:88)
at androidx.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.provideRootView(ViewInteractionModule_ProvideRootViewFactory.java:37)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:27)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:63)
at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:223)
at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:65)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:158)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:155)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I/TestRunner: finished: testIfAPageIsLaunched(com.example.seansandroid.dementiaapp.ExampleInstrumentedTest)
I/MonitoringInstr: Activities that are still in CREATED to STOPPED: 0
I/TestRunner: run finished: 1 tests, 1 failed, 0 ignored
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/MonitoringInstr: waitForActivitiesToComplete() took: 0ms

测试完成。

我知道它问我是否启动了一项活动,但是我的click()方法不是这样做的吗?谢谢您的帮助。

0 个答案:

没有答案