我正在尝试使用 okhttp3.mockwebserver.MockWebServer 在 Android 上进行一些仪表测试。这是我在 build.gradle (:app) 文件中用于仪器测试的内容:
androidTestImplementation 'androidx.annotation:annotation:1.1.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestUtil 'androidx.test:orchestrator:1.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test:core:1.3.0'
androidTestImplementation 'org.mockito:mockito-android:2.7.22'
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.9.0'
Run Code Online (Sandbox Code Playgroud)
这是我在 andoidTest/java 文件夹中的 java 文件:
import android.content.Intent;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
public class MainActivityTest {
MockWebServer server = new MockWebServer();
@Rule
public ActivityTestRule<MainActivity> activityRule
= new ActivityTestRule<>(MainActivity.class);
@Test
public void testNoSessionData() {
try {
server.enqueue(new MockResponse().setBody("[]").setResponseCode(200));
server.start(8080);
activityRule.launchActivity(
new Intent(
InstrumentationRegistry.getInstrumentation().getContext(),
MainActivity.class
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
);
} catch (Exception e) …Run Code Online (Sandbox Code Playgroud)