Spring Cloud Contract - how to test simple string response?

1.5k views Asked by At

I'm using Spring Cloud Contract framework for REST services integration testing and related client apps contracts. It works for object JSON request / response, but is there a chance to check simple string. Espetially, service which responses Boolean (true or false in response body)?

Part of groovy definition:

response {
    status 200
    headers {
        header('Content-Type', 'application/json;charset=UTF-8')
    }
    body(
        value(client('true'), server(regex("true|false")))
    )
}

Related part of generated test:

    String responseBody = response.getBody().asString();
    null;

Is it a bug, isn't it? Or should I do something better in groovy definition? First row gives a chance generator understands what I want, but second line is completely out. Thanks for any hint.

P.S. I know there are different opinions about only string "true" validity as JSON response (some people says so, other not) Please, don't solve it, but only the real issue :-)

2

There are 2 answers

0
Veereshwaran Muniraj On

org.springframework.cloud.contract.spec.Contract.make { request { method 'GET' urlPath ('url')

    body([
             'json-string'
        ])
    headers {
        header('Content-Type', 'application/json;charset=UTF-8')
    }
}
response {
    status 200
    body( "\"true\"" )
} }

This is worked for me.

0
Marcin Grzejszczak On

The issue got fixed here https://github.com/spring-cloud/spring-cloud-contract/issues/443 . With the new releases (Dalston.SR5 and Edgware forward) this no longer will be a problem.