2017-02-10 1 views
1

통합 테스트를 위해 Spock 1.1을 사용하는 스프링 부트 1.5.1.RELEASE 프로젝트가 있습니다. 내가 가진 엔드 포인트 테스트하기 위해 노력하고스팍 및 스프링 부트 통합 테스트

@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을 파일의 일부인 경우확실하지.

답변

4

application/json 미디어 유형을 반환하는 것으로 끝점을 표시하는 것만으로는 충분하지 않습니다.

"Your list of words go here" 

가되지 않습니다 : 당신은 또한 유효 JSON의 내용을 반환해야합니다.

당신은 목록 쓰기 반환해야하는 경우,

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

봄이 자동으로 JSON 페이로드에 POJO의 매핑 할 수 있습니다 @ResponseBody

+0

감사를 보라 :

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

또는 일반 메시지 당신은 @ResponseBody가 자동으로 JSON을 반환한다는 것을 알았지 만, 실제 유효한 JSON 문자열을 반환하지 않는다는 부분을 놓쳤습니다. – sonoerin

+0

@sonoerin 오신 것을 환영합니다! – Opal

관련 문제