3

I'm running mock web server to test REST API calls, when activity launch and start the mockWebserver and execute the API call I'm getting the connection refused,using OKHTTP v-3.10.0 & Retrofit 2.3.0

ActivityTest Code:

 @Rule
    public ActivityTestRule<STBUpgradeActivity> mActivityRule = new ActivityTestRule<>(STBUpgradeActivity.class, false, false);

    private MockWebServer server;

    @Before
    public void setUp() throws Exception {

        super.setUp();
        server = new MockWebServer();
        server.start();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        String serverUrl = server.url("/").toString();
        EnvConfig.WEB_API_SIT = serverUrl;
    }

    @Test
    public void testPageContentIsShown() throws Exception {
        String fileName = "stb_upgrade_page_content_200.json";
        server.enqueue(new MockResponse()
                .setResponseCode(200)
                .setBody(RestServiceTestHelper.getStringFromFile(getInstrumentation().getContext(), fileName)));

        Intent intent = new Intent();
        mActivityRule.launchActivity(intent);

        onView(withId(R.id.button_upgrade)).check(matches(isDisplayed()));
        onView(withText("New residential subscribers and standard install only. If Sports is a part of your package")).check(matches(isDisplayed()));
    }

======= RestAPI client

 private Retrofit getClient() { 
        retrofit = new Retrofit.Builder()
                .baseUrl(WEB_API_HOST)
                .addConverterFactory(GsonConverterFactory.create())
                .client(getHttpClient())
                .build();

        return retrofit;
    }

failure call back in debug mode

mockserver start

Sam
  • 6,215
  • 9
  • 71
  • 90

1 Answers1

1

Change the setUp code to

 @Before
    public void setUp() throws Exception {

        super.setUp();
        server = new MockWebServer();
        server.start(8080);
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
    }

set the Retroft Base URL to

private Retrofit getClient() {
        retrofit = new Retrofit.Builder()
                .baseUrl("http://localhost:8080/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(getHttpClient())
                .build();

        return retrofit;
    }
Sam
  • 6,215
  • 9
  • 71
  • 90