1

所以我在登录应用程序时遇到了一些问题。

我一直在尝试为它编写一个集成测试......

class LoginIT : AndroidIntegrationTest() {

    @get:Rule
    var loginActivityRule = activityScenarioRule<LoginActivity>()

    var done = false;

    @Test
    fun login_setsUser() {
        loginActivityRule.scenario.onActivity { loginActivity ->
            loginActivity.onLoginRequestCompleted(checkUser)
            loginActivity.usernameEditText.setText("Test001")
            loginActivity.passwordEditText.setText("0000")
            loginActivity.loginButton.performClick()
        }

        while (!done) {
        }
    }

    val checkUser = { succeeded: Boolean ->
        assertThat(succeeded).isTrue()

        mainActivityRule.scenario.onActivity { mainActivity ->
            val userManager = mainActivity.userManager.getActiveUser()
            assertThat { userManager.creds }.isInstanceOf(Valid::class.java)
            assertThat { userManager.name }.isEqualTo("Test001")
        }
        done = true
    }
}

在哪里

@RunWith(AndroidJUnit4::class)
abstract class AndroidIntegrationTest {

    @get:Rule
    var mainActivityRule = activityScenarioRule<MainActivity>()

    @get:Rule
    val serviceRule = ServiceTestRule()
}

那失败了

java.lang.NullPointerException: Cannot run onActivity since Activity has been destroyed already

checkUser体内。所以我扔了一个

assertThat { mainActivityRule.scenario.state }.isEqualTo(STARTED)

login_setsUser作为...中的第一个声明

这也失败了,因为

org.opentest4j.AssertionFailedError: expected:<S[TARTED]> but was:<S[uccess(DESTROYED)]>

现在事实上它并Success(DESTROYED)没有DESTROYED告诉我我无论如何都使用了错误的状态,但我更关心的是它确实在测试一开始就说DESTROYED对了。

那么为什么活动被破坏了,我如何让它保持活力呢?

日志,不管它们值多少钱:

I/TestRunner: started: login_setsUser()
D/LifecycleMonitor: Lifecycle status change: my.user.app.ui.MainActivity@3b19973 in: PRE_ON_CREATE
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
W/m.u.in: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/m.u.in: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
W/m.u.in: Accessing hidden field Landroid/view/View;->mAccessibilityDelegate:Landroid/view/View$AccessibilityDelegate; (greylist, reflection, allowed)
I/System.out: MainActivity::onCreate completed in 1058603300 ns = 1058.6033ms
D/LifecycleMonitor: Lifecycle status change: my.user.app.ui.MainActivity@3b19973 in: CREATED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: Lifecycle status change: my.user.app.ui.MainActivity@3b19973 in: STARTED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: Lifecycle status change: my.user.app.ui.MainActivity@3b19973 in: RESUMED
    running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: Lifecycle status change: my.user.app.ui.MainActivity@3b19973 in: PAUSED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
W/Settings: Setting always_finish_activities has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@ee392ab
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@346ae87 in: PRE_ON_CREATE
    running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@346ae87 in: CREATED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
    callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@346ae87 in: STARTED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
    callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@346ae87 in: RESUMED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
    callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/HostConnection: HostConnection::get() New Host Connection established 0xd54e0ec0, tid 9260
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_1 
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/EGL_emulation: eglCreateContext: 0xbb3f60e0: maj 3 min 1 rcv 4
D/EGL_emulation: eglMakeCurrent: 0xbb3f60e0: ver 3 1 (tinfo 0xbb4924d0)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
W/Gralloc3: mapper 3.x is not supported
D/HostConnection: createUnique: call
    HostConnection::get() New Host Connection established 0xbb4d1140, tid 9260
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_3_1 
D/eglCodecCommon: allocate: Ask for block of size 0x1000
D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ffff6000 size 0x2000
D/EGL_emulation: eglMakeCurrent: 0xbb3f60e0: ver 3 1 (tinfo 0xbb4924d0)
I/OpenGLRenderer: Davey! duration=2296ms; Flags=1, IntendedVsync=2400399771696, Vsync=2402283104954, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=2402290151800, AnimationStart=2402290416600, PerformTraversalsStart=2402294169900, DrawStart=2402612162700, SyncQueued=2402653256800, SyncStart=2402655900200, IssueDrawCommandsStart=2402655952000, SwapBuffers=2402679617700, FrameCompleted=2402698544800, DequeueBufferDuration=57000, QueueBufferDuration=404000, 
D/LifecycleMonitor: Lifecycle status change: my.user.app.ui.MainActivity@3b19973 in: STOPPED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
    callback completes: androidx.test.core.app.ActivityScenario$1@1519030
    running callback: androidx.test.core.app.ActivityScenario$1@96310a4
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@96310a4
D/LifecycleMonitor: Lifecycle status change: my.user.app.ui.MainActivity@3b19973 in: DESTROYED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
    callback completes: androidx.test.core.app.ActivityScenario$1@1519030
    running callback: androidx.test.core.app.ActivityScenario$1@96310a4
    callback completes: androidx.test.core.app.ActivityScenario$1@96310a4
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@346ae87 in: PAUSED
    running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@96310a4
    callback completes: androidx.test.core.app.ActivityScenario$1@96310a4
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@c50e4e9
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@a161c9c in: PRE_ON_CREATE
    running callback: androidx.test.core.app.ActivityScenario$1@1519030
    callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@96310a4
    callback completes: androidx.test.core.app.ActivityScenario$1@96310a4
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@a161c9c in: CREATED
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@1519030
    callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@96310a4
    callback completes: androidx.test.core.app.ActivityScenario$1@96310a4
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@a161c9c in: STARTED
    running callback: androidx.test.core.app.ActivityScenario$1@1519030
    callback completes: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: running callback: androidx.test.core.app.ActivityScenario$1@96310a4
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@96310a4
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@a161c9c in: RESUMED
    running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
    running callback: androidx.test.core.app.ActivityScenario$1@96310a4
    callback completes: androidx.test.core.app.ActivityScenario$1@96310a4
D/EGL_emulation: eglMakeCurrent: 0xbb3f60e0: ver 3 1 (tinfo 0xbb4924d0)
D/[VOLLEY REQ DISPATCHER]: [VOLLEY REQUEST] VolleyLoginRequest
D/Volley: [546] WaitingRequestManager.maybeAddToWaitingRequests: new request, sending to network 1-https://user.my/login
D/LifecycleMonitor: Lifecycle status change: my.user.ui.ui.LoginActivity@346ae87 in: STOPPED
    running callback: androidx.test.core.app.ActivityScenario$1@1519030
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@1519030
    running callback: androidx.test.core.app.ActivityScenario$1@96310a4
D/LifecycleMonitor: callback completes: androidx.test.core.app.ActivityScenario$1@96310a4
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: my.user, PID: 9189
    java.lang.NullPointerException: Cannot run onActivity since Activity has been destroyed already
4

0 回答 0