1
votes

I have a spring boot 1.5.1.RELEASE project using Spock 1.1 for the integration tests. I have a basic controller:

@RestController("/words")
public class WordsController {

   @RequestMapping(method = RequestMethod.GET)
   @ResponseBody
   public ResponseEntity getAllWords() {
      return ResponseEntity
         .status(HttpStatus.OK)
         .contentType(MediaType.APPLICATION_JSON)
         .body("Your list of words go here");
   }
}

I am trying to test the endpoint with:

@ContextConfiguration(classes = MyApplication.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
class WordsControllerTest extends Specification {
RESTClient restClient = new RESTClient("http://localhost:3000", ContentType.JSON)

    def "test the GET endpoint is available"() {
        when:
        def response = restClient.get(
                path: '/words',
                requestContentType: JSON
        )

        then:
        response.status == 200
    }

And here is my Application main class:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

But when I run this, the I get a really weird error:

groovyx.net.http.RESTClient : Error parsing 'application/json' response

groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is 'Y' with an int value of 89 Unable to determine the current character, it is not a string, number, array, or object line number 1 index number 0 Your list of words go here�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ^ at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:206) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:157) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:46) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:384) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:128) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.json.JsonSlurper.parse(JsonSlurper.java:221) ~[groovy-all-2.4.7.jar:2.4.7] at groovyx.net.http.ParserRegistry.parseJSON(ParserRegistry.java:280) ~[http-builder-0.7.1.jar:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40] at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40] at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1215) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1082) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.lang.Closure.call(Closure.java:414) ~[groovy-all-2.4.7.jar:2.4.7] at groovy.lang.Closure.call(Closure.java:430) ~[groovy-all-2.4.7.jar:2.4.7]

Not sure if it makes a difference, but here is part of my gradle file:

compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-devtools')
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
runtime('com.h2database:h2')
compile group: 'org.codehaus.groovy.modules.http-builder', name: 'http-builder', version: '0.7.1'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.spockframework:spock-core:1.1-groovy-2.4-rc-2')
testCompile('org.spockframework:spock-spring:1.1-groovy-2.4-rc-2')
testCompile "org.hamcrest:hamcrest-core:1.2"

I am not sure why the JSON is not being either sent or received properly.

1

1 Answers

4
votes

It's not enough to mark your endpoint as returning application/json media type. You also need to return valid JSON content which:

"Your list of words go here"

is not.

If you need to return a list write:

return ResponseEntity
   .status(HttpStatus.OK)
   .contentType(MediaType.APPLICATION_JSON)
   .body("{\"words\": [\"one\", \"two\"]}");

or a plain message:

return ResponseEntity
   .status(HttpStatus.OK)
   .contentType(MediaType.APPLICATION_JSON)
   .body("{\"msg\": \"content\"");

Spring can automatically map POJO's to JSON payload, have a look at @ResponseBody