title: Adding elemets to set in Mockito
body: <p>In tests I need to create method which will return a set of elements. I have problems with adding elements to this set, constantly getting null pointer exception. I have created new set by cast</p>
  <pre><code>Set&lt;T&gt; newSet = (Set&lt;T&gt;) mock(Set.class);
  </code></pre>
  <p>but I have still problem adding any element to this set.</p>
  <p>I do have method like this:</p>
  <pre><code>private Set&lt;T&gt; prepareData(){
  T element1 = mock(T.class);
  T element2 = mock(T.class);
  when(element1.getText()).thenReturn('abc');
  when(element1.getText()).thenReturn('def');
  
  Set&lt;T&gt; set = new HashSet&lt;&gt;;
  set.add(element1);
  set.add(element2);
  
  return set;
  }
  
  </code></pre>
  <p>For list it all worked well, but I had to change it to set and have NPE in line adding element1 to set.</p>