rest - setting up MockMVC for Spring Restful Web Service -
so don't think there lot of questions on here on mockmvc spring testing springmvc controllers read through spring tutorial https://spring.io/guides/tutorials/rest/2/ there lot there trying simple on web service single id parameter. controller looked this:
@restcontroller @requestmapping("/demandrest") public class demandservicescontroller { @autowired demandservice demandservice; @requestmapping(value = "/demand/{$id}", method = requestmethod.get, headers= "accept-application/jsson") public demand readdemand(@requestparam(value="id", required=true) string id){ return demandservice.readdemand(new long(id)); }
and wrote unit test uses org.springframework.test.web.servlet.mockmvc , attempt mock out (really stub) service call , assert on status code , getting status code 404. test looks this
import static org.mockito.mockito.when; import static org.springframework.test.web.servlet.request.mockmvcrequestbuilders.get; import static org.springframework.test.web.servlet.result.mockmvcresultmatchers.status; import static org.springframework.test.web.servlet.setup.mockmvcbuilders.standalonesetup; import org.junit.after; import org.junit.before; import org.junit.test; import org.mockito.injectmocks; import org.mockito.mock; import org.mockito.mockitoannotations; import org.springframework.http.mediatype; import org.springframework.http.converter.json.mappingjackson2httpmessageconverter; import org.springframework.test.web.servlet.mockmvc; public class demandservicescontrollertest { mockmvc mockmvc; @injectmocks demandservicescontroller demandservicescontroller = new demandservicescontroller(); @mock demandservice demandservice; @before public void setupunittest() throws exception { mockitoannotations.initmocks(this); this.mockmvc = standalonesetup(demandservicescontroller). setmessageconverters(new mappingjackson2httpmessageconverter()).build(); } @after public void teardown() throws exception { } @test public void testreaddemandstate() throws exception { long id = new long(99); demand demand = new demand(); demand.setdescription("description"); when(demandservice.readdemand(id)).thenreturn(demand ); this.mockmvc.perform(get("/demandrest/demand/{id}",id.tostring()). accept(mediatype.application_json)).andexpect(status().isok()); } }
i couple other things i'll mention before hit post. looking @ example tutorial tried pick stuff use isn't exact copy. 1 big difference had do, , suspect has how in pom testing plugin configured, had instantiate controller example smart enough know create instance. also, example relies on gradle project set , have pom file project. not sure if makes difference. seems new stuff pretty simple example. in advance help.
you should fix requestmapping from
@requestmapping(value = "/demand/{$id}", method = requestmethod.get, headers= "accept-application/jsson")
to
@requestmapping(value = "/demand/{id}", method = requestmethod.get, headers= "accept=application/json")
{$id}
should {id} , headers
have typo , incorrect syntax. perhaps should consider using produces
, consumes
instead of headers
param also.
this should fix 404 status.
Comments
Post a Comment