Assertions Module

RECORDO

JsonAssertion

Asserts that the actual object is equal as JSON to the expected one stored in a file.

If the expected JSON file is missing, the actual value will be saved as expected.

All you need to do is verify the details, and the test will be ready.

If an assertion fails, the actual value will be saved in a new file for manual comparison.

JsonAssertion Parameters

.using(ObjectMapper mapper)

Sets the ObjectMapper to be used for JSON conversion.

.extensible(boolean value)

Sets whether the comparison should allow for additional properties in the expected JSON.

Defaults to false.

.withStrictOrder(boolean value)

Sets whether the order of elements in the JSON arrays should be strictly enforced.

Defaults to true.

.including(String... fields)

Specifies a list of fields to be included during comparison.

Fields can be specified with nested structures using dot notation (e.g., parent.name, user.role.name).

You can also use index for collections and arrays (e.g., children[0].id, issues[0].tags[0].text) or wildcard character * to match any element (e.g., children[*].name, issues[*].tags[*].text).

.excluding(String... fields)

Specifies a list of fields to be excluded during comparison.

Fields can be specified with nested structures using dot notation (e.g., parent.name, user.role.name).

You can also use index for collections and arrays (e.g., children[0].id, issues[0].tags[0].text) or wildcard character * to match any element (e.g., children[*].name, issues[*].tags[*].text).

Examples

@Test
void should_get_book_by_id() {
    Book actual = bookService.getById(1L);
    
    JsonAssertion.assertAsJson(actual)
            .isEqualTo("/books/book.json");
}
@Test
void should_get_books_by_author(
        @Read("/books/author.json") Author author
) {
    Page<Book> actual = bookService.findAllByAuthor(author);
    
    JsonAssertion.assertAsJson(actual)
            .using(objectMapper)
            .extensible(true)
            .including("content[*].id", "content[*].title", "content[*].author.id")
            .isEqualTo("/books/short_books.json");
}
@Test
void should_get_all_books() {
    List<Book> actual = bookService.findAll();
    
    JsonAssertion.assertAsJson(actual)
            .excluding("author.firstName", "author.lastName")
            .withStrictOrder(false)
            .isEqualTo("/books/short_books.json");
}

CsvAssertion

Asserts that the CSV string matches the expected one from a file.

If the expected CSV file is missing, the actual value will be saved as expected.

You only need to verify them, and then the test will be ready.

If an assertion fails, the actual value will be saved for manual comparison.

CsvAssertion Parameters

withHeaders

With header line. Default value is false.

withStrictOrder

Requires strict ordering of array elements. Default value is false.

withColumnSeparator

Default value is ,

withLineSeparator

Default value is

Examples

@Test
void test() {
    String csv = ...
    CsvAssertion.assertCsv(csv)
        .withHeaders(true)
        .withStrictOrder(false)
        .isEqualsTo("/expected.csv");
}

Last updated