Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response ERROR #63

Closed
nksaroj opened this issue Apr 25, 2017 · 1 comment
Closed

Response ERROR #63

nksaroj opened this issue Apr 25, 2017 · 1 comment

Comments

@nksaroj
Copy link

nksaroj commented Apr 25, 2017

Why I get the below error.

E/RESTMock: <- Response ERROR: NOT MOCKED: GET /ServiceStatus HTTP/1.1

Below is my code

@rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(HomePageActivity.class,false,true);

@Before
public void init() {


    //be sure to reset it before each test!
    RESTMockServer.reset();


}



@Test
public void shouldDisplayUpgradeRequiredMessage() {



    RESTMockServer.whenGET(pathContains("ServiceStatus"))
            .thenReturnFile(200, "ServiceStatus.json");

    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }


    RequestsVerifier.verifyGET(pathEndsWith("ServiceStatus.json")).exactly(1);


    onView(ViewMatchers.withId(R.id.tvServiceMessage)).check(matches(withText(UpgradeMessage)));

}

This is the detailed stacktrace
04-25 19:55:28.008 17309-17309/CommCore.avv.Debug D/LifecycleMonitor: Lifecycle status change: CommCore.avv.app.homepage.HomePageActivity@c0dfeb5 in: STARTED
04-25 19:55:28.009 17309-17340/CommCore.avv.Debug D/OkHttp: --> GET http://localhost:44683/ServiceStatus http/1.1
04-25 19:55:28.009 17309-17340/CommCore.avv.Debug D/OkHttp: Referer: http://localhost:44683/?OS=Android&OSVersion=7.1.1&Make=unknown&Model=Android SDK built for x86&AppVersion=4.05.0&ScreenWidth=1080&ScreenHeight=1776&UUID=641c8afde8f574b1
04-25 19:55:28.009 17309-17340/CommCore.avv.Debug D/OkHttp: --> END GET
04-25 19:55:28.010 17309-17309/CommCore.avv.Debug D/LifecycleMonitor: Lifecycle status change: CommCore.avv.app.homepage.HomePageActivity@c0dfeb5 in: RESUMED
04-25 19:55:28.022 17309-17341/CommCore.avv.Debug D/RESTMock: -> New Request: GET /ServiceStatus HTTP/1.1
04-25 19:55:28.022 17309-17341/CommCore.avv.Debug E/RESTMock: <- Response ERROR: NOT MOCKED: GET /ServiceStatus HTTP/1.1
04-25 19:55:28.023 17309-17341/CommCore.avv.Debug I/MockWebServer: MockWebServer[44683] received request: GET /ServiceStatus HTTP/1.1 and responded: HTTP/1.1 500 Server Error
04-25 19:55:28.024 17309-17340/CommCore.avv.Debug D/OkHttp: <-- 500 Server Error http://localhost:44683/ServiceStatus (15ms)

@andrzejchm
Copy link
Owner

1: You mock request http://localhost:44683/ServiceStatus and verify if a request with path that ends with ServiceStatus.json is called (mind the .json ending in verifier)

2: You specified your test rule to start the activity automatically for each test. This means that the first line of your test is going to be executed after your activity starts. If your activity performs those requests in onCreate or onStart or event in onResume then they will be fired before you even manage to mock those tests in MockWebServer. To fix that, replace
new ActivityTestRule<>(HomePageActivity.class,false,true); with new ActivityTestRule<>(HomePageActivity.class,false,false); (mind the false at third parameter). and start your activity manually in the test AFTER you specify mocks with mActivityRule.launchActivity(null), so it would look something like this:

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

@Before
public void init() {
    //be sure to reset it before each test!
    RESTMockServer.reset();
}

@Test
public void shouldDisplayUpgradeRequiredMessage() {
    RESTMockServer.whenGET(pathContains("ServiceStatus"))
            .thenReturnFile(200, "ServiceStatus.json");
    mTestRule.launchActivity(null);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    RequestsVerifier.verifyGET(pathEndsWith("ServiceStatus.json")).exactly(1);
    onView(ViewMatchers.withId(R.id.tvServiceMessage)).check(matches(withText(UpgradeMessage)));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants