spring-cloud-contract: Error: constant string too long

Hi, I’m using spring-cloud-contract version 1.2.7.RELEASE.

When I try to load ‘text/html’ response body data from a file (when defining Groovy contract) and that data is more than 65536 bytes, I get an error ‘constant string too long’ when compiling Java generated test class.

Generated test has a lines similar to these: String responseBody = response.getBody().asString(); assertThat(responseBody).isEqualTo("Here goes a very long string read from file in Groovy contract."); // here's the failure

Loading file to response body in a contract: ... contentType(textHtml()) ... body(file("myFileWithLongString.txt")) ...

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Please change file('fileWithALongStringMoreThan65536Bytes.txt') to fileAsBytes('fileWithALongStringMoreThan65536Bytes.txt'). Then your generated test will look like this:

@SuppressWarnings("rawtypes")
public class ContractVerifierTest {

	@Test
	public void validate_readFromLongFile() throws Exception {
		// given:
			MockMvcRequestSpecification request = given()
					.header("Accept", "text/html");

		// when:
			ResponseOptions response = given().spec(request)
					.queryParam("test","true")
					.get("/example");

		// then:
			assertThat(response.statusCode()).isEqualTo(200);
			assertThat(response.header("Content-Type")).matches("text/html.*");

		// and:
			assertThat(response.getBody().asByteArray()).isEqualTo(fileToBytes(this, "readFromLongFile_response_longFile.txt"));
	}

}