mvc-requester

MvcRequester is a tool to make and assert REST-API communication

License

License

GroupId

GroupId

com.jupiter-tools
ArtifactId

ArtifactId

mvc-requester
Last Version

Last Version

0.4
Release Date

Release Date

Type

Type

jar
Description

Description

mvc-requester
MvcRequester is a tool to make and assert REST-API communication
Project URL

Project URL

https://github.com/jupiter-tools/mvc-requester
Source Code Management

Source Code Management

https://github.com/jupiter-tools/mvc-requester

Download mvc-requester

How to add to project

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

Dependencies

compile (9)

Group / Artifact Type Version
org.springframework : spring-web jar [5.0.16,)
org.springframework : spring-webmvc jar [5.0.16,)
org.springframework : spring-test jar [5.0.16,)
org.springframework.security : spring-security-test jar [5.0.16,)
org.hamcrest : hamcrest-library jar 1.3
com.fasterxml.jackson.core : jackson-databind jar 2.10.3
com.google.guava : guava jar 29.0-jre
org.apache.commons : commons-lang3 jar 3.3.2
javax.servlet : javax.servlet-api jar 3.1.0

test (7)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.2.0
org.junit.jupiter : junit-jupiter-engine jar 5.2.0
org.junit.platform : junit-platform-engine jar 1.2.0
org.junit.jupiter : junit-jupiter-params jar 5.2.0
org.assertj : assertj-core jar 3.11.1
org.projectlombok : lombok jar 1.16.20
org.apache.commons : commons-io jar 1.3.2

Project Modules

There are no modules declared in this project.

MvcRequester

Getting started

You need to add a next dependency:

<dependency>
    <groupId>com.jupiter-tools</groupId>
    <artifactId>mvc-requester</artifactId>
    <version>0.4</version>
</dependency>

And now you can write MVC tests in a more simple way.

Let’s consider the next controller:

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/object")
    public SimpleObject getObject() {
        return new SimpleObject("test-name", 1987);
    }

    @GetMapping("/custom-object")
    public SimpleObject getWithParams(@RequestParam("name") String name,
                                      @RequestParam("value") int value) {
        return new SimpleObject(name, value);
    }

    @GetMapping("/{id}/object")
    public SimpleObject getWithPathVariable(@PathVariable("id") int id) {
        return new SimpleObject(String.valueOf(id), id);
    }
}

Simple GET request:

@Test
void testReturnAs() throws Exception {
    // Act
    SimpleObject result = MvcRequester.on(mockMvc)
                                      .to("/test/object")
                                      .get()
                                      .returnAs(SimpleObject.class); (1)
    // Asserts
    assertThat(result).isNotNull() (2)
                      .extracting(SimpleObject::getName, SimpleObject::getValue)
                      .containsOnly("test-name", 1987);
}
  1. return received response as a type safety object

  2. asserting of the received object with a type safety

Make a GET request with parameters:

@Test
void getWithParams() throws Exception {
    // Act
    SimpleObject result = MvcRequester.on(mockMvc)
                                      .to("/test/custom-object")
                                      .withParam("name", "custom")
                                      .withParam("value", 10101)
                                      .get()
                                      .returnAs(SimpleObject.class);
    // Asserts
    assertThat(result).isNotNull()
                      .extracting(SimpleObject::getName, SimpleObject::getValue)
                      .containsOnly("custom", 10101);
}

Using path variables

MvcRequester.on(mockMvc)
            .to("/users/{name}/acls", "admin") (1)
            .get()
            .returnAs(AclDto.class);
  1. String admin will be put instead of {name} variable in the url, before send request.

Checking returned status

@Test
void testCreateObject() throws Exception {
    MvcRequester.on(mockMvc)
                .to("/objects/create")
                .post()
                .expectStatus(HttpStatus.CREATED); (1)
}
  1. Check the HTTP status of the response

Send POST request with the body

Let’s consider the next controller:

@RestController
@RequestMapping("/test")
public class TestController {

    @PostMapping("/object-body")
    public SimpleObject postWithBody(@RequestBody SimpleObject body) {
        return new SimpleObject(body.getName() + "-test",
                                body.getValue() + 1000);
    }
}
SimpleObject postBody = new SimpleObject("body", 987); (1)

SimpleObject result = MvcRequester.on(mockMvc)
                                  .to("/test/object-body")
                                  .post(postBody) (2)
                                  .returnAs(SimpleObject.class);
  1. create an object which will send in the body

  2. send a POST request with converting the body to JSON

Expected Parametrized Type

For example, we consider an API which return the list of entities:

@RestController
@RequestMapping("/objects")
public class TestController {

    @GetMapping("/list")
    public List<SimpleObject> getObject() {
        SimpleObject a = new SimpleObject("AAA", 1);
        SimpleObject b = new SimpleObject("BBB", 1);
        SimpleObject c = new SimpleObject("CCC", 1);
        return Arrays.asList(a, b, c);
    }
}

and we can test it like that:

@Test
void parametrizedType() throws Exception {
    // Act
    List<SimpleObject> objectList = MvcRequester.on(mockMvc)
                                                .to("/objects/list")
                                                .get()
                                                .doReturn(new TypeReference<List<SimpleObject>>() {});
    // Asserts
    assertThat(objectList).isNotNull()
                          .hasSize(3)
                          .extracting(SimpleObject::getName)
                          .containsOnly("AAA", "BBB", "CCC");
}

Use custom headers in request

MvcRequester.on(mockMvc)
            .to("test/headers/check")
            .withHeader("custom-header", "12345")
            .get();

Upload the MultipartFile

byte[] data = "file content".getBytes();

MvcRequester.on(mockMvc)
            .to("/test/create")
            .withFile("data",
                      "filename.txt",
                      MimeType.valueOf("text/plain"),
                      data)
            .upload();

Authorization

OAuth

MvcRequester.on(mockMvc)
            .to("/test/oauth")
            .withOAuth(TOKEN)
            .get();

will send a request with the next header:

Authorization: Bearer {TOKEN}

Basic Authorization

String result = MvcRequester.on(mockMvc)
                           .to("/test/basic")
                           .withBasicAuth("root", "12345")
                           .post()

will send a request with the next header:

Authorization: Basic {base64}

Response charset

To get a response in the specific charset you can use MvcRequestResult.charset method, for example when we expect a response in cp1251:

String response = MvcRequester.on(mockMvc)
                              .to("/api/endpoint")
                              .get()
                              .charset(Charset.forName("cp1251"))
                              .returnAsPrimitive(String.class);

By default MvcRequester uses the UTF-8 charset.

com.jupiter-tools

Versions

Version
0.4
0.3-spring4
0.3
0.2-spring4
0.2
0.1