Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create HttpServletResponse for unit tests in Spring?

How to create HttpServletResponse for unit tests, where I can write output stream and headers(not MockHttpServletResponse)? In assert block I expect to compare OutputStream and headers with etalon httpServletResponse.

I used Spring4, java8 and EasyMock lib.

like image 923
Roberto Avatar asked Aug 08 '18 05:08

Roberto


People also ask

How do you define HttpServletResponse?

Defines an HTTP servlet response that a servlet running on a Web server sends to a client using HTTP. This interface allows the servlet's service method to access HTTP headers and return data to its client. The servlet engine implements this interface.

What is HttpServletResponse in spring boot?

The HttpServletResponse interface enables a servlet to formulate an HTTP response to a client. The response object encapsulates all information to be returned from the server to the client.


1 Answers

Below way by using EasyMock

 HttpServletRequest mockRequest = EasyMock.createMock(HttpServletRequest.class);
 HttpServletResponse mockResponse = EasyMock.createMock(HttpServletResponse.class);

Using spring mock class

import  org.springframework.mock.web.MockHttpServletResponse;
import  org.springframework.mock.web.MockHttpServletRequest;

HttpServletRequest httpServletRequest = new MockHttpServletRequest();
HttpServletResponse httpServletResponse = new MockHttpServletResponse();
like image 118
Amit Kumar Lal Avatar answered Oct 16 '22 14:10

Amit Kumar Lal