mock a method calling another method using mockito, powermock
<p>-> controller.java</p>

<pre><code>public controller() {
    public controller(DataInterpreter interpret,ControllerClientUtility util, InterfaceConnection inter) {
        interpreter = interpret;
        utility = util;
        interfaced = inter;
    }
}
</code></pre>

<p>...</p>

<pre><code>public void closeOne(String vpnSessionId) throws Exception {
        try{
            if ( interfaced.connect() &amp;&amp; (interfaced.CheckIntegrity(SessionId)) ){
                interfaced.kill(vpnSessionId);
            }else{
                closeAll();
            }
        }catch(Exception e){
            if ( e.getMessage().startsWith("INTERFACE_ERR:") ){
                closeAll();
            }else{
                throw new Exception(e);
            }
        }
    }
</code></pre>

<p>-> methods in InterfaceConnection.java</p>

<pre><code>public String getReponseFor(String command) throws Exception{
    if (send(command)){
        return receive();
    }
    else{
        throw new Exception("INTERFACE_ERR: Could not get Response");
    }
}


public List&lt;String&gt; getListOfConnections() throws Exception{
   String statusResponse = getReponseFor("something");
   ..(regex searches and then make a list connectionsConnected) 
   return connectionsConnected;
}


public boolean CheckIntegrity(String SessionId){
    try {
        List&lt;String&gt; connections = new ArrayList&lt;String&gt;();
        connections = getListOfConnections();
        if (connections.contains(SessionId)){
            return true;
        }
        return false;
    }catch(Exception e){
        return false;
    }
}
</code></pre>

<p>Is there a way to mock the output of getListOfConnections ? I tried doing something like this but did not work</p>

<p>-> controllerTest.java</p>

<pre><code> @Mock private InterfaceConnection interfaced;

  @Before
  public void beforeTests() throws Exception {
    MockitoAnnotations.initMocks(this);
    impl = new Controller(interpreter,utility,interfaced);
</code></pre>

<p>...</p>

<pre><code>@Test
  public void testDisconnectOneSessionWithBadSessionId_sendCommand() throws Exception{
      String badSessionId = "123:123";
      List&lt;String&gt; mockConnections = new ArrayList&lt;String&gt;();
      mockConnections.add("asdasds");
      when(interfaced.getListOfConnections()).thenReturn(mockConnections);

      impl.closeOne(badSessionId);
      Mockito.verify(utility)....
 }
</code></pre>

<p>I hope I'm clear, thanks in advance.</p>