title: Mockito:Java - Unfinished stubbing detection
body: <p>I have been working with Mockito and have run into this error message:</p>
  
  <pre><code>org.mockito.exceptions.misusing.UnfinishedStubbingException: 
  Unfinished stubbing detected here:
  -&gt; at       com.rbc.rewards.catalogue.service.B2SServicesTest.getProductList_Success(B2SServ icesTest.java:52)
  
  E.g. thenReturn() may be missing.
  Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
  Hints:
    1. missing thenReturn()
    2. you are trying to stub a final method, you naughty developer!
    3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
  </code></pre>
  
  <p>I have read in two posts on Stack Overflow about this issue but they do not go into full detail. I believe it has something to do with <strong>nesting mocks inside a mocking</strong> (from what I read). However I do not see or fully understand by the small snippets people have posted.</p>
  
  <p><strong>My test class is as followed (leaving out unnecessary code):</strong></p>
  
  <pre><code>// Mock:  uses Java Reflection in order to create mock objects of each class
  @Mock
  private Scrapes scrapeS;
  @Mock
  private SsoS ssoS;
  @Mock
  private BScrape bScrape;
  
  //@InjectMocks annotation is used to create and inject the mock object
  @Mock
  private BService bService;
  
  
  @Before
  public void setUp() throws Exception {
      MockitoAnnotations.initMocks(this);
  }
  
  // Testing:
  @Test
  public void getProductList_Success() throws Exception{
  
          when(BService.getProductList("cookie", "6753"))
                  .thenReturn(
                  scrapeS.scrapePost(new String(),
                          new String(),
                          new HashMap&lt;&gt;(),
                          new bService()));
  }
  </code></pre>
  
  <p>Method I need to call:</p>
  
  <pre><code>public List&lt;prodItem&gt; getProdList(String raw_cookie, String catId) throws Exception {
      String url = ssoSetting.getUrls().BProductList();
      String post = "url" + catId +"url";
      BScrape scraper = new BScrape ();
      Map&lt;String, String&gt; postRequest = new HashMap();
      postRequest.put("searchRequestParams", post);
      return scrapeS.scrapePost(url, raw_cookie, postRequest, scraper);
  }
  </code></pre>
  
  <p>I am also using resources from <a href="http://www.tutorialspoint.com/mockito/mockito_junit_integration.htm" rel="nofollow noreferrer">TutorialsPoint</a>.</p>
  
  <p>It is happening within the @test method and I believe this mocking inside mocking is because I am using it wrong I am assuming.</p>
  
  <p><strong>After Implementation from Answer (working):</strong></p>
  
  <pre><code>@Mock
  private SSetting sSetting = new SSetting ();
  // Mock:  uses Java Reflection in order to create mock objects and is injected into InjectMocks
  @Mock
  private ProdItem productItem = new ProdItem ();
  @Mock
  private sService scrapeService = new sService ();
  @Mock
  private BScrape bScrape ;
  
  //@InjectMocks annotation is used to create and inject the mock object
  @InjectMocks
  private BService bService = new BService ();
  
  @Before
  public void setUp() throws Exception {
      MockitoAnnotations.initMocks(this);
  }
  
  // Testing:
  @Test
  public void getProductList_Success() throws Exception{
  
      List&lt;ProductItem&gt; retList = new ArrayList&lt;&gt;();
      retList.add(new ProductItem());
      retList.add(new ProductItem());
  
      //try{
      when(b2SServices.getProductList("cookie", "6753")).thenCallRealMethod();
  
      //test the add functionality
      when(BService .getProdList("cookie", "6753")).thenReturn(retList);
  
  
      }catch(Exception exception){
          System.out.println(exception);
      }
  }
  </code></pre>