Android Espresso的测试:
第一版
export class AppComponent {
constructor(private cartService: CartService) {
}
doSomething() {
this.cartService.addItem();
}
}
这里是结果日志:
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
private lateinit var mockServer: MockWebServer
@Rule
@JvmField
var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java, false, false)
@Before
fun setup() {
mockServer = MockWebServer()
mockServer.start(8081)
Debug.d(TAG, "setup_START")
}
@Test
fun testMockServer() {
Debug.d(TAG, "testMockServer_start")
mockServer.enqueue(MockResponse()
.setResponseCode(200)
.setBodyDelay(10000, TimeUnit.MILLISECONDS)
.setBody(FileUtil.getStringFromFile(context, randomTraderStubName.stubName)));
Debug.d(TAG, "testMockServer_finish")
}
@After
@Throws
fun tearDown() {
mockServer.shutdown()
}
,您会看到立即显示文本“ 05-31 18:10:52.327 D/com.myproject.activity.TradersActivityTest(10312): testMockServer_start
05-31 18:10:52.371 D/com.myproject.activity.TradersActivityTest(10312): testMockServer_finish
”。不用等10秒。
第二版:
testMockServer_finish
结果logcat:
@Test
fun testMockServer() {
Debug.d(TAG, "testMockServer_start")
tradersIntentTestRule.finishActivity()
mockServer.enqueue(MockResponse()
.setResponseCode(200)
.setBodyDelay(10000, TimeUnit.MILLISECONDS)
.setBody(FileUtil.getStringFromFile(context, randomTraderStubName.stubName)));
tradersIntentTestRule.launchActivity(Intent())
Debug.d(TAG, "testMockServer_finish")
}
,现在您可以看到文本“ 05-31 18:14:54.103 D/com.myproject.android.activity.TradersActivityTest(11689): testMockServer_start
05-31 18:15:04.322 D/com.myproject.android.activity.TradersActivityTest(11689): testMockServer_finish
”在10秒后显示
为什么在第一个版本中不等待10秒?