title: Mockito - Verfication of another object's method call inside target class method
body: <p>I am writing a unit test for a method that involves cookies. Here is the method.</p>
  
  <pre><code>public Boolean addPropertyToUserWishlist(HttpServletRequest request,
      HttpServletResponse response, String propId) throws IOException {
      String message = "";
      try {
  
          Cookie[] cookies = request.getCookies();
          if (checkCookieValueExists(cookies, propId)) {
              message = "Already added to favourites!";
          } else {
              if (checkCookieExists(cookies)) {
                  for (Cookie cookie : cookies) {
                      if ("favourites".equals(cookie.getName())) {
                          cookie.setValue(cookie.getValue() + "_" + propId);
                          cookie.setMaxAge(60 * 60 * 24 * 365 * 5);
                          response.addCookie(cookie);
                          message = "Added to Favourites!";
                          break;
                      }
                  }
              } else {
                  Cookie c = new Cookie("favourites", propId);
                  c.setMaxAge(60 * 60 * 24 * 365 * 5);
                  response.addCookie(c);
                  message = "Added to Favourites!";
              }
          }
      } catch (Exception ex) {
          ex.printStackTrace();
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>And here is the unit test for the method.</p>
  
  <pre><code>@Test
  public void testAddPropertyToUserWishlist() throws Exception {
      System.out.println("addPropertyToUserWishlist");
  
      //decleration
      HttpServletRequest request = mock(HttpServletRequest.class);
      HttpServletResponse response = mock(HttpServletResponse.class);
      Cookie c = mock(Cookie.class);
      Cookie[] cookies = new Cookie[2];
      String propId = "5"; //testing the for property with id 5
      WishlistController instance = Mockito.spy(new WishlistController());
  
      // training
      Mockito.doReturn(false).when(instance).checkCookieExists(cookies);
      Mockito.doReturn(false).when(instance).checkCookieValueExists(cookies, propId);
      when(request.getCookies()).thenReturn(cookies);
  
      // testing
      Boolean result = instance.addPropertyToUserWishlist(request, response, propId);
      //verify(response, times(1)).addCookie(c);
      assertTrue(result);
  }
  </code></pre>
  
  <p>I wanted to test if the cookie was created. Since I am not executing this code in the browser. So How can I verify if the constructor for creating new cookie was called and the methods <code>setMaxAge</code> was called once?</p>