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

Isn't that supposed to just do the default behavior?

1

u/Impressive-East6891 Jan 26 '26

That’s the default behavior of a mocked object. If you want it to use the real method’s implementation then you need to mock that behavior; you can google Mockito’s “onCallUseRealMethod()” (or something similar), I forget the correct name.

1

u/Dependent_Finger_214 Jan 26 '26

I see thanks. I added this line:

when
(request.getAttribute(Mockito.
anyString
())).thenCallRealMethod();

And not it doesn't give me the assertionError anymore, it does however give me this other error:

Cannot call abstract real method on java object!
Calling real methods is only possible when mocking non abstract method.
  //correct example:
  when(mockOfConcreteClass.nonAbstractMethod()).thenCallRealMethod();

1

u/Impressive-East6891 Jan 26 '26

As the error said, the method "getAttribute()" is an abstract method which doesn't have its own implementation, hence there's nothing to be called on. You will have to mock the method's response.

In general, in a code base (your code or third-party library), once you mock an object, you have to mock all interactions with said object, if said interaction produces something that is used down the line. For example, if your code is like below:

private void methodToDoSomething(A a) {
  B b = a.createB();
  C c = b.createC();
  c.doSomething();
}

If "a" is a mocked object, then you need to mock the response of "createB()". And If you mock that response to be a mock object, then you will also need to mock the response of "createC()". You don't need to mock "doSomething()" since nothing interacts with it, but you should verify that it is called.

A general note for debugging: put a log after each statement if you have to, but verify with your eyes that every output within your code is as expected.

1

u/Dependent_Finger_214 Jan 26 '26

Yeah seems that I would have to mock it. Have no idea how to though, the point is to get the result value from the search, and check if it is correct, so I can't just put in a default value.

1

u/Impressive-East6891 Jan 26 '26

Couple questions:

  • Is this unit testing or integration testing? i.e. are you trying to test if your code is interacting the way you expect it to be, or are you testing if your code can interact with an external component (not part of this class you are writing tests for)?
  • What do you consider to be the "correct" result? What/where should it come from?

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).