7

テストのセッション属性の設定に問題があります。MockMvc を使用してコントローラーへの呼び出しをテストしています。セッション モデルには member 属性があります (ログインした人を表します)。SessionModel オブジェクトがセッション属性として追加されます。以下の formBacking メソッドの ModelMap パラメータに入力されることを期待していましたが、ModelMap は常に空です。

コントローラー コードは、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"));

  }

ここに問題のコントローラーがあります

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

}
4

1 に答える 1

3

テストクラスの場合、@WebAppConfigurationアノテーションを追加し、以下も自動配線します。(WebApplicationContextおよび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"));

  }
}
于 2013-01-09T20:56:17.500 に答える