Ash*_*ell 7 junit spring spring-test-mvc
我在为测试设置会话属性时遇到问题.我正在使用MockMvc来测试对控制器的调用.会话模型上有一个成员属性(表示已登录的人).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"));
}
Run Code Online (Sandbox Code Playgroud)
这是有问题的控制器
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
对于您的测试类,添加@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"));
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5032 次 |
最近记录: |