Android Studio 3.3
Вот мой тест Espresso с MockWebServer:
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
@RunWith(AndroidJUnit4::class)
class TradersActivityTest {
private val context = InstrumentationRegistry.getInstrumentation().getContext()
private val TAG = TradersActivityTest::class.java.name
@Rule
@JvmField
var tradersIntentTestRule = IntentsTestRule(TradersActivity::class.java, false, false)
@Before
fun setup() {
mockServer = MockWebServer()
mockServer.start(8081)
Debug.d(TAG, "SUCCCESS_START_MOCKWEBSRVER")
val intent = Intent(targetContext, TradersActivity::class.java)
tradersIntentTestRule.launchActivity(intent)
}
@Test
fun oneTrader_manyWalletList() {
Debug.d(TAG, "oneTrader_manyWalletList_start")
mockServer.enqueue(MockResponse()
.setResponseCode(200)
.setBody(RestServiceTestHelper.getStringFromFile(context, "trader_6_wallet.json")));
onView(withId(R.id.noTradersTextView))
.check(matches(not(isDisplayed())))
}
Как вы можете видеть перед началом теста, я запускаю MockWebServer
на порту 8081 .
Когда я запускаю тест oneTrader_manyWalletList
Я получаю SocketTimeoutException
Здесь logcat:
04-21 16:13:24.808 D/com.myprojectui.activity.TradersActivity(20739): onCreate: savedInstanceState = null
04-21 16:13:24.929 D/com.myprojectapi.RestClientFactory(20739): static_initializer: START_GSON_setting!
04-21 16:13:24.930 D/com.myprojectapi.RestClientFactory(20739): static_initializer: END_GSON_setting!
04-21 16:13:24.957 D/com.myprojectui.activity.TradersActivity(20739): init: tradersViewModel = com.myprojectviewmodel.TradersViewModel@bc11d1e
04-21 16:13:24.959 D/OkHttp (20739): --> GET http://127.0.0.1:8081/traders/json http/1.1
04-21 16:13:24.959 D/LifecycleMonitor(20739): Lifecycle status change: com.myprojectui.activity.TradersActivity@c36fdf1 in: CREATED
04-21 16:13:24.960 D/LifecycleMonitor(20739): Lifecycle status change: com.myprojectui.activity.TradersActivity@c36fdf1 in: STARTED
04-21 16:13:24.960 D/LifecycleMonitor(20739): Lifecycle status change: com.myprojectui.activity.TradersActivity@c36fdf1 in: RESUMED
Как вы можете видеть TradersActivity
запуск HTTP-запроса на порт 8081 чтобы получить список трейдеров
OkHttp (20739): --> GET http://127.0.0.1:8081/traders/json http/1.1
Но через несколько секунд я получаю SocketTimeOutException:
04-21 16:13:34.798 I/StreamController( 2002): cleanUpControllerScope(nowcards-15-2ece5ba9-92e5)
04-21 16:13:34.977 D/OkHttp (20739): <-- HTTP FAILED: java.net.SocketTimeoutException
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): onFailure: Error: null
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): java.net.SocketTimeoutException
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:484)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at java.net.PlainSocketImpl.-wrap0(PlainSocketImpl.java)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at okio.Okio$2.read(Okio.java:140)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at okio.RealBufferedSource.indexOf(RealBufferedSource.java:354)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:226)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
04-21 16:13:34.988 E/com.myproject.api.DefaultRestClientCallback(20739): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
Почему?