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

Unable to get RESTMock to work with external libraries of RESTapi #68

Closed
syariffudinzo opened this issue Jul 3, 2017 · 5 comments
Closed

Comments

@syariffudinzo
Copy link

Hi, first of all I really admire the simplicity.

but somehow I find it difficult to implement in my project.
so my project consist of multiple libraries in which consist of multiple api call from each Library.

I've followed all the guide given but still unable to mock the rest data (the RESTMock doesn't intercept the API call)

defaultConfig {
testInstrumentationRunner 'io.appflate.restmock.android.RESTMockTestRunner'
}
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    androidTestCompile ('com.github.andrzejchm.RESTMock:android:0.2.2') {
        exclude module: 'okhttp'
    } 

exclude because of some conflict of versioning.

In my test:

public class SomeTest {

@Rule
    public ActivityTestRule<LauncherActivity> mActivityTestRule = new
            ActivityTestRule<>(LauncherActivity.class);

@Before
    public void startUp() {
        RESTMockServer.reset();
        retrofit = new Retrofit.Builder()
                .baseUrl(RESTMockServer.getUrl())
                .build();
    }

@Test
    public void someTest() {
        RESTMockServer.whenPOST(RequestMatchers.pathContains("customer/password_reset"))
                .thenReturnEmpty(200);
// do something here and there
}

@After
    public void tearDown() throws Exception {
        HelperTool.clearApplicationData(context);
    }

}

Above are some part of the code, I'm using espresso as you can notice in my gradle. when I run the code, the logcat still showing me that it points to my backend server. instead of RESTMockServer.getUrl()

Is there anything I missed here? Sorry still new in this

@andrzejchm
Copy link
Owner

First things first:

  1. Did you specify a custom test runner that starts RESTMock before the tests start?
  2. Do you perform any API call within the tests, or are those being made within the LauncherActivity? If you have those calls within launcher activity, then you will have to make sure it does have a Retrofit configuration pointing to RESTMock's url. In SomeTest you create new retrofit instance that is local to the test itself, but not to the app being tested.

@syariffudinzo
Copy link
Author

syariffudinzo commented Jul 3, 2017

  1. did you mean this?

b) RESTMockServerStarter

If you have your custom test runner and you can't extend RESTMockTestRunner, you can always just call the RESTMockServerStarter. Actually RESTMockTestRunner is doing exactly the same thing:

I thought it is the same thing, but I definitely will try it with both the dependencies and the RESTMockServerStarter

  1. it is being made in the LaucherActivity, and when you mean by

If you have those calls within launcher activity, then you will have to make sure it does have a Retrofit configuration pointing to RESTMock's url

does that mean i need to implement them in the main Application itself?

@andrzejchm
Copy link
Owner

andrzejchm commented Jul 3, 2017

the best way to understand this is to take a look into androidsample code within this repo. So:

https://github.com/andrzejchm/RESTMock/blob/develop/androidsample/src/main/java/io/appflate/restmock/androidsample/di/AppModule.java

in this dagger module, we build the retrofit object being used across the whole app. As you can see, there is a constructor parameter specifying the baseUrl. This one is being created in SampleApplication class when the app is running normally with the real url for the production servers. While running tests, I'm using TestApplication instead of SampleApplication as an Application class for the app, and there I'm specifying RESTMock's server url as a base url instead of real production url, as you can see here:
https://github.com/andrzejchm/RESTMock/blob/develop/androidsample/src/androidTest/java/io/appflate/restmock/androidsample/TestApplication.java

In order to be able to specify different Application subclass for tests and normal app run, you have to create a custom test runner, like in example: https://github.com/andrzejchm/RESTMock/blob/develop/androidsample/src/androidTest/java/io/appflate/restmock/androidsample/CustomTestRunner.java

and have it specified within your build.gradle file
testInstrumentationRunner 'io.appflate.restmock.androidsample.CustomTestRunner'
as can be seen here: https://github.com/andrzejchm/RESTMock/blob/develop/androidsample/build.gradle

@tomaszmnich
Copy link

@andrzejchm Could you expand README tutorial with above steps? It could be helpful for the new RESTMock users.

@andrzejchm
Copy link
Owner

This is already a part of the readme as 6. point in the Setup. I will add a reference to this issue though for better explanation

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

3 participants