Android wiremock - path of json mappings files

1,803 views
Skip to first unread message

Chris THOLEY

unread,
Mar 17, 2016, 10:17:58 AM3/17/16
to wiremock-user
Hello and first of all thanks for this good work.

I would like to know where are the "mappings" and "__files" directories supposed to be stored on a Device/emulator ?

Currently, I have a basic test using Wiremock and Robotium with that "stubFor" :

stubFor(get(urlMatching("/1"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBodyFile("myTestJson.json")));

While the test runs, I can access the "localhost:{MY_PORT}/__admin" URL from the emulator's browser and it returns an empty "mappings" answer like this : 

{ "mappings" : [] } 

Obviously, I also get a 404 because no mappings seems to be configured.

Using Android Studio, the file "myTestJson.json" is stored classically in the src/test/resources/__files directory.


Regards

Tom Akehurst

unread,
Mar 17, 2016, 11:54:09 AM3/17/16
to wiremock-user
Stubs create programmatically don't get written automatically to mappings/__files, so you wouldn't see them there.

Assuming the stubFor(...) call succeeded, I'm surprised your mappings list was empty. Did you reset WireMock at the end of the test perhaps? Also, since you seem to be using the static stubFor(...), are you certain it's pointed at the correct host + port?

Chris THOLEY

unread,
Mar 17, 2016, 12:18:54 PM3/17/16
to wiremock-user
Yes, the stubFor(...) call succeed, for example when I retrieve the json the following simple way, the mapping is good and handled by wiremock :

@Rule
public WireMockRule wireMockRule = new WireMockRule(9998);

@Test
public void testWiremock(){

String jsonBody = assetTestFile(activity, "mytestJson.json");

        stubFor(get(urlMatching("/1"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody(jsonBody)));

        solo.unlockScreen();   //Robotium instruction

        //FAKE WAIT
        solo.waitForDialogToOpen(10000L);
}

Host+port is correct too : this way the http://localhost:9998/__admin call shows me the correct mapping. (from the emulator).

But the .withBodyFile("myTestJson.json") way seems not to work.
FYI, this one worked fine in simple unitTests so it seems to only be related to UI tests in my case (emulator).
Message has been deleted

Chris THOLEY

unread,
Mar 17, 2016, 1:15:22 PM3/17/16
to wiremock-user
Maybe here is a clue for us : 

When it's configured with the .withBodyFile() method and I call the requested URL (from the emulator's browser), I achieved to see this :

HTTP ERROR: 500
Problem accessing /1. Reason:
java.lang.RuntimeException: java.io.FileNotFoundException: /src/test/resources/__files/myTestJson.json: open failed: ENOENT (No such file or directory)


Seems wiremock on the emulator tries to access this path :  /src/test/resources/__files/myTestJson.json.

Tom Akehurst

unread,
Mar 19, 2016, 10:40:10 AM3/19/16
to wiremock-user
Apologies, I have no personal experience of running on Android. I wouldn't expect the default file source to just work on Android though, given that the filesystem is organised differently than a workstation.

I seem to remember something about an Android team writing their own implementation of the FileSource interface and plugging that in. Might be worth a try?

Sam Edwards

unread,
Mar 20, 2016, 10:37:05 PM3/20/16
to wiremock-user
Hey Chris, I have worked with WireMock on Android for a little over 3 months and haven't figured out a way to load mappings in like you can with mappings and __files on the desktop. I read in asset Json and serve that up using programmatic mappings as you do. If you figure out a way to do this, I'd love to know. I just haven't had the time to engineer something like that. I would try using the assets directory and try to figure out how to make it work from there. Sorry for no help, but hopefully reassures here is no easy answer yet.

Tom, what class reads in the files and mappings and is there a method that could be overridden for a custom implementation?

Tom Akehurst

unread,
Mar 21, 2016, 3:55:11 AM3/21/16
to wiremock-user
Yep, you can create your own implementation of FileSource and pass that into the rule or server constructor.

Anders Kristiansen

unread,
Nov 29, 2016, 7:16:51 AM11/29/16
to wiremock-user
Hi!

In regards to this answer:

Yep, you can create your own implementation of FileSource and pass that into the rule or server constructor.

I'm having a similar issue, and basically all I want to do is change the path of the file source. Would I really need to create a new implementation for this?

I've (naively) tried to set the path through this approach: WireMockConfiguration.wireMockConfiguration().fileSource(new ClassPathFileSource("my/custom/file/path")) and then passing the configuration object to the server constructor, as you suggested, but it did not work.

I haven't found any documentation for this either, and would appreciate any suggestions.

Chris Gunawardena

unread,
May 22, 2017, 5:40:00 AM5/22/17
to wiremock-user

The path is inside the simulator, so you need to have the test files in /assets folder so they get copied to the device or sim.
Then you can do this:

stubFor(any(anyUrl())
.atPriority(10)
.willReturn(aResponse().proxiedFrom("http://mobileapi.jumbo.com")));

// stub config because of http missing in prod
stubFor(any(urlEqualTo("/v2/configuration"))
.atPriority(1)
.willReturn(aResponse()
.withStatus(200)
.withBody(Utils.asset("testdata/configuration.json"))));

I had trouble with withBodyFile, so I"m using .withBody another function to read the file and copy it. 

/**
* Returns the content of a file in the path
* @param assetPath File path
* @return
*/
public static String asset(String assetPath) {
Context context = getInstrumentation().getContext();
try {
InputStream inputStream = context.getAssets().open(assetPath);
return inputStreamToString(inputStream, "UTF-8");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static final int BUFFER_SIZE = 4 * 1024;
private static String inputStreamToString(InputStream inputStream, String charsetName)
throws IOException {
StringBuilder builder = new StringBuilder();
InputStreamReader reader = new InputStreamReader(inputStream, charsetName);
char[] buffer = new char[BUFFER_SIZE];
int length;
while ((length = reader.read(buffer)) != -1) {
builder.append(buffer, 0, length);
}
return builder.toString();
}
Reply all
Reply to author
Forward
0 new messages