title: unit testing a method that takes a list as an argument
body: <p>I need to test a method that is taking a list as an argument. Below is the sample code:</p>
  
  <pre><code>public class C
  {
      private int x;
      private String y;
      //getters and setters
  }
  public class B
  {
      public void collectC(List&lt;C&gt; cList)
      {
          for(C c : cList)
          {
              System.out.println("int: " + c.getX() + "String: "+ c.getY());
          }
      }
  }
  </code></pre>
  
  <p>So class B is simply collecting objects of class C and iteration on it using enhanced for loop.
  Now, I want to test a method of class B. Below is the testing code.</p>
  
  <pre><code>public class BTest
  {
      private List&lt;C&gt; cList;
      @Mock private C c;
      @InjectMocks private B b;
  
      @Before
      public void setUp()
      {
          cList = new ArrayList&lt;&gt;();
          cList.add(c);
          MockitoAnnotations.initMocks(this);
      }
  
      @Test
      public void testCollectC()
      {
          Mockito.when(c.getX()).thenReturn(5);
          Mockito.when(c.getY()).thenReturn("Hello There");
          b.collectC(cList);
      }
  }
  </code></pre>
  
  <p>So, this is giving me error <code>NullPointerException</code> on <code>System.out.println()</code> one line where I am invoking methods on 'c' object.
  I then changed the code where I mocked a list and iterator too, so now my code is working fine. But I want to know that what is the problem with above-mentioned code and why it is failing? </p>