stasek / fake-rest-server

Simple fake REST server for testing integration
MIT License
0 stars 0 forks source link
annotations jar java junit4 junit5 kotlin mock rest-api test testing

fake-rest-server

apm Bintray WIKI EXAMPLE

This server is a stub for testing RestApi integration.

maven

...
<repositories>
    <repository>
      <id>jcenter</id>
      <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>
...
<dependency>
  <groupId>ru.svnik.tests</groupId>
  <artifactId>fake-rest-server</artifactId>
  <version>0.0.13</version>
  <type>pom</type>
</dependency>
...

gradle

compile 'ru.svnik.tests:fake-rest-server:0.0.13'

Also you can build the lid by yourself.

gradle lib  ## with all dependencies
gradle jar  ## only fake server

Take lib in ./build/libs

use in your code

kotlin

 val app = FakeServer(port : Int = 7000, pathToFileContainsResource: String = "/resource.json").server()
  //your test code
 app.stop()

java

 FakeServer app = FakeServer(int port, String pathToFileContainsResource).server();
  //your test code
 app.stop();

use annotation in Junit4

import ru.svnik.tests.elements.FakeRestServer;

public class JunitAnnotationTest {

    @Rule
    public FakeRestServerRule rule = new FakeRestServerRule();

    @Test
    @FakeRestServer(port = 7000,resourceFile = "/resource.json")
    public void name() throws InterruptedException {
        sleep(300);
    }
}

use annotation in Junit5

import ru.svnik.tests.junit5.FakeRestServer;

public class JunitAnnotationTest {

    @Test
    @FakeRestServer(port = 7000,resourceFile = "/resource.json")
    public void name() throws InterruptedException {
        sleep(300);
    }
}

use annotation in TestNG

import ru.svnik.tests.elements.FakeRestServer;

@Listeners(FakeRestServerListener.class)
public class TestNGAnnotationTest {

        @Test
        @FakeRestServer(port = 7000,resourceFile = "/resource.json")
        public void name() throws InterruptedException {
            sleep(300);
        }
}

Required resources

Required resources is resource.json and error.json. error.json file of the standard response on error. resource.json contains all information about the server resources.

 [
  {
  "resource": "/api/hello/",
    "code": 200,
 },
 {
    "resource": "/api/robots/",
    "code": 200,
    "path": "/robots.json",
    "method": ["get"],
    "path_to_error": "/empty.json",
    "error_code": 200,
    "required_field":{
      "info.old_info.info[1].name": "server"
    },
    "required_headers": {
      "token": "123456789"
    },
    "required_queries": {
      "last_name": ["Rodriguez", "Anna"]
    }
  }
 ]

resource and path required fields in resource.json.

Not required fields:

as app

If you want to use the server as a stand-alone application, use the app profile.

gradle app

java -Dport=80 -Dresourcefile="/resource.json" -jar fake-rest-server-app-version-hash.jar

Unfortunately you have to change the files in the jar.