2013-01-09 2 views
7

테스트를 위해 세션 속성을 설정하는 데 문제가 있습니다. 컨트롤러에 대한 호출을 테스트하기 위해 MockMvc를 사용하고 있습니다. 세션 모델에는 구성원 속성 (로그인 한 사용자를 나타냄)이 있습니다. SessionModel 객체는 세션 속성으로 추가됩니다. 아래의 formBacking 메서드에 대한 ModelMap 매개 변수가 채워질 것으로 예상했지만 ModelMap은 항상 비어 있습니다.Spring 3.2의 JUnits에 대한 세션 속성 설정

컨트롤러 코드는 webapp에서는 실행되지만 JUnit에서는 정상적으로 작동하지 않습니다. 내가 뭘 잘못했는지 알기나 해? 여기

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

    } 

이며, 여기에 테스트 클래스는 @WebAppConfiguration 주석을 추가하고뿐만 아니라 다음을 autowire하기 위해 질문

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

} 

답변

3

의 컨트롤러입니다. (WebApplicationContextMockHttpSession)

@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

감사 마누엘. 미안 해요, 나는 그 annotations/autowired 변수가 있지만, 불행히도 그것은 여전히 ​​나를 위해 작동하지 않습니다. 나는 또한 sessionAttr 대신 위에 명시된 세션을 명시 적으로 설정하려고 시도했다. –