MockWebServer: illegalStateException: 已经调用了 start()

MockWebServer: llegalStateException: start() already called

我尝试 运行 测试 MockWebServer

我想用模拟响应进行 UI 测试,这样我就可以测试 valid\invalid UI 更改,例如登录或显示登录错误 API.

然而,每次我 运行 我得到的代码都是 以下异常:

java.lang.IllegalStateException: start() already called

代码:

@RunWith(AndroidJUnit4.class)
public class UITestPlayground {

    String testUrl = "http://testurl.com/";
    MockWebServer server = new MockWebServer();

    @Rule
    public IntentsTestRule<LoginActivity> mIntentsRule = new IntentsTestRule<>(LoginActivity.class);

    @Before
    public void beforeHelper() throws IOException {
        TestHelper.removeUserAndTokenIfAny(mIntentsRule.getActivity());
        URLS.baseUrl = testUrl;
        server.url(URLS.baseUrl);
        //try to shutting down the server JUT IN CASE...
        server.shutdown();
        server.start();

    }

    @After
    public void afterHelper() throws IOException {
        server.shutdown();
    }


    @Test
    public void invalidLoginDueNotValidJSONResponse() {

        server.enqueue(new MockResponse().setBody("Something not valid JSON response"));

        String emailToBeTyped = "tester@tester.com";
        String passToBeTyped = "passtest";

        ViewActions.closeSoftKeyboard();
        // Type text and then press the button.
        onView(withId(R.id.login_email_edit)).perform(typeText(emailToBeTyped));
        ViewActions.closeSoftKeyboard();
        onView(withId(R.id.login_pass_edit)).perform(typeText(passToBeTyped));
        ViewActions.closeSoftKeyboard();
        onView(withId(R.id.log_in_btn)).perform(click());

        //TODO: check on valid error message appearing

    }
 }

我做错了什么? .start()只调用了一次,我连.shutdown()都在 case...我不明白怎么调用了不止一次

提前致谢。

github 的原始示例中,我发现 顺序颠倒了。

实际启动服务器,然后将其设置为 url。

而不是设置url然后启动服务器。

有意思。