JSON Verifier

The rule which helps to verify JSON returned by REST services (Spring, JAX-RS, etc).

License

License

MIT
Categories

Categories

JSON Data
GroupId

GroupId

io.github.yashchenkon
ArtifactId

ArtifactId

json-verifier
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

JSON Verifier
The rule which helps to verify JSON returned by REST services (Spring, JAX-RS, etc).
Project URL

Project URL

https://github.io/yashchenkon/jsonverifier
Source Code Management

Source Code Management

https://github.com/yashchenkon/jsonverifier

Download json-verifier

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.22
junit : junit jar 4.12
commons-io : commons-io jar 2.5
com.fasterxml.jackson.core : jackson-databind jar 2.8.8.1
com.flipkart.zjsonpatch : zjsonpatch jar 0.3.1

Project Modules

There are no modules declared in this project.

JSON verifier

The rule which helps to verify JSON returned by REST services (Spring, JAX-RS, etc). It allows you to hide paths to the files with JSON completely. Just put *.json file to json/TestClassName/testMethodName/ directory and the rule will compare response of the service to this file. Another option is JSON matcher that can be used to compare JSON objects.

Motivation

During writing the tests for REST services I saw that there are a lot of hardcoded JSON into test classes. I think that it's very bad practice and we should avoid hard coding. The better way is to save expected JSON content into the files from a classpath. Besides, we can automatically generate a path to this files based on a test class name and on a method name.

Code Example

There are two options:

  1. Using rule
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserQueryServiceApplication.class)
public class UserControllerTest {

    @Rule
    public JsonContentVerifier jsonContentVerifier = new JsonContentVerifier();
    
    // ..

    @Test
    public void testGetUserWithoutFriendsWithoutExpandParam() throws Exception {
        String responseBody = mockMvc.perform(get("/users/1").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse().getContentAsString();
        jsonContentVerifier.assertJson(responseBody);
    }
}
  1. Using JSON matcher
@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserQueryServiceApplication.class)
public class UserControllerTest {

    @Rule
    public JsonContentLoader jsonContentLoader = new JsonContentLoader();
    
    // ..

    @Test
    public void testGetUserWithoutFriendsWithoutExpandParam() throws Exception {
        String responseBody = mockMvc.perform(get("/users/1").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse().getContentAsString();
        assertThat(responseBody, JsonMatcher.json(jsonContentLoader.load()));
    }
}

Installation

Add following dependency to pom.xml

<dependency>
    <groupId>io.github.yashchenkon</groupId>
    <artifactId>json-verifier</artifactId>
    <version>1.0.0</version>
</dependency>

Tests

Description will be added later.

Contributors

Please, create pull requests in case of any fixes.

License

MIT

Versions

Version
1.0.0