title: How to mock unreachable third party class properties via Mockito
body: <p>I have this beautiful scenery in front of me including JSF, jUnit(4.11) and Mockito(1.10.19):</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class UserAuth implements Serializable {
  
   private List&lt;UserRole&gt; roleList;
   private LocalChangeBean localChangeBean;
  
  public UserAuth() {
  
          roleList = new ArrayList&lt;UserRole&gt;();
  
          localChangeBean = (LocalChangeBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("localChangeBean");
  
          setLocalChangeBean(localChangeBean);
  
          setRoleList(getLocalChangeBean().getRoleList());
          //many other property setting and some JSF stuff
      }
  
   public boolean checkAuth() {
          for (UserRole role : getRoleList()) {
              if(role.getName().equals("SUPER_USER"))
                  return true;
          }
          return false;
      }
  }
  
  //A hell of a lot more code, proper getters/setters etc.
  </code></pre>
  
  <p>Here is the test class:</p>
  
  <pre><code>public class UserAuthTest {
  
      @Test
      public void testCheckAuth() {
  
          UserAuth bean = mock(UserAuth.class);
  
          List&lt;UserRole&gt; mockRoleList = new ArrayList&lt;UserRole&gt;();
          UserRole ur = mock(UserRole.class);
          when(ur.getName()).thenReturn("SUPER_USER");
          mockRoleList.add(ur);
  
          when(bean.getRoleList()).thenReturn(mockRoleList);
  
          assertEquals(true, bean.checkAuth());
      }
  </code></pre>
  
  <p>The thing is; UserRole class is not reachable by me, it's another part of the project. It doesn't have a no-argument constructor and the existing constructor requires other unreachable classes etc. Thus I can't instantiate it. In these circumstances, all I want to do is to make that mock UserRole object behave such as returning the needed String when it's getName() method gets called. </p>
  
  <p>But obviously; when I try to add that UserRole mock object into the List of UserRoles, the behavior that I tried to define is not stored with the object. And yes, the code looks pretty funny in its current stance. Though I left it there to learn what should I do to achieve this simple, little goal of mine.</p>
  
  <p>Post-Edit:
  I couldn't manage the problem without changing the original bean, though I followed Jeff's suggestion below and it worked well as a strategy of isolation. I did not mark it as the best answer since the question was "How to mock an unreachable third party class?" (in the current example its the UserRole class) Eventually the noob me understood that "Mocking an unreachable third party class is no different than mocking any other class". </p>
  
  <p>Here is how I managed it:</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class UserAuth implements Serializable {
  
   private List&lt;UserRole&gt; roleList;
   private LocalChangeBean localChangeBean;
  
  public UserAuth() {
  
          //the actual constructor including all JSF logic, highly dependent
      }
  
  UserAuth(List&lt;UserRole&gt; roleList) {
      setRoleList(roleList);
      //package private test-helper constructor which has no dependency on FacesContext etc.
    }
   public boolean checkAuth() {
          for (UserRole role : getRoleList()) {
              if(role.getName().equals("SUPER_USER"))
                  return true;
          }
          return false;
      }
  }
  </code></pre>
  
  <p>And here is the test class (attention to the iterator mock, it has the whole trick):</p>
  
  <pre><code>public class UserAuthTest {
  
  private UserRole mockRole;
  private Iterator&lt;UserRole&gt; roleIterator;
  private List&lt;UserRole&gt; mockRoleList;
  
  private UserAuth tester; 
  
  @SuppressWarnings("unchecked")
  @Before
  public void setup() {
  
      mockRoleList = mock(List.class);
      mockRole = mock(UserRole.class);
      roleIterator = mock(Iterator.class);
  
      when(mockRoleList.iterator()).thenReturn(roleIterator);
      when(roleIterator.hasNext()).thenReturn(true, false);
      when(roleIterator.next()).thenReturn(mockRole);
  
      tester = new UserAuth(mockRoleList);
  
  }
  
  @Test
  public void testCheckAuth(){
      when(mockRole.getName()).thenReturn("SUPER_USER");
      assertEquals("SUPER_USER expected: ", true, tester.checkAuth());
  }
  </code></pre>