unit testing - Spring controller tests with mocks -
so i'm having issues coming solution test.
this method want test(i'm new this). clears fields on web page each time it's loaded.
@requestmapping("/addaddressform") public string addaddressform(httpsession session) { session.removeattribute("firstname"); session.removeattribute("surname"); session.removeattribute("phone"); session.removeattribute("workno"); session.removeattribute("homeno"); session.removeattribute("address"); session.removeattribute("email"); return "simpleforms/addcontact"; }
and here's have far test
package controllertests; import java.text.attributedcharacteriterator.attribute; import org.junit.before; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.mock.web.mockhttpsession; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.web.webappconfiguration; import org.springframework.test.web.servlet.mockmvc; import org.springframework.test.web.servlet.mvcresult; import org.springframework.test.web.servlet.request.mockhttpservletrequestbuilder; import org.springframework.test.web.servlet.request.mockmvcrequestbuilders; import org.springframework.test.web.servlet.result.mockmvcresulthandlers; import org.springframework.test.web.servlet.setup.mockmvcbuilders; import org.springframework.web.context.webapplicationcontext; import static org.springframework.test.web.servlet.request.mockmvcrequestbuilders.get; import static org.springframework.test.web.servlet.result.mockmvcresultmatchers.status; @runwith(springjunit4classrunner.class) @webappconfiguration @contextconfiguration (classes = {simpleformscontrollertest.class}) public class simpleformscontrollertest { @autowired private webapplicationcontext wac; private mockmvc mockmvc; @before public void setup() { this.mockmvc = mockmvcbuilders.webappcontextsetup(this.wac).build(); } @test public void addaddressform_existingcontactdetailsinform_removalfromsession() throws exception{ mockhttpsession mocksession = new mockhttpsession(); mocksession.putvalue("firstname", "test"); mocksession.putvalue("surname", "test"); mocksession.putvalue("phone", "test"); mocksession.putvalue("workno", "test"); mocksession.putvalue("homeno", "test"); mocksession.putvalue("address", "test"); mocksession.putvalue("email", "test"); mockmvc.perform(get("simpleforms/addaddressform").session(mocksession)); }
as first time i've ever had kind of thing don't have clue go this.
then have assert values, e.g.:
assertnull(mocksession.getattribute("firstname"));
if want make sure case, can do:
assertnotnull(mocksession.getattribute("firstname"));
before fire request.
Comments
Post a Comment