title: Testing a Fragment class in isolation using Mockito
body: <p>Added <code>@VisibleForTesting</code> and protected. My test can now this method:</p>
  
  <pre><code>   @VisibleForTesting
      protected void setupDataBinding(List&lt;Recipe&gt; recipeList) {
          recipeAdapter = new RecipeAdapter(recipeList);
          RecyclerView.LayoutManager layoutManager
                  = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
          rvRecipeList.setLayoutManager(layoutManager);
          rvRecipeList.setAdapter(recipeAdapter);
      }
  </code></pre>
  
  <p>Updated the test case using spy object: However, the real setupDataBinding(recipe) is getting called even when I created a mock of he spy that will get called. Maybe I am doing this wrong.</p>
  
  <pre><code>@Test
  public void testShouldGetAllRecipes() {
      RecipeListView spy = Mockito.spy(fragment);
      doNothing().when(spy).setupDataBinding(recipe);
  
      fragment.displayRecipeData(recipe);
  
      verify(recipeItemClickListener, times(1)).onRecipeItemClick();
  }
  </code></pre>
  
  <p>I am trying to test the methods in my <code>Fragment</code> class as below. However, I am trying to mock out the methods to verify that the methods are called the correct number of times. However, the problem is I have a <code>private</code> method <code>setupDataBinding(...)</code> that setups on the <code>RecyclerView</code> that is called from <code>displayRecipeData(...)</code>. I want to mock these calls as I don't want to call the real object on the <code>RecyclerView</code>. I just want to verify that <code>setupDataBinding(...)</code> gets called.</p>
  
  <p>I have tried using spy and <code>VisibleForTesting</code>, but still not sure how to do this.</p>
  
  <p>I am trying to test the Fragment in isolation.</p>
  
  <pre><code>public class RecipeListView
          extends MvpFragment&lt;RecipeListViewContract, RecipeListPresenterImp&gt;
          implements RecipeListViewContract {
  
      @VisibleForTesting
      private void setupDataBinding(List&lt;Recipe&gt; recipeList) {
          recipeAdapter = new RecipeAdapter(recipeList);
          RecyclerView.LayoutManager layoutManager
                  = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
          rvRecipeList.setLayoutManager(layoutManager);
          rvRecipeList.setAdapter(recipeAdapter);
      }
  
      @Override
      public void displayRecipeData(List&lt;Recipe&gt; recipeList) {
          /* Verify this get called only once */
          setupDataBinding(recipeList);
  
          recipeItemListener.onRecipeItem();
      }
  }
  </code></pre>
  
  <p>This is how I am testing. I have added the <code>VisibleForTesting</code> thinking I could help. And I have tried using the spy.</p>
  
  <pre><code>public class RecipeListViewTest {
      private RecipeListView fragment;
      @Mock RecipeListPresenterContract presenter;
      @Mock RecipeItemListener recipeItemListener;
      @Mock List&lt;Recipe&gt; recipe;
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(RecipeListViewTest.this);
          fragment = RecipeListView.newInstance();
      }
  
      @Test
      public void testShouldGetAllRecipes() {
          fragment.displayRecipeData(recipe);
          RecipeListView spy = Mockito.spy(fragment);
  
          verify(recipeItemListener, times(1)).onRecipeItem();
      }
  }
  </code></pre>
  
  <p>What would be the best way to test the above in isolation?</p>
  
  <p>Many thanks for any advice.</p>