i'm using spring 3.2.11.release , junit 4.11. in particular spring controller, have method ends thusly ...
return new modelandview(new redirectview(redirecturi, true));
in junit test, how verify return submission controller in redirectview returned? used use org.springframework.test.web.abstractmodelandviewtests.assertviewname, returns "null", when non-empty modelandview object returned. here how i'm constructing junit test ...
request.setrequesturi(“/mypage/launch"); request.setmethod("post"); … final object handler = handlermapping.gethandler(request).gethandler(); final modelandview mav = handleradapter.handle(request, response, handler); assertviewname(mav, "redirect:/landing");
any on how verify redirectview comes proper value appreciatd,
as koiter said, consider moving spring-test , mockmvc
it providers methods test controllers , requests/reponses in declarative way
you need @autowired webapplicationcontext wac;
and on @before
method setup use @webappconfiguration
of class.
you'll end
@contextconfiguration("youconfighere.xml") //or (classes = {yourclassconfig.class} @runwith(springjunit4classrunner.class) @webappconfiguration public class mycontrollertests { @autowired webapplicationcontext wac private mockmvc mockmvc; @before public void setup() { //setup mock use web context this.mockmvc = mockmvcbuilders.webappcontextsetup(wac).build(); } }
then need use mockmvcresultmatchers assert things
@test public void testmyredirect() throws exception { mockmvc.perform(post("you/url/") .andexpect(status().isok()) .andexpect(redirecturl("you/redirect") }
note: post(), status() isok() redirecturl()
statics imports mockmvcresultmatchers
see more can match here