问题
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;
}
How can we write junit test case for this method? The challenge I'm facing here is we need to mock request.getSession().getAttribute(PROJECT_WISE_SESSION)
. So I tried when(request.getSession()).thenReturn(session);
but with request.getSession()
I'm getting null
.
回答1:
try something like that
@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
}
来源:https://stackoverflow.com/questions/58714540/how-can-we-mock-session-values-request-getsession-in-junit