Groovy 斯波克公司;Spring引导集成测试

Groovy 斯波克公司;Spring引导集成测试,groovy,spring-boot,spock,Groovy,Spring Boot,Spock,我有一个SpringBoot1.5.1.RELEASE项目,使用Spock1.1进行集成测试。我有一个基本控制器: @RestController("/words") public class WordsController { @RequestMapping(method = RequestMethod.GET) @ResponseBody public ResponseEntity getAllWords() { return ResponseEntity

我有一个SpringBoot1.5.1.RELEASE项目,使用Spock1.1进行集成测试。我有一个基本控制器:

@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");
   }
}
我正在尝试使用以下方法测试端点:

@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
    }
这是我的应用程序主类:

@SpringBootApplication
public class MyApplication {

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

}
但是当我运行这个时,我得到一个非常奇怪的错误:

groovyx.net.http.RESTClient:解析时出错 “应用程序/json”响应

groovy.json.JsonException:无法确定当前字符, 它不是字符串、数字、数组或对象

当前读取的字符为“Y”,int值为89,无法读取 确定当前字符,它不是字符串、数字、数组或字符串 对象行编号1索引编号0您的单词列表 在这里�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ^在 groovy.json.internal.jsonparsercharray.decodeValueInternal(jsonparsercharray.java:206) ~[groovy-all-2.4.7.jar:2.4.7]at groovy.json.internal.jsonparsercharray.decodeValue(jsonparsercharray.java:157) ~[groovy-all-2.4.7.jar:2.4.7]at groovy.json.internal.jsonparsercharray.decodeFromChars(jsonparsercharray.java:46) ~[groovy-all-2.4.7.jar:2.4.7]at groovy.json.internal.jsonparsercharray.parse(jsonparsercharray.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.parserjson(ParserRegistry.java:280) ~[http-builder-0.7.1.jar:na]at sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) ~(na:1.8.0_40)at invoke(NativeMethodAccessorImpl.java:62) ~(na:1.8.0_40)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 位于java.lang.reflect.Method.invoke(Method.java:497)的~[na:1.8.040] ~(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 call(Closure.java:414) ~[groovy-all-2.4.7.jar:2.4.7]at call(Closure.java:430) ~[groovy-all-2.4.7.jar:2.4.7]

不确定这是否有区别,但这是我的gradle文件的一部分:

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"

我不知道为什么JSON没有被正确发送或接收

仅将端点标记为返回
application/json
媒体类型是不够的。您还需要返回有效的JSON内容,其中:

"Your list of words go here"
事实并非如此

如果需要返回列表,请执行以下操作:

return ResponseEntity
   .status(HttpStatus.OK)
   .contentType(MediaType.APPLICATION_JSON)
   .body("{\"words\": [\"one\", \"two\"]}");
或者一个简单的信息:

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

Spring可以自动将POJO映射到JSON有效负载,查看一下

仅将端点标记为返回
application/JSON
媒体类型是不够的。您还需要返回有效的JSON内容,其中:

"Your list of words go here"
事实并非如此

如果需要返回列表,请执行以下操作:

return ResponseEntity
   .status(HttpStatus.OK)
   .contentType(MediaType.APPLICATION_JSON)
   .body("{\"words\": [\"one\", \"two\"]}");
或者一个简单的信息:

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

Spring可以自动将POJO映射到JSON有效负载,看一看

谢谢,我知道@ResponseBody会自动返回JSON,但我错过了没有返回实际有效JSON字符串的部分。谢谢,我知道@ResponseBody会自动返回JSON,但我忽略了不返回实际有效JSON字符串的部分。