title: How to mock HttpServletRequest with Headers?
body: <p>I am using <a href="https://site.mockito.org" rel="noreferrer">Mockito</a> with <a href="https://junit.org" rel="noreferrer">JUnit</a> to test an application. I need to add headers to an <a href="https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpServletRequest.html" rel="noreferrer"><code>HttpServletRequest</code></a> while mocking. This is the first time I am using mock concept to test the application. How can we set headers to the request object while using this mock concept?</p>
  <h2>Application code</h2>
  <pre><code>@Produces({ MediaType.APPLICATION_JSON })
  @Path(&quot;/devices&quot;)
  public class DvrRestService {
      private static final Logger logger = LoggerFactory.getLogger(DvrRestService.class);
  
      private DvrMiddleService dvrMiddleService;
  
      @Inject
      public DvrRestService(DvrMiddleService dvrMiddleService) {
          this.dvrMiddleService = dvrMiddleService;
      }
  
      @GET 
      @Path(&quot;/{deviceId}/metadata&quot;)
      public Response getDeviceMetadata(@Context HttpServletRequest request, @PathParam(&quot;deviceId&quot;) String deviceId,
          @RequiredSession final Session session) {
  
          try {
              public static String[] REQUEST_HEADERS = { &quot;if-none-match&quot; };
              List&lt;String&gt; requiredHeaders = Lists.newArrayList(REQUEST_HEADERS);
              Map&lt;String, String&gt; headers = new HashMap&lt;String, String&gt;();
  
              Enumeration&lt;String&gt; headerNames = request.getHeaderNames();
              while (headerNames.hasMoreElements()) { // here gettting NullPointerException
  
                  String headerName = headerNames.nextElement();
                  if (requiredHeaders.contains(headerName.toLowerCase())) {
                      String value = request.getHeader(headerName);
                      if (value != null) {
                          headers.put(headerName, value);
                          System.out.println(&quot;headerName: &quot; + headerName + &quot;, Value: &quot; + value);
                      }
                  }
              }
              DvrResponse response = dvrMiddleService.getDeviceMetadata(deviceId.toUpperCase(), getHeaders(request));
              return processResponse(response.statusCode, response.getResponse(), DeviceMetadataResponse.class,
                  response.getHeaders());
          } catch (Exception e) {
              return processErrorResponse(e, new DeviceMetadataResponse(), logger);
          }
  
      }
  }
  </code></pre>
  <h2>Test</h2>
  <pre><code>public class DvrRestServiceTest {
  
      static DvrMiddleService dms;
      static HttpServletRequest request;
      static Session session;
      static DvrRestService drs;
      public static final String DeviceId = &quot;000004D42070&quot;;
      @BeforeClass
      public static void init(){
          dms = mock(DvrMiddleService.class);
          request = mock(HttpServletRequest.class);
          session = mock(Session.class);
          drs = new DvrRestService(dms);
      }
      @Test
      public void getDeviceMetadataTest(){
          Response rs = drs.getDeviceMetadata(request, DeviceId, session);
          assertEquals(Response.Status.OK, rs.getStatus());
      }
  }
  </code></pre>