Coder Social home page Coder Social logo

retromock's Introduction

Retromock

like Wiremock for Retrofit, but faster.

What is it?

Retromock is an easy way to mock external APIs if you use Square's HTTP client, retrofit. It allows you to specify expected requests and corresponding responses using a DSL reminiscent of wiremock.

Request matching is performed with hamcrest matchers, so it is easy to extend the DSL with your own matchers, e.g. for custom authentication schemes. Retromock comes with a wide variety of matchers that allow you to match on many parts of an HTTP request, including the path, query parameters or headers. You can also use JSON Path expressions to match on POST or PUT request bodies.

How do I use Retromock?

tl;dr: look at MockClientTest.

To use Retormock, you perform these steps:

  1. use the DSL to setup routes

    MockClient.Provider mockClient = MockClient.when()
     .GET("/some/path")
     .withHeader("x-foo", is("headerVal"))
     .matching(body(
       jsonPath("title", startsWith("test")),
       jsonPath("properties.foo", is(200))
     ))
     .thenReturn(helloWorld);

many of the methods are overloaded or have alternative ways of specifying the same functionality. For example, if you want to specify the request method yourself, you can use MockClient.when().aRequest().withMethod("PATCH").andPath("/whatever").

  1. Tell Retrofit to use your mock client instead of the HTTP client library you usually use

    RestAdapter restAdapter = new RestAdapter.Builder()
       .setClient(mockClient)
       .setEndpoint("http://example.org/") // ignored, but you must set some value
       // more config, e.g. for request interceptors
       .build();
  2. Use Retrofit as usual

    YourApiClient yac = restAdapter.create(YourApiClient.class);
    
    assertEquals("Hello, World", yac.someMethod());

Alternatives to Retromock

Depending of how much "infrastructure" you want to test, there are other approaches you can use.

  • In the simplest case, if you use retrofit, your API clients are simple Java interfaces, so you can provide mock implementations of the interfaces or use a mocking library like Mockito.

  • If you want to simulate network conditions like delays / connection failures, Square provides retrofit-mock.

  • Or you can go the whole hog and ramp up a server to serve your mock responses with various libraries like wiremock, et al.

retromock's People

Contributors

ccmtaylor avatar

Stargazers

Ivan Alvarado avatar Aleksander Brzozowski avatar Wade avatar Pranav Lathigara avatar Javier Cano avatar Joel Dean avatar Bernat Borrás Paronella avatar Nitish avatar Rashid Altayer avatar Sam Edwards avatar Sattar avatar Riccardo Ciovati avatar cesine avatar Pardeep Singh Sanghera avatar Matthew Compton avatar Paweł avatar Josh Skeen avatar Tiago Veloso avatar  avatar Manikanta Gade avatar Janusz Bagiński avatar  avatar  avatar Surya Gaddipati avatar

Watchers

Richie Vos avatar Shinji Kuwayama avatar Trek Glowacki avatar James Cloos avatar  avatar  avatar

retromock's Issues

Retrofit 2?

WireMock itself now works on Android. What are your thoughts? What are you using now?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.