samedi 3 octobre 2015

Testing controllers in Spring?

I want to write a functional test by calling some controllers.

        @Autowired
        private WebApplicationContext ctx;

        private MockMvc mockMvc;
        ....

        MvcResult mvcResult = mockMvc.perform(get("/perform/1")).andReturn();
        Map<String, Object> model = mvcResult.getModelAndView().getModel();

        //assert....

        MvcResult mvcResult = mockMvc.perform(get("/perform/2")).andReturn();
        Map<String, Object> model = mvcResult.getModelAndView().getModel();

        //assert....

After perform/1 is called a User model is stored in the session:

 @Controller
 @SessionAttributes("user")
 public class CoreController {

I would like to call perform/2 immediate after perform/1 in the same test but it seems that user = null then. Is this normal behavior?

How can I accomplish this? How can I write a functional test with Spring controllers?

Aucun commentaire:

Enregistrer un commentaire