Giter Club home page Giter Club logo

webstub's Introduction

WebStub Build Status

This library intends to simplify stubbing out responses from external HTTP entities that your application/service depends on. This can be useful in testing your application from within JUnit tests. It internally uses Jetty as the servlet container.

Example usage:

@BeforeClass
public static void beforeAll() {
    server = newServer(9099);
    stubServer = server.withContext("/context");

    server.start();
}

@Before
public void setUp() {
    stubServer.reset();
}

@Test
public void shouldStubHttpCalls() {
    stubServer.get("/accounts/1").returns(response(200).withContent("account details"));

    Response response = httpClient.get("http://localhost:9099/context/accounts/1");
    assertThat(response.status(), is(200));
    assertThat(response.content(), is("account details"));
}

@Test
public void shouldMatchResponseForRequestsContainingHeaders() {
    stubServer.get("/accounts/1").withHeader("x", "y").returns(response(200));

    Response response = httpClient.get("http://localhost:9099/context/accounts/1", asList(new BasicHeader("x", "y")));
    assertThat(response.status(), is(200));
}

@AfterClass
public static void afterAll() {
    server.stop();
}

Refer to tests: RequestLineStubbingSpec, HeaderStubbingSpec, BodyContentStubbingSpec, MultipleContextStubbingSpec.

For a real example, refer to WebStubDemo where a real Spring application is tested using WebStub and Inproctester The example uses the latest released version of WebStub and hence there might be minor differences to the example shown above.

Features

  • Stubs a real server (embedded Jetty) on-the-fly from within your tests (so test data setup lies in the test) using a fluent DSL
  • Supports GET, POST, PUT and DELETE verbs
  • Supports stubbing requests (method, uri, query params, headers, content) and responses (status code, headers, content)
  • Allows resetting stub configuration before/after every test, to keep individual tests independent
  • Requests/responses can take ContentBuilder implementations; create ContentBuilders (e.g. to convert your domain objects to JSON or XML) to suit your needs
  • Supports stubbing multiple web contexts on a single server
  • Can be used for functional testing of services running externally or inside the test itself

Releases

The latest released version is 1.0.1

The released artifact is available at Maven Central Repository, while the latest snapshot is available here. The maven co-ordinates are:

<dependency>
  <groupId>com.thoughtworks</groupId>
  <artifactId>web-stub</artifactId>
  <version>x.y.z</version>
</dependency>

The release notes are available here.

RoadMap

  • Support for https
  • Support for auth
  • Other HTTP verbs
  • Starting stub servers in-process, rather than on real native ports
  • Dashboard for the stub server
  • Better reporting of assertion failures

Get involved

  • Raise bugs
  • Suggest features
  • Collaborate, fork

License

WebStub is distributed under the terms of Apache Software License v2.0: http://www.apache.org/licenses/LICENSE-2.0.html

webstub's People

Contributors

arjunk avatar tusharm avatar

Watchers

 avatar  avatar

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.