Announcement Announcement Module
Collapse
No announcement yet.
[JUnit] Setting session attributes for tests on Spring 3.2 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [JUnit] Setting session attributes for tests on Spring 3.2

    Hi Folks,

    I'm having trouble setting session attributes for a test. I am using MockMvc to test calls to a controller. The session model has a member attribute on it (representing the person who has logged in). The SessionModel object is added as a session attribute. I was expecting it to be populated in the ModelMap parameter to formBacking method below, but the ModelMap is always empty.

    The controller code works fine when running through the webapp, but not in the JUnit. Any idea what I could be doing wrong?

    Thanks for your help!
    Ash

    Here is my JUnit Test



    Code:
    @Test
          public void testUnitCreatePostSuccess() throws Exception {
            
            UnitCreateModel expected = new UnitCreateModel();
            expected.reset();
            expected.getUnit().setName("Bob");
            
            SessionModel sm = new SessionModel();
            sm.setMember(getDefaultMember());
            
            this.mockMvc.perform(
                post("/units/create")
                .param("unit.name", "Bob")
                .sessionAttr(SessionModel.KEY, sm))
                .andExpect(status().isOk())
                .andExpect(model().attribute("unitCreateModel", expected))
                .andExpect(view().name("tiles.content.unit.create"));
            
          }

    and here is the controller in question


    Code:
    @Controller
        @SessionAttributes({ SessionModel.KEY, UnitCreateModel.KEY })
        @RequestMapping("/units")
        public class UnitCreateController extends ABaseController {
          
          private static final String CREATE = "tiles.content.unit.create";
        
          @Autowired
          private IUnitMemberService unitMemberService;
        
          @Autowired
          private IUnitService unitService;
        
          @ModelAttribute
          public void formBacking(ModelMap model) {
            
            SessionModel instanceSessionModel = new SessionModel();
            instanceSessionModel.retrieveOrCreate(model);
            
            UnitCreateModel instanceModel = new UnitCreateModel();
            instanceModel.retrieveOrCreate(model);
          }
          
          @RequestMapping(value = "/create", method = RequestMethod.GET)
          public String onCreate(
              @ModelAttribute(UnitCreateModel.KEY) UnitCreateModel model, 
              @ModelAttribute(SessionModel.KEY) SessionModel sessionModel) {
            
            model.reset();
            
            return CREATE;
          }
          
          @RequestMapping(value = "/create", method = RequestMethod.POST)
          public String onCreatePost(
              @ModelAttribute(SessionModel.KEY) SessionModel sessionModel,
              @Valid @ModelAttribute(UnitCreateModel.KEY) UnitCreateModel model, 
              BindingResult result) throws ServiceRecoverableException {
        
            if (result.hasErrors()){
              return CREATE;
            }
            
            long memberId = sessionModel.getMember().getId();
        
            long unitId = unitService.create(model.getUnit());
            unitMemberService.addMemberToUnit(memberId, unitId, true);
        
            return CREATE;
          }
          
        }
Working...
X