0

I have developed a small application with login screen and also written some Espresso script to automate the same.

I know that WireMock is a flexible library for stubbing and mocking web services. With the help of that one can create actual HTTP server so that your code under test can connect to as it would a real web service.

Now I want to develop web service for login screen with the help of wiremock.I have gone through details provided Here but the description is too much technical . If any one have any tutorial , reference or idea how to do the same that will be great.

I am using below tool.

  • Android Studio
  • Espresso
  • Gradle Build Tool
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dinesh Chandra
  • 329
  • 1
  • 2
  • 27
  • 2
    Here is a related talk I gave about using wiremock on Android. https://www.youtube.com/watch?v=-xQCNf_5NNM – yogurtearl Oct 06 '15 at 04:51
  • 2
    Thanks @yogurtearl . I watched the video . Its realy helpful and have started to implement it.. – Dinesh Chandra Oct 09 '15 at 09:52
  • 2
    Dinesh, in @yogutearl's video he mentioned that you have to mess with/modify WireMock to get it to work on Android. With the latest version, 2.0.8-beta you can use it without modification. I give details on how to get this setup in another Stack Overflow Answer: http://stackoverflow.com/a/34657479/509081 – Sam Edwards Jan 07 '16 at 14:36

3 Answers3

1

Disclaimer: I'm a java developer and have never developed on android.

Is that really what you want to do? Why not wrap your webservice invocations in a class with an interface? (using jaxrs or similar). Testing could then be much simpler as you can mock a plain old interface (using mockito or similar).

This approach has the benefit that tests would 'fail fast' if the web service interface changes in an incompatible way (since tests would no longer compile).

Mocking the http request / response all the time sounds verbose and hard to maintain compared with mocking an interface.

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • 1
    Your suggestion is valid for some levels of testing. However, if you want to test your app in it's pure form (except for a different URL endpoint), then WireMock allows you to do that. This is particularly helpful in an Android Espresso test because your app's User Interface is being driven by automated UI commands and you want to test your app as close to the final version as you can. – Sam Edwards Jan 07 '16 at 14:40
0

You could just run wiremock from your acceptance unit test like this:

@Rule
public WireMockRule wireMockRule = new WireMockRule(options().port(PORT).notifier(new ConsoleNotifier(true)));

@Rule
public ActivityTestRule<T> activityRule = new ActivityTestRule<T>(type) {
        @Override
        protected void beforeActivityLaunched() {
            stubFor(get(urlPathEqualTo("/myUrl"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withBody("{ 'status': 'OK' }")));
        }
    };

@Before
public void before() {
    activityRule.getActivity();
}

@Test
public void test() {
    // using espresso trigger the call on your activity
}

I assumed you have espresso in place and you know how to use that. Note that the stub must be in beforeActivityLaunched method otherwise if the activity makes some calls on start then the stubs won't be ready yet.

kazuar
  • 1,094
  • 1
  • 12
  • 14
-1

I'd like to add to the previous answers and suggest using Wiremock JAX-RS (disclaimer: I am the author). It allows you to automate the stubbing, if you express your interface with JAX-RS.

It adds the WireMockJaxrs.invocation(...) method that is used like this:

final List<ItemDTO> responseObject = Arrays.asList(new ItemDTO("pong"));
final StubMapping sm =
    stubFor( //
        invocation(ItemResouce.class, (r) -> r.getItems()) //
            .willReturn(aResponse().withStatus(SC_ACCEPTED), responseObject));
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Tomas Bjerre
  • 3,270
  • 22
  • 27