title: Mockito when().thenReturn calls the method unnecessarily
body: <p>I'm working a bit on an inherited code.
  I've written a test that is supposed to catch NullPointerException (for it is trying to call a method from null object)</p>
  
  <pre><code>@Test(expected=NullPointerException.class)
  public void checkXRequirement_NullProduct_AddAction_ShouldThrowNullPointerException() throws CustomException {
    Site site = mock(Site.class);
    Product product = null;
    when(BasketHelper.getAction(request)).thenReturn(0);
    when(BasketHelper.getActionProduct(site, request)).thenReturn(product);
    BasketHelper.requiresX(request, site);
  
  }
  </code></pre>
  
  <p>Relevant Methods and Variables:</p>
  
  <pre><code>public static final int ACTION_ADD = 0;
  public static final int ACTION_DELETE = 1;
  
  protected static int getAction(HttpServletRequest a_request) {
    String sBuyProduct = a_request.getParameter(ATTRIBUTE_NAME_BUY_PRODUCT);
    String sBuyProduct = a_request.getParameter(ATTRIBUTE_NAME_BUY_PRODUCT);
  
    if (sBuyProduct != null) iAction = ACTION_ADD;
    else (sDelProduct != null) iAction = ACTION_DELETE;
  
    return iBasketAction
  }
  
  protected static Product getActionProduct(Site a_site, HttpServletRequest a_request) {
  
      String sBuyProduct = a_request.getParameter(ATTRIBUTE_NAME_BUY_PRODUCT);
      String sDelProduct = a_request.getParameter(ATTRIBUTE_NAME_DEL_PRODUCT);
      String sProduct = null;
  
      switch (getBasketAction(a_request)) {
          case BASKET_ACTION_ADD:
          sProduct = sBuyProduct;
      break;
          case BASKET_ACTION_DELETE:
          sProduct = sDelProduct;
      break;
      }
  
      int iProductId;
      try {
          iProductId = Integer.parseInt(sProduct);
      } catch (NumberFormatException nbrEx) {
          return null;
      }
  
      Product prod = getProductById(iProductId);
  
      if (prod.isMasterProduct()) {
          prod = getChildProduct(prod, a_site, a_request);
      }
  
      return prod;
  }
  
  
  public static boolean requiresX(HttpServletRequest request, Site site) throws CustomException {
    try{
      if (getAction(request) == ACTION_ADD) { 
      Product prod = getActionProduct(site, request);
      return prod.getType().isRequiredX();
      }  
    } catch(NullPointerException exception) {
      log.error("Error Message", exception);
    }
    return false;
  }
  </code></pre>
  
  <p>The jUnit result of running the test is a failure with the stack trace of:</p>
  
  <pre><code>java.lang.Exception: Unexpected exception, expected&lt;java.lang.NullPointerException&gt; but was&lt;org.mockito.exceptions.misusing.WrongTypeOfReturnValue&gt;
  Caused by: org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
  Integer cannot be returned by getParameter()
  getParameter() should return String#
  </code></pre>
  
  <p><strong>Do I misinterpret how when().thenReturn is supposed to work here?</strong> I just want getAction to return 0 and getActionProduct to return null whenever it's being called. Clearly getParameter() is called and I don't know why exactly.</p>