我在设置测试的 session 属性时遇到问题。我正在使用 MockMvc 来测试对 Controller 的调用。 session 模型有一个成员属性(代表已登录的人)。 SessionModel 对象作为 session 属性添加。我期望将其填充到下面的 formBacking 方法的 ModelMap 参数中,但 ModelMap 始终为空。
Controller 代码在通过 Web 应用程序运行时工作正常,但在 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
@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;
}
}
最佳答案
为您的测试类添加 @WebAppConfiguration
注释并 Autowiring 以下内容。(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"));
}
}
关于spring - 在 Spring 3.2 上设置 JUnit 的 session 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14232833/