title: How to write unit test case to test secure attribute of session cookie
body: <p>I recently added a piece of code in a servlet which basically adds a 'secure' attribute to the session cookie. </p>
  
  <pre><code>   String encryptedSessionId = encryptionProxy.strongEncrypt(request.getSession().getId());
      response.setHeader("SET-COOKIE", "JSESSIONID=" + encryptedSessionId + "; secure");
  </code></pre>
  
  <p>And I want to write a Junit test case to check whether "secure" attribute is being added to session cookie or not.</p>
  
  <p>I was trying to mock session, request and response object to write a test case but its not working as expected and throwing an NullPointerException</p>
  
  <p>my test case,</p>
  
  <pre><code>    @Test    
  public void testMethod() {
      MockHttpServletRequest request = Mockito.mock(MockHttpServletRequest.class);
      HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
      HttpSession session = new MockHttpSession(null, (String) request.getSession().getAttribute("secure"));
  
      //VcfoWelcomeOptInEnrollmentServlet vcfoWelcomeOptInEnrollmentServlet = Mockito.mock(VcfoWelcomeOptInEnrollmentServlet.class);
  
      //when(session.getAttribute("secure")).thenReturn("secure");
  
      //System.out.println(response.getHeader("SET-COOKIE"));
  
      when(response.getHeader("SET-COOKIE")).thenReturn("secure");
    }
  </code></pre>
  
  <p>I kept the test case as it is so that you can get an idea about the things that I tried so far.</p>
  
  <p>I would like to know whats wrong with my approach? Also, What is the correct approach to write a successful unit test case.</p>
  
  <p>Any help would be appreciated!</p>
  
  <p>I am using Junit 4 and Mockito 2</p>
  
  <p>error stack</p>
  
  <pre><code>java.lang.NullPointerException
  at com.xxx.accountLink.vcfo.client.servlet.ServletTest.testMethod(ServletTest.java:29)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:88)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
  at java.lang.reflect.Method.invoke(Method.java:613)
  at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:316)
  at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
  at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:300)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:288)
  at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
  at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:208)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:147)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:121)
  at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
  at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:123)
  at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
  at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
  at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  </code></pre>