2013-01-09 12 views
7

Ho problemi a impostare gli attributi di sessione per un test. Sto usando MockMvc per testare le chiamate a un controller. Il modello di sessione ha un attributo membro su di esso (che rappresenta la persona che ha effettuato l'accesso). L'oggetto SessionModel viene aggiunto come attributo di sessione. Mi aspettavo che venisse compilato nel parametro ModelMap per formattare il metodo di registrazione di seguito, ma ModelMap è sempre vuoto.Impostazione degli attributi di sessione per JUnits nella primavera 3.2

Il codice del controller funziona correttamente quando si esegue l'applicazione Web, ma non in JUnit. Qualche idea su cosa potrei fare male?

Ecco il mio test JUnit

@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")); 

    } 

e qui è il controller in questione

@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; 
    } 

} 

risposta

3

Per la vostra classe di test aggiungere il @WebAppConfiguration annotazione e autowire il seguente pure. (WebApplicationContext e MockHttpSession)

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration({ "classpath:springDispatcher-servlet.xml" }) 
public class MySessionControllerTest { 
    @Autowired WebApplicationContext wac; 
    @Autowired MockHttpSession session; 

    private MockMvc mockMvc; 

    @Before 
    public void setup() { 
     this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); 
    } 

    @Test 
    public void testUnitCreatePostSuccess() throws Exception { 
     UnitCreateModel expected = new UnitCreateModel(); 
     expected.reset(); 
     expected.getUnit().setName("Bob"); 

     SessionModel sm = new SessionModel(); 
     sm.setMember(getDefaultMember()); 
     session.setAttribute(SessionModel.KEY, sm); 
     this.mockMvc.perform(
      post("/units/create") 
      .session(session) 
      .param("unit.name", "Bob") 
      .andExpect(status().isOk()) 
      .andExpect(model().attribute("unitCreateModel", expected)) 
      .andExpect(view().name("tiles.content.unit.create")); 

    } 
} 
+0

Grazie Manuel. Scusa avrei dovuto dire, ho quelle annotazioni/variabili autowired, ma sfortunatamente non funziona ancora per me. Ho anche provato ad impostare la sessione in modo esplicito come hai sopra (invece di sessionAttr) –

Problemi correlati