unit testing - java.lang.AssertionError: Status expected:<200> but was:<404> -
i know there many similar posts question. tried implement not working me. please me why getting error java.lang.assertionerror: status expected:<200> was:<404>
i tried implement mediatype.application_json annotation @enablewebmvc not working
do need include headers working? please let me know
code have written is:
controller class:
@enablewebmvc @controller public class controller { @requestmapping(value = "/app/data", method = requestmethod.post, produces = mediatype.application_json_value, consumes = mediatype.application_json_value) public responseentity<responsedata> datainquiry( @requestbody requestdata requestdata, @requestheader(value = constants.id, required = false) string transactionid) { //do action return new responseentity<responsedata>(responsedata, headers, httpstatus.ok); }
controllertest class:
@runwith(springjunit4classrunner.class) @contextconfiguration({"classpath*:spring/beanrefcontext.xml"}) @webappconfiguration public class controllertest{ public static final mediatype application_json_utf8 = new mediatype(mediatype.application_json.gettype(), mediatype.application_json.getsubtype(), charset.forname("utf8")); private mockmvc mockmvc; @autowired webapplicationcontext wac; objectmapper mapper; annotationmethodhandleradapter adapter; mockhttpservletrequest request; mockhttpservletresponse response; @before public void setup() { system.out.println("before method execution in commoninquirycontrollertest class "); //this.mockmvc = mockmvcbuilders.webappcontextsetup(this.wac).build(); mockitoannotations.initmocks(this); this.mockmvc = mockmvcbuilders.webappcontextsetup(this.wac).dispatchoptions(true).build(); adapter = new annotationmethodhandleradapter(); request = new mockhttpservletrequest(); response = new mockhttpservletresponse(); mapper = new objectmapper(); } @test public void inquirydatatest() throws exception, jsonprocessingexception { requestdata anobject = new requestdata(); anobject.setid("1234"); anobject.setqualifier("somedata"); mapper = new objectmapper(); mapper.configure(serializationfeature.wrap_root_value, false); objectwriter ow = mapper.writer().withdefaultprettyprinter(); string requestjson=ow.writevalueasstring(anobject ); assertnotnull(anobject.getid()); assertnotnull(anobject.getqualifier()); resultactions resultactions = mockmvc .perform(post("/app/data") .contenttype(mediatype.application_json) .accept(mediatype.application_json) .content(mapper.writevalueasbytes(requestjson))); resultactions.andexpect(status().isok()); //this print response json string resultactions.anddo(mockmvcresulthandlers.print()); assert.assertequals(200, response.getstatus()); }
xml info: beancontext.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:encryption="http://www.jasypt.org/schema/encryption" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.jasypt.org/schema/encryption http://www.jasypt.org/schema/encryption/jasypt-spring3-encryption-1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <import resource="classpath:core-application-context.xml"/> <import resource="classpath:region-context.xml"/> <jee:jndi-lookup id="somedatasourceid" jndi-name="some name" proxy-interface="javax.sql.datasource"/> <!-- enable configuration of transactional behavior based on annotations --> <tx:annotation-driven/> <bean class="org.springframework.dao.annotation.persistenceexceptiontranslationpostprocessor"/> <!-- platformtransactionmanager still required --> <bean id="transactionmanager" class="org.springframework.transaction.jta.websphereuowtransactionmanager"/> </beans>
in region-context.xml:
<?xml version="1.0" encoding="utf-8"?> <beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <!-- import context file , pass requisite properties , data source named datasource --> <context:component-scan base-package="com.java.geek"/> </beans>
even though enable webmvc, need scan controller auto register controller & url mappings.component-scan scans packages find , register beans within application context.
<context:component-scan base-package="com.mycompany.xyz" />
i doubt component scanning missing in context xml.if add statement in beanrefcontext.xml.
Comments
Post a Comment