Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Controller unit test failure when the request body is empty and is read directly #82

@aentwist

Description

@aentwist

Implementing complete GraphQL support involves reading the request body and considering it as the query for one special case. In a controller, this means using request.reader.text.

def index() {
    def body = request.reader.text
    respond body
}

In a test class the controller method is called multiple times as there are multiple tests.

def "test"() {
    when:
    controller.index()

    then:
    true
}

def "test2"() {
    when:
    controller.index()

    then:
    true
}

This is a problem when the body is empty. When executing the second test, it fails with java.io.IOException: Stream closed. The tests are not independent.

Please note that when the body is not empty, i.e.
request.json = [hello: "world"]
the tests pass.

Please also note that replacing request.reader.text with request.JSON in the controller causes the tests to pass. This is specifically for reading the body text.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions