Featured Mock

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

GroupId

GroupId

com.github.rmannibucau
ArtifactId

ArtifactId

featured-mock
Last Version

Last Version

0.4
Release Date

Release Date

Type

Type

jar
Description

Description

Featured Mock
Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Source Code Management

Source Code Management

https://github.com/rmannibucau/featured-mock

Download featured-mock

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.rmannibucau/featured-mock/ -->
<dependency>
    <groupId>com.github.rmannibucau</groupId>
    <artifactId>featured-mock</artifactId>
    <version>0.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.rmannibucau/featured-mock/
implementation 'com.github.rmannibucau:featured-mock:0.4'
// https://jarcasting.com/artifacts/com.github.rmannibucau/featured-mock/
implementation ("com.github.rmannibucau:featured-mock:0.4")
'com.github.rmannibucau:featured-mock:jar:0.4'
<dependency org="com.github.rmannibucau" name="featured-mock" rev="0.4">
  <artifact name="featured-mock" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.rmannibucau', module='featured-mock', version='0.4')
)
libraryDependencies += "com.github.rmannibucau" % "featured-mock" % "0.4"
[com.github.rmannibucau/featured-mock "0.4"]

Dependencies

compile (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.2.2
io.netty : netty-codec-http jar 4.0.9.Final

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

Featured Mock

The idea of this small project is to be able to use files (in the test classpath) to mock either method return or http requests.

Mock interfaces

Use com.github.rmannibucau.featuredmock.mock.FeaturedMock#mock method to generate a proxy. The rule to find the content are simple: it matches a file in the classloader built from the qualified class name (interface) and method name. For instance org.superbiz.MyInterface#myMethod will look for org/superbiz/MyInterface/myMethod[extension] file.

By default json, yml and xml files are searched.

When creating a mock you can specify a custom com.github.rmannibucau.featuredmock.mock.unmarshaller.Unmarshaller. The unmarshaller will be used to convert the file to objects (jackson for json and jaxb for xml by default).

Sample (Mock creation, json feature and result after calling a method):

{ "attr1": "uno", "attr2": "due" }
// DTO
public class Value {
    private String attr1;
    private String attr2;

    public String getAttr1() {
        return attr1;
    }

    public void setAttr1(final String attr1) {
        this.attr1 = attr1;
    }

    public String getAttr2() {
        return attr2;
    }

    public void setAttr2(final String attr2) {
        this.attr2 = attr2;
    }
}

// Service
public interface API {
    Value foo();
}

// mock usage
final API api = api = FeaturedMock.mock(API.class);
final Value value = api.foo();
// value.getAttr1() == "uno"
// value.getAttr2() == "due"

HTTP mock

You need to create a server. Here is a sample:

// the builder API supports host, ssl etc...
final FeaturedHttpServer server = new FeaturedHttpServerBuilder().port(1234).build().start();
// do some work
server.stop();

The idea is the same as with featured mocks excepted the file used to respond is found from the request uri. For instance a request on http://localhost:1234/foo/bar will match a file /foo/bar[ext].

Extension can be the same as for featured mock or no extension at all. In this last case the response type will be text/plain.

Note: if you need to match the same url for two http methods you can prefix your file name with the http method and -. For instance GET-foo/bar.

Note: com.github.rmannibucau.featuredmock.http.ContentTypeMapper allows you to change the extension to use for a particular request type. It is based on accept header.

Versions

Version
0.4
0.3
0.2
0.1