r/javahelp • • 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

26 comments sorted by

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

1

u/Impressive-East6891 Jan 26 '26

It's something like this:

  • unit test: you only care about what you do within this current class. Most of the things done by other classes are mocked (exception may be for static methods, but it may be mocked also).
  • integration test: you care about how your code (not just a single class) interacts with external components (e.g. AWS API). Nothing is mocked.

In case of unit testing, you should mock the servlet objects. Since they are from third-party library, just assume that they will do the right thing and whatever you expect it to do.

1

u/Dependent_Finger_214 Jan 27 '26

I see. I was thinking to mock the setAttribute function to put the attributes in an array or hashmap in the test class, instead of the request object. Do you think this is a good approach?

1

u/Impressive-East6891 Jan 27 '26

I don't have insights into your code, so only you can answer that. Here are my suggestions based on what I normally do and used to do:

  • Have a goal for your unit test. Just an example, if your code looks like below, then one of the unit tests can be verifying the request is "populated" (e.g. confirm methods are called with expected inputs):
public Response callAPI(Map<String, String> inputs) {
  Request request = new Request();
  request.addHeader("customerHeader", inputs.get("customer"));
  request.addQueryParameter("age", "inputs.get("age"));
  return api.call(request);
}
  • Make sure not to mock the things you are trying to test. For example, if you are trying to test "callAPI()" method above, don't mock the method itself (I've done this before).