title: Mocking a servlet, How do I go about mocking this correctly?
body: <p>Im having trouble testing this. </p>
  
  <p>the line 
      ServletContext servletContext = getServletContext();
  is giving me grief, should this be mocked and if so how would I go about it?
   The error im getting is from it is.
        java.lang.IllegalStateException: ServletConfig has not been initialized</p>
  
  <p>My code is</p>
  
  <pre><code>  public class StockSearchServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException {
      String dateOut = "yyyy/MM/dd";        
      DateTimeFormatter ldtf = DateTimeFormat.forPattern(dateOut);
      String symbol = request.getParameter("symbol");
      String from = request.getParameter("from");
      DateTime fromDate = new DateTime(ldtf.parseDateTime(from));
      String until = request.getParameter("until");
      DateTime untilDate = new DateTime(ldtf.parseDateTime(until));
      String interval = request.getParameter("interval");
      HttpSession session = request.getSession();
      StockQuery stockQuery = null;
      try {
          stockQuery = new StockQuery(symbol,from,until);
          stockQuery.setInterval(interval);
      } catch (ParseException ex) {
          Logger.getLogger(StockSearchServlet.class.getName()).log(Level.SEVERE, null, ex);
      }
      session.setAttribute("stockQuery", stockQuery);        
      StockService stockService = ServiceFactory.getStockServiceInstance();
      List&lt;StockQuote&gt; quotes = new ArrayList&lt;StockQuote&gt;();
  
      StringBuilder stringBuilder = new StringBuilder();
  
      try {
          quotes = stockService.getQuote(symbol,fromDate, untilDate ,StockService.getIntervalEnum(interval));
      } catch (StockServiceException ex) {
          Logger.getLogger(StockSearchServlet.class.getName()).log(Level.SEVERE, null, ex);
      }
      for (StockQuote stockQuote : quotes) {
          stringBuilder.append("&lt;br&gt;"+ stockQuote.getSymbol() + " " + stockQuote.getPrice() + " " + stockQuote.getDate());
      }
      String outputStockString = stringBuilder.toString();
      session.setAttribute("outputStockString", outputStockString);
  
      ServletContext servletContext = getServletContext();
      RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/stockquoteResults.jsp");
      dispatcher.forward(request, response);
  }
  </code></pre>
  
  <p>This is my current test im using</p>
  
  <pre><code>  @Test
    public void testDoPost() throws IOException, ServletException {
      String symbol = "GOOG";
      String from = "2015/10/05";
      String until = "2015/10/09";
      String interval = "day";
  
      StockSearchServlet stockSearchServlet = new StockSearchServlet();
      HttpServletRequest request = mock(HttpServletRequest.class);
      when(request.getParameter("symbol")).thenReturn(symbol);
      when(request.getParameter("from")).thenReturn(from);
      when(request.getParameter("until")).thenReturn(until);
      when(request.getParameter("interval")).thenReturn(interval);
      when(request.getSession()).thenReturn(new MySession());
      HttpServletResponse response = mock(HttpServletResponse.class);
      //when(request.getSession()).thenReturn(new MySession());
      new StockSearchServlet().doPost(request, response);
      //stockSearchServlet.doPost(request, response);
  
      String outputString = (String) request.getSession().getAttribute("outputStockString");
      assertEquals("verify state", outputString.length(), 1);
  }
  </code></pre>
  
  <p>and this class is part of the test class. </p>
  
  <pre><code>   class MySession implements HttpSession {
      private Map&lt;String, Object&gt; attributes = new HashMap&lt;String, Object&gt;();
  
      @Override
      public long getCreationTime() {
          return 0;
      }
  
      @Override
      public String getId() {
          return null;
      }
  
      @Override
      public long getLastAccessedTime() {
          return 0;
      }
  
      @Override
      public ServletContext getServletContext() {
          return new ServletContext() {
              @Override
              public String getContextPath() {
                  return null;
              }
              @Override
              public ServletContext getContext(String s) {
                  return null;
              }
              @Override
              public int getMajorVersion() {
                  return 0;
              }
              @Override
              public int getMinorVersion() {
                  return 0;
              }
              @Override
              public int getEffectiveMajorVersion() {
                  return 0;
              }
              @Override
              public int getEffectiveMinorVersion() {
                  return 0;
              }
              @Override
              public String getMimeType(String s) {
                  return null;
              }
              @Override
              public Set&lt;String&gt; getResourcePaths(String s) {
                  return null;
              }
              @Override
              public URL getResource(String s) throws MalformedURLException {
                  return null;
              }
              @Override
              public InputStream getResourceAsStream(String s) {
                  return null;
              }
              @Override
              public RequestDispatcher getRequestDispatcher(String s) {
                  return new RequestDispatcher() {
                      @Override
                      public void forward(ServletRequest servletRequest, ServletResponse servletResponse)
                              throws ServletException, IOException {
                      }
                      @Override            
                      public void include(ServletRequest servletRequest, ServletResponse servletResponse)
                              throws ServletException, IOException {
                      }
                  };
              }
              @Override
              public RequestDispatcher getNamedDispatcher(String s) {
                  return null;
              }
              @Override
              public Servlet getServlet(String s) throws ServletException {
                  return null;
              }
              @Override
              public Enumeration&lt;Servlet&gt; getServlets() {
                  return null;
              }
              @Override
              public Enumeration&lt;String&gt; getServletNames() {
                  return null;
              }
              @Override
              public void log(String s) {
              }
              @Override
              public void log(Exception e, String s) {
              }
              @Override
              public void log(String s, Throwable throwable) {
              }
              @Override
              public String getRealPath(String s) {
                  return null;
              }
              @Override
              public String getServerInfo() {
                  return null;
              }
              @Override
              public String getInitParameter(String s) {
                  return null;
              }
              @Override
              public Enumeration&lt;String&gt; getInitParameterNames() {
                  return null;
              }
              @Override
              public boolean setInitParameter(String s, String s1) {
                  return false;
              }
              @Override
              public Object getAttribute(String s) {
                  return null;
              }
              @Override
              public Enumeration&lt;String&gt; getAttributeNames() {
                  return null;
              }
              @Override
              public void setAttribute(String s, Object o) {
              }
              @Override
              public void removeAttribute(String s) {
              }
              @Override
              public String getServletContextName() {
                  return null;
              }
              @Override
              public ServletRegistration.Dynamic addServlet(String s, String s1) {
                  return null;
              }
              @Override
              public ServletRegistration.Dynamic addServlet(String s, Servlet servlet) {
                  return null;
              }
              @Override
              public ServletRegistration.Dynamic addServlet(String s, Class&lt;? extends Servlet&gt; aClass) {
                  return null;
              }
              @Override
              public &lt;T extends Servlet&gt; T createServlet(Class&lt;T&gt; aClass) throws ServletException {
                  return null;
              }
              @Override
              public ServletRegistration getServletRegistration(String s) {
                  return null;
              }
              @Override
              public Map&lt;String, ? extends ServletRegistration&gt; getServletRegistrations() {
                  return null;
              }
              @Override
              public FilterRegistration.Dynamic addFilter(String s, String s1) {
                  return null;
              }
              @Override
              public FilterRegistration.Dynamic addFilter(String s, Filter filter) {
                  return null;
              }
              @Override
              public FilterRegistration.Dynamic addFilter(String s, Class&lt;? extends Filter&gt; aClass) {
                 return null;
              }
              @Override
              public &lt;T extends Filter&gt; T createFilter(Class&lt;T&gt; aClass) throws ServletException {
                  return null;
              }
              @Override
              public FilterRegistration getFilterRegistration(String s) {
                  return null;
              }
              @Override
              public Map&lt;String, ? extends FilterRegistration&gt; getFilterRegistrations() {
                  return null;
              }
              @Override
              public SessionCookieConfig getSessionCookieConfig() {
                  return null;
              }
              @Override
              public void setSessionTrackingModes(Set&lt;SessionTrackingMode&gt; set) {
              }
              @Override
              public Set&lt;SessionTrackingMode&gt; getDefaultSessionTrackingModes() {
                  return null;
              }
              @Override
              public Set&lt;SessionTrackingMode&gt; getEffectiveSessionTrackingModes() {
                  return null;
              }
              @Override
              public void addListener(String s) {
              }
              @Override
              public &lt;T extends EventListener&gt; void addListener(T t) {
              }
              @Override
              public void addListener(Class&lt;? extends EventListener&gt; aClass) {
              }
              @Override
              public &lt;T extends EventListener&gt; T createListener(Class&lt;T&gt; aClass) throws ServletException {
                  return null;
              }
              @Override
              public JspConfigDescriptor getJspConfigDescriptor() {
                  return null;
              }
              @Override
              public ClassLoader getClassLoader() {
                  return null;
              }
              @Override
              public void declareRoles(String... strings) {
              }
          };
      }
  
      @Override
      public void setMaxInactiveInterval(int interval) {
      }
  
      @Override
      public int getMaxInactiveInterval() {
          return 0;
      }
  
      @Override
      public HttpSessionContext getSessionContext() {
          return null;
      }
  
      @Override
      public Object getAttribute(String name) {
          return attributes.get(name);
      }
  
      @Override
      public Object getValue(String name) {
          return null;
      }
  
      @Override
      public Enumeration&lt;String&gt; getAttributeNames() {
          return null;
      }
  
      @Override
      public String[] getValueNames() {
          return new String[0];
      }
  
      @Override
      public void setAttribute(String name, Object value) {
          attributes.put(name,value);
      }
  
      @Override
      public void putValue(String name, Object value) {
  
      }
  
      @Override
      public void removeAttribute(String name) {
  
      }
  
      @Override
      public void removeValue(String name) {
  
      }
  
      @Override
      public void invalidate() {
  
      }
  
      @Override
      public boolean isNew() {
          return false;
      }
  }
  </code></pre>