【问题标题】:How can we mock session values (request.getSession() ) in JUnit?我们如何在 JUnit 中模拟会话值(request.getSession())?
【发布时间】:2020-03-02 00:22:42
【问题描述】:
private BankApp processSelection(HttpServletRequest request,
        String[] facets, String selectedText) {
    DebugUtility.debug(LOG, "Enter into JiraController :: processDashBoardPortFolioSelection()");
    BankApp project = new BankApp();
    List<BankApp> dataAgg = (List<BankApp>) request.getSession()
            .getAttribute(PROJECT_WISE_SESSION);
    if (CollectionUtils.isNotEmpty(dataAgg)) {
        List<BankApp> projects = new ArrayList<>();
        for (String currentProjectId : facets) {
            Project currProject = vendorUtils.getProjectDetails(currentProjectId);
            List<SourceProjectAssocation> sourceSystem = currProject.getSourceSytems("JIRA");
            List<String> jiraProjects = new ArrayList<>();
            sourceSystem.stream().forEach(s -> jiraProjects.add(s.getAssociatedJiraProject()));
            for (BankApp projectData : dataAgg) {
                if (jiraProjects.contains(projectData.getProjectKey())) {
                    projects.add(projectData);
                }
            }
        }
        project = processAllProjectsData(projects);
        project.setProjectKey(selectedText);
        project.setProjectsCount(projects.size());
    }
    DebugUtility.debug(LOG, "project in processDashBoardPortFolioSelection :: " + project);
    DebugUtility.debug(LOG, "Exit from JiraController :: processDashBoardPortFolioSelection()");
    return project;
}

我们如何为这个方法编写 junit 测试用例?我在这里面临的挑战是我们需要模拟request.getSession().getAttribute(PROJECT_WISE_SESSION)。所以我尝试了when(request.getSession()).thenReturn(session);,但使用request.getSession(),我得到了null

【问题讨论】:

  • 请提供格式化代码

标签: java spring-boot session junit mockito


【解决方案1】:

试试类似的方法

@Test
public void test() {
    HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
    HttpSession sessionMock = Mockito.mock(HttpServletRequest.class);
    Mockito.when(requestMock.getSession()).thenReturn(sessionMock);

    processSelection(requestMock, <and your facets and selectedText here>)

    //do some asserts/verifies here
}

【讨论】:

  • 感谢 Valentyn Anzhurov 的回复。我正在尝试你的解决方案
  • HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class); HttpServletRequest sessionMock = Mockito.mock(HttpServletRequest.class); Mockito.when(requestMock.getSession()).thenReturn((HttpSession) sessionMock);//好不好
  • @KrishGaur 没有。您不能将 sessionMock(引擎盖下的 HttpServletRequest)转换为 HttpSession。只需使用我的代码
  • @KrishGaur 那又怎样?
  • @Test public void testfacets() throws IOException { when(service.getData(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) 。然后返回(getJiraEntity()); HttpEntity requestEntity = new HttpEntity(constructReqHeaders()); ResponseEntity responseEntity = restTemplate .withBasicAuth(env.getProperty("username"), env.getProperty("password")) .exchange("/api/jira?facets=allProjects&mode=all&selectedtext=AllProjects", HttpMethod.GET,请求实体,对象.类); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 2011-06-25
相关资源
最近更新 更多