1

I am trying to mock the HttpServletRequest and HttpServletResponse objects in order to set parameters and attributes to the request.

I don't have the package MockHttpServletRequest. Is there any way I can get the mock object of HttpServletRequest without using MockHttpServletRequest?

2
  • It seems that you are struggling with mocking ... as said: you better read some tutorials, like: vogella.com/tutorials/Mockito/article.html Commented Aug 28, 2017 at 8:04
  • Simply worked with Spring's mock package. Commented Aug 28, 2017 at 11:12

2 Answers 2

1

You can simply use mockito to mock HttpServletRequest and HttpServletResponse

HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Sign up to request clarification or add additional context in comments.

1 Comment

This is throwing error - java.lang.ExceptionInInitializerError at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17) at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41)
0

I was using the same code as want2learn, but still facing errors. Long story short dependency issue. I was using runner which was dependent on Mockito2 and mocking with the help of Mockito1 or just Mockito causing it to fail. (I am still learning JUnit so I don't know how it works yet). Also, I am doing everything without Spring. If anyone needs example code comment below.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.