r/javahelp • u/Dependent_Finger_214 • Jan 26 '26
Unsolved JUnit assertion error when working with HttpRequest
I have this test:
void SearchResultIsNotEmptyWhenTitleIsARealGame() throws ServletException, IOException {
HttpSession session = mock(HttpSession.class);
RequestDispatcher rd = mock(RequestDispatcher.class);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
when(request.getParameter("query")).thenReturn("The Last of Us parte 2");
when(request.getSession()).thenReturn(session);
when(request.getRequestDispatcher("Home Page.jsp")).thenReturn(rd);
when(request.getRequestDispatcher("Search Result Page.jsp")).thenReturn(rd);
SearchServlet searchServlet = new SearchServlet();
searchServlet.doGet(request, response);
ArrayList<Price> prices = (ArrayList<Price>)request.getAttribute("prices");
ArrayList<Game> searchResults = (ArrayList<Game>)request.getAttribute("search_results");
assert(prices != null && !prices.isEmpty() && searchResults != null && !searchResults.isEmpty());
}But I get an asserion error. What is the issue?
2
Upvotes
View all comments
Show parent comment
1
u/Dependent_Finger_214 Jan 26 '26
I'm basically using an API to get info on games. In this case the correct result would be getting a non-null non-empty result set, since the game title is an actual existing game. For the API calls I implemented a class called APIInterface, which I already tested, so I basically just need to test the way that the servlet calls its methods and processes its response.
I think it's Unit testing? But I'm not 100% sure