我有一个使用Spock 1.1进行集成测试的spring boot 1.5.1.RELEASE项目。我有一个基本的控制器:Spock&Spring Boot集成测试
@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 : 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]
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没有被发送或接收的正确:如果它的确与众不同,但这里是我gradle这个文件的一部分不知道。
谢谢你知道@ResponseBody会自动返回JSON,但我错过了没有返回实际有效的JSON字符串的部分。 – sonoerin
@sonoerin不客气! – Opal