Android Studio 3.3,Koltlin 1.3 Java 1.8
这是我的Espresso对MockWebServer的测试。
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.runner.AndroidJUnit4
import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
class AddTraderActivityNetworkTest {
private lateinit var mockServer: MockWebServer
@Rule
@JvmField
var addTraderIntentTestRule: IntentsTestRule<AddTraderActivity> = IntentsTestRule(AddTraderActivity::class.java)
@Before
fun setup() {
mockServer = MockWebServer()
mockServer.start(8081)
}
@Test
fun buttonStart_click_serverError_showToast() {
mockServer.enqueue(MockResponse()
.setResponseCode(500))
onView(withId(R.id.startButton))
.perform(click())
onView(withText(R.string.server_error)).inRoot(ToastMatcher())
.check(matches(isDisplayed()))
}
@Test
fun buttonStart_click_timeOut_showToast() {
mockServer.enqueue(MockResponse()
.setResponseCode(200)
.throttleBody(1024, 1, TimeUnit.SECONDS))
onView(withId(R.id.startButton))
.perform(click())
onView(withText(R.string.service_unavailable)).inRoot(ToastMatcher())
.check(matches(isDisplayed()))
}
@After
@Throws
fun tearDown() {
// We're done with tests, shut it down
mockServer.shutdown()
}
}
开始测试buttonStart_click_serverError_showToast
时,测试成功。但是当我开始测试buttonStart_click_timeOut_showToast
时,我得到了错误:
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:169)
我该如何解决? 我开始活动,但出现错误。为什么?