Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 放心-预期的内容类型”;JSON";不';“与实际内容类型不匹配”&引用;_Java_Junit_Content Type_Rest Assured - Fatal编程技术网

Java 放心-预期的内容类型”;JSON";不';“与实际内容类型不匹配”&引用;

Java 放心-预期的内容类型”;JSON";不';“与实际内容类型不匹配”&引用;,java,junit,content-type,rest-assured,Java,Junit,Content Type,Rest Assured,在我的rest控制器类中,我有以下方法 @RequestMapping(value = "/film", method = RequestMethod.GET, produces = "application/json") public Film getFilm(@RequestParam("search") String filmSearch){ FilmDomain filmDomain = new FilmDomain(); Film film = filmDomain.g

在我的rest控制器类中,我有以下方法

@RequestMapping(value = "/film", method = RequestMethod.GET, produces = "application/json")
public Film getFilm(@RequestParam("search") String filmSearch){
    FilmDomain filmDomain = new FilmDomain();
    Film film = filmDomain.getCurrentFilm(filmSearch);
    return film;
}
这特别说明我正在将application/json对象返回给请求用户。但是,当我执行rest-assured测试时

@Test
public void test_specified_film_is_retrieved(){
    given().when().get("/view/film?search=The%20Godfather").then().contentType(ContentType.JSON).body("filmTitle", equalTo("The Godfather"));
}
我收到以下错误

java.lang.AssertionError: 1 expectation failed.
Expected content-type "JSON" doesn't match actual content-type "".

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:471)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:636)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at io.restassured.internal.ResponseSpecificationImpl.contentType(ResponseSpecificationImpl.groovy:399)
at io.restassured.internal.ValidatableResponseOptionsImpl.contentType(ValidatableResponseOptionsImpl.java:244)
at restcontrollerapitests.FilmInfoControllerTest.test_specified_film_is_retrieved(FilmInfoControllerTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
我很困惑,不知道为什么我的标题在rest测试中没有说明任何内容类型。我将请求放入我的web浏览器并选择Headers选项卡,元数据如下所示

响应标题

Content-Type    application/json;charset=UTF-8
Date    Wed, 17 Jan 2018 10:09:14 GMT
Transfer-Encoding   chunked
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-GB,en;q=0.5
Connection  keep-alive
Cookie  jenkins-timestamper-offset=0
Host    localhost:8080
Upgrade-Insecure-Requests   1
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0)     Gecko/20100101 Firefox/57.0
请求标题

Content-Type    application/json;charset=UTF-8
Date    Wed, 17 Jan 2018 10:09:14 GMT
Transfer-Encoding   chunked
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-GB,en;q=0.5
Connection  keep-alive
Cookie  jenkins-timestamper-offset=0
Host    localhost:8080
Upgrade-Insecure-Requests   1
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0)     Gecko/20100101 Firefox/57.0

关于为什么我的ContentType没有被请求的rest-assured测试看到,有什么想法吗?

使用accept-content格式的
application/json
,而不是


接受text/html、
application/xhtml+xml
、application/xml;q=0.9,/;q=0.8

将您的
ContentType.JSON
替换为
“application/JSON\r\n”
。换行符和回车符不能通过restasured来区分,我必须在上一个项目中这样做才能使它工作。

当他使用浏览器时,响应标题说明它正在返回json,所以它可以工作。测试不起作用。不确定追加的字符集是否是问题的原因……但请尝试使用contentType(“application/json;charset=UTF-8”),我按照您的指示进行了尝试,但没有成功。如果我使用contentType(“”),它会工作。但是,我无法执行下面的.body()语句,因为它无法确认JSON、文本和XML。fwiiw可能重复,添加
then().log().all()
有助于解决实际服务器响应问题。当我遇到问题时,我发现了这个技巧,所以我相信,当我找到它时,我可能会将其标记为重复。