我正在编写 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!!
}
}
这是我在 by 中添加的依赖项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 在其运行面板中显示的内容:
干杯!
编辑:忘记添加我在 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)
更新:测试在物理设备上运行时正确完成。仍然不确定是什么导致模拟器挂起。