0

In my rest controller class I have the following method

@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;
}

Which specifically states I am returning application/json object to the requesting user. However when I execute the rest-assured test

@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"));
}

I receive the following error

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)

I am confused and at loss as to why my header is stating no content type in the rest test. I put the request into my web browser and selected the Headers tab and the metadata is as follows

Response Headers

Content-Type    application/json;charset=UTF-8
Date    Wed, 17 Jan 2018 10:09:14 GMT
Transfer-Encoding   chunked

Request Headers

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

Any ideas as to why my ContentType isn't being seen by the requesting rest-assured tests?

shirafuno
  • 387
  • 3
  • 9
  • 24
  • not sure if the charset being appended is cause the issue....but pls try with contentType("application/json;charset=UTF-8") – akshaya pandey Jan 17 '18 at 11:02
  • I tried as you instructed but no luck. if I do contentType("") it works. However I cannot then do the following .body() statement as it's unable to confirm JSON, TEXT, XML. – shirafuno Jan 17 '18 at 11:16
  • 3
    Possible duplicate of [setting content type in rest assured](https://stackoverflow.com/questions/26976624/setting-content-type-in-rest-assured) – Turtle Jan 24 '18 at 11:14
  • fwiiw, adding `then().log().all()` helps troubleshooting actual server response. – Ahmad Abdelghany Jul 14 '20 at 09:35

3 Answers3

2

Replace your ContentType.JSON by "application/json\r\n". The line feed and carriage return cannot be differenciated by restassured, I had to do this in my last project to make it work.

Turtle
  • 1,626
  • 16
  • 26
  • When I had the problem, I found this trick on SO I believe, so I may tag as a dupe when I find it. – Turtle Jan 24 '18 at 11:14
0

When I had this, the reason was because my mocked service was not returning a response. Therefore the controller was not returning a response so the content type was blank.

So my answer when I had this problem was to actually return some content from my controller!

Mark
  • 600
  • 3
  • 9
-3

use application/json in accept content format instead of

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

Jabongg
  • 2,099
  • 2
  • 15
  • 32
  • When he uses his browser, the response header states it's returning json, so it works. It's the test that doesn't work. – Turtle Jan 17 '18 at 10:50