私は、統合テストに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ファイルの一部である場合わかりません。
感謝を見て、JSONペイロードにPOJOのをマッピングすることができますあなたは@ResponseBodyが自動的にJSONを返すことは知っていましたが、実際の有効なJSON文字列を返さないことについては忘れました。 – sonoerin
@sonoerinよろしくお願いします! – Opal