0

我需要运行一个 JUnit vs Spring MVC 测试用例,其中的先决条件包括某些数据存在于 HTTP 中Session。最重要的是我不能连接一个session-scoped bean:我必须访问httpServletContext.getSession().

在展示代码之前,让我解释一下。我需要测试的控制器假定某个数据存储在会话中,否则会引发异常。这是目前正确的行为,因为在没有会话的情况下永远不会调用该控制器,并且会话始终在登录时使用应用程序数据进行初始化。显然控制器处于安全状态。

在我的测试中,我只需要根据请求参数测试这个控制器是返回重定向还是 404 未找到。

我想建立我的测试用例,例如

@Autowired
private HttpServletRequest httpServletRequest;

@Autowired
private ModuleManager moduleManager;

@Autowired
private WebApplicationContext webApplicationContext;

private MenuItem rootMenu;

private MockMvc mockMvc;


@Before
public void setUp() throws Exception
{

    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                             // No asserzioni
                             .build();

    rootMenu = moduleManager.getRootMenu()
                            .clone();
    httpServletRequest.getSession()
                      .setAttribute(MenuItem.SESSION_KEY, rootMenu);

    assertNotNull(rootMenu.getDescendant(existingSelectedMenu));
    assertNull(rootMenu.getDescendant(notExistingMenu));

}

@Test
public void testNavigate() throws Exception
{

    mockMvc.perform(get("/common/navigate?target=" + existingSelectedMenu))
           .andExpect(status().is3xxRedirection());

    assertNotSelected(rootMenu, existingSelectedMenu);

    mockMvc.perform(get("/common/navigate?target=" + notExistingMenu))
           .andExpect(status().is4xxClientError());

}

部分代码是真正不言自明的。无论如何,我希望/common/navigate使用我存储在会话中的值。像这样

@RequestMapping(value = "/common/navigate",
        method = RequestMethod.GET)
public String navigate(@RequestParam("target") String target) throws NotFoundException
{

    MenuItem rootMenu = (MenuItem) httpServletRequest.getSession()
                                               .getAttribute(MenuItem.SESSION_KEY);
    if (rootMenu == null)
        throw new RuntimeException("Menu not found in session"); //Never happens

    MenuItem menuItem = rootMenu.getAndSelect(target);
    if (menuItem == null)
        throw new NotFoundException(MenuItem.class, target); //Expected

    return "redirect:" + menuItem.getUrl();
}

现在猜。当我运行我的代码时会发生什么?

RuntimeException 在我评论的行中抛出,因为在会话中找不到菜单对象

显然这个问题现在是隐含的,但我仍然会写它:如何将数据注入 Session 对象,以便被测控制器将它们作为前提条件可用?

4

1 回答 1

0

现在自己找到了解决方案。

问题是会话本身也必须被嘲笑。Spring 提供了一个可以MockHttpSession解决问题的类。它可以预先填充所有前置条件,必须传递给每个MockMvc请求,以便模拟将会话连接到(模拟的)servlet 上下文。

以下代码初始化会话

    mockHttpSession = new MockHttpSession(webApplicationContext.getServletContext());

    mockHttpSession.setAttribute(MenuItem.SESSION_KEY, rootMenu);

以下使用连接到它的模拟会话执行请求

mockMvc.perform(get("/common/navigate?target=" + existingSelectedMenu).session(mockHttpSession))
           .andExpect(status().is3xxRedirection());
于 2015-08-21T12:54:43.487 回答