【问题标题】:Rest Assured - Expected content-type "JSON" doesn't match actual content-type ""放心 - 预期的内容类型“JSON”与实际的内容类型“”不匹配
【发布时间】:2018-01-17 10:42:16
【问题描述】:

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

@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 对象返回给请求用户。但是,当我执行放心测试时

@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)

我对为什么我的标题在其余测试中没有说明任何内容类型感到困惑和不知所措。我将请求放入我的网络浏览器并选择了 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

关于为什么我的 ContentType 没有被请求的放心测试看到的任何想法?

【问题讨论】:

  • 不确定附加的字符集是否会导致问题....但请尝试使用 contentType("application/json;charset=UTF-8")
  • 我按照您的指示尝试了,但没有成功。如果我做 contentType("") 它工作。但是我不能执行以下 .body() 语句,因为它无法确认 JSON、TEXT、XML。
  • fwiiw,添加 then().log().all() 有助于解决实际服务器响应问题。

标签: java junit content-type rest-assured


【解决方案1】:

将您的ContentType.JSON 替换为"application/json\r\n"。换行和回车无法通过放心来区分,我必须在上一个项目中这样做才能使其正常工作。

【讨论】:

  • 当我遇到问题时,我相信我在 SO 上发现了这个技巧,所以当我找到它时,我可能会标记为骗子。
【解决方案2】:

在接受内容格式中使用application/json 而不是

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

【讨论】:

  • 当他使用他的浏览器时,响应标头指出它正在返回 json,所以它可以工作。是测试不起作用。
猜你喜欢
  • 2015-04-15
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-18
相关资源
最近更新 更多