簡體   English   中英

我應該如何正確關閉我的AndroidX Espresso測試?

[英]How should I properly shutdown my AndroidX Espresso test?

我正在編寫一個Espresso測試來測試我的應用鏈接是否由我的應用正確處理。 我已經設置了Android Studio並創建了一個通過的測試,但問題是測試套件掛起了。 我創建了一個類LinkDispatcherActivity ,它負責解析傳入的鏈接並將其數據分派給適當的活動。 設置測試以使用ActivityTestRule啟動LinkDispatcherActivity並為其提供要測試的URL的意圖。 測試正在運行和通過,所以我假設我已經正確設置了所有內容(一個獨立的挑戰😜)。

我嘗試過使用新的ActivityScenarioRule但無法弄清楚如何將意圖傳遞給它。

這是我的測試類:

import android.content.Intent
import android.net.Uri
import android.widget.TextView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import gov.nih.nlm.wiser.R
import gov.nih.nlm.wiser.link.WiserLinkDispatcherActivity
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.instanceOf
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@LargeTest
class SubstanceLinkTest {
    @get:Rule
    val activityTestRule: ActivityTestRule<LinkDispatcherActivity>
            = ActivityTestRule(LinkDispatcherActivity::class.java, false, false) // initialTouchMode: false, launchActivity: false

    @Test
    fun shouldOpenLinkDispatcherActivity() {
        val i = Intent().apply {
            data = Uri.parse("https://mydomain/action?data=338")
        }

        activityTestRule.launchActivity(i)

        onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.action_bar))))
                .check(matches(withText("My Title"))) // Passes!!
    }
}

這是我在build.gradle添加的依賴build.gradle

dependencies {
    // Core library
    implementation 'androidx.test:core:1.2.0'

    // AndroidJUnitRunner and JUnit Rules
    implementation 'androidx.test:runner:1.2.0'
    implementation 'androidx.test:rules:1.2.0'

    // Assertions
    implementation 'androidx.test.ext:junit:1.1.1'
    implementation 'androidx.test.ext:truth:1.2.0'
    implementation 'com.google.truth:truth:0.42'

    // Espresso dependencies
    implementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.android.support:multidex-instrumentation:1.0.3'
}

最后,這是Android Studio在其“運行”面板中顯示的內容: Android Studio

干杯!

編輯:忘了添加我在Logcat中收到此錯誤:

2019-06-21 12:02:24.326 6311-6035/? E/cckz: *~*~*~ Channel {0} was not shutdown properly!!! ~*~*~*
        Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
    java.lang.RuntimeException: ManagedChannel allocation site
        at cclc.<init>(:com.google.android.gms@17455040@17.4.55 (100700-248795830):1)
        at cckz.<init>(:com.google.android.gms@17455040@17.4.55 (100700-248795830):2)
        at ccdo.b(:com.google.android.gms@17455040@17.4.55 (100700-248795830):14)
        at rzc.a(:com.google.android.gms@17455040@17.4.55 (100700-248795830):43)
        at rzc.a(:com.google.android.gms@17455040@17.4.55 (100700-248795830):58)
        at atbl.a(:com.google.android.gms@17455040@17.4.55 (100700-248795830):9)
        at atbl.a(:com.google.android.gms@17455040@17.4.55 (100700-248795830):26)
        at qbi.run(:com.google.android.gms@17455040@17.4.55 (100700-248795830):1)
        at sgs.b(:com.google.android.gms@17455040@17.4.55 (100700-248795830):37)
        at sgs.run(:com.google.android.gms@17455040@17.4.55 (100700-248795830):21)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at smq.run(Unknown Source:7)
        at java.lang.Thread.run(Thread.java:764)

更新:在物理設備上運行時,測試正確完成。 仍然不確定是什么導致模擬器掛起。

我建議使用@After注釋和一個名為'tearDown'的方法。 您可以在此處執行最終斷言並清理任何需要結束並可能調用publisher.shutdown();數據庫或進程publisher.shutdown(); publisher.awaitTermination(1, TimeUnit.MINUTES)希望這可以有所幫助。

檢查這個帖子:[ https://github.com/googleapis/google-cloud-java/issues/3648]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM