0

我正在尝试拦截在我的 espresso 测试中进行的网络调用,以便我可以使用本地副本覆盖响应并使测试更具确定性。我面临的问题是即使我正确设置了 MockWebServer 我从来没有看到任何当我在浓缩咖啡测试中进行网络调用时,http 调用被它拦截。我花了将近两天的时间阅读这方面的内容,但无法找出问题所在。

public class FragmenEspressoTest extends InstrumentationTest<SomeActivity> {
    private MockWebServer mockWebServer;

    @Override
    public Class<HomeDrawerActivity> getActivityClass() {
        return HomeDrawerActivity.class;
    }

    @Before
    public void setUp() {
        super.setUp();
        mockWebServer = new MockWebServer();
        mockWebServer.setDispatcher(dispatcher);
        try {
            mockWebServer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    final Dispatcher dispatcher = new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {

            if (request.getPath().equals("/v1/login/auth/")){
                return new MockResponse().setResponseCode(200);
            } else if (request.getPath().equals("v1/check/version/")){
                return new MockResponse().setResponseCode(200).setBody("version=9");
            } else if (request.getPath().equals("/v1/profile/info")) {
                return new MockResponse().setResponseCode(200).setBody("{\\\"info\\\":{\\\"name\":\"Lucas Albuquerque\",\"age\":\"21\",\"gender\":\"male\"}}");
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    @After
    public void tearDown() {
        super.tearDown();
        try {
            mockWebServer.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void verifyUITest() {
    //verify some UI do some network calls
   }
}
4

0 回答 0