seiji / mocking
forked from sogabe/mocking
.idea added Objects instead of java's one 4 months ago
RuleSet RuleSet & FindBugs Checkstyle check 2 years ago
src added Objects instead of java's one 4 months ago
.gitattributes use Japanese 3 months ago
.gitignore ログまわりの修正 2 years ago
Jenkinsfile fixed stage name 3 months ago
LICENSE added LICENCE text 2 years ago
README.md Maven instructions 2 years ago
build.gradle added build.gradle 1 year ago
pom.xml bumped libraries 4 months ago
README.md

Mocking

Build Status

Mocking is a test framework which is inspired by Restito.

Mocking provides a DSL to:

  • Mimic rest server behavior
  • Record HTTP calls to the server
  • Perform verification against happened calls
  • Automatic serialization

Static import

In order to use Mocking effectively, it's recommended to statically import methods from the following classes:

import static com.buildria.mocking.Mocking.when;
import static com.buildria.mocking.Mocking.verifyWhen;

Quick example

public class SampleCodeTest {

    @Rule
    public Mocking mocking = new Mocking();

    @Rule
    public TestNameRule testNameRule = new TestNameRule();

    @Before
    public void setUp() throws Exception {
        RestAssured.port = mocking.getPort();
    }

    @Test
    public void testReadMeSampleCode() {
        Person person = new Person("Bob", 20);

        // MockingTest
        when("/api/p/{id}").
                withPathParam("id", 5).
        then().
                withStatusCode(SC_201_CREATED).
                withContentType("application/json; charset=UTF-8").
                withBody(person);

        // Rest-assured
        given().
                pathParam("id", 5).
                accept("application/json").
                contentType("application/json; charset=UTF-8").
                body(person).
        when().
                put("/api/p/{id}").
        then().
                statusCode(SC_201_CREATED).
                contentType("application/json; charset=UTF-8").
                body("name", is("Bob")).
                body("old", is(20));

        // MockingTest
        verifyWhen("/api/p/{id}").
                withPut().
                withPathParam("id", 5).
        then().
                withAccept("application/json").
                withContentType("application/json; charset=UTF-8").
                withBody("name", is("Bob")).
                withBody("old", is(20)).
                withoutHeader("Referer");
    }
    
(snip)
    
}

Maven instructions

    <dependencies>
        <dependency>
            <groupId>com.buildria.mocking</groupId>
            <artifactId>mocking</artifactId>
            <version>0.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>buildria.com</id>
            <name>buildria.com Maven2 Repository</name>
            <url>http://ci.buildria.com/plugin/repository/everything/</url>
        </repository>
    </repositories>