title: Unit testing an executor callable
body: <p>I have a controller class like this that passes some executor into an instance of runnable.
  This isn't how it works but just for simplicity I did it like this.</p>
  
  <pre><code>package com.test.executor;
  
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  public class Controller {
      private ExecutorService executor;
  
      public Controller() {
          executor = Executors.newCachedThreadPool();
      }
  
      public void doRun() {
          MyRunner runner = new MyRunner(executor);
          Thread myRunner = new Thread(runner);
          myRunner.start();
      }
  
      public static void main(String[] args) {
          new Controller().doRun();
      }
  }
  </code></pre>
  
  <p>The runner receives the instance of the executor and then passes certain callables.
  Now, the callables are a bit diverse as some callables access the database/call some web service/file system</p>
  
  <p>I am having some issues on how to write a proper JUnit for this case.
  I wanted to have as much code coverage as possible.</p>
  
  <pre><code>package com.test.executor;
  
  import java.util.Collections;
  import java.util.List;
  import java.util.concurrent.Callable;
  import java.util.concurrent.ExecutionException;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Future;
  import java.util.concurrent.TimeUnit;
  import java.util.concurrent.TimeoutException;
  
  public class MyRunner implements Runnable {
  
      private ExecutorService executor;
  
      public MyRunner(ExecutorService executor) {
          this.executor = executor;
      }
  
      @Override
      public void run() {
          // TODO Auto-generated method stub
          Future&lt;Boolean&gt; ret1 = executor.submit(new SampleCall1());
  
          try {
              ret1.get(5, TimeUnit.MINUTES);
          } catch (InterruptedException | ExecutionException | TimeoutException e) {
              e.printStackTrace();
          }
  
          // Do other things
  
          Future&lt;List&lt;String&gt;&gt; ret2 = executor.submit(new SampleCall2());
  
          try {
              ret2.get(5, TimeUnit.MINUTES);
          } catch (InterruptedException | ExecutionException | TimeoutException e) {
              e.printStackTrace();
          }
  
          // Do other things
  
          Future&lt;Long&gt; ret3 = executor.submit(new SampleCall3());
  
          try {
              ret3.get(5, TimeUnit.MINUTES);
          } catch (InterruptedException | ExecutionException | TimeoutException e) {
              e.printStackTrace();
          }
      }
  
      public static class SampleCall1 implements Callable&lt;Boolean&gt; {
  
          @Override
          public Boolean call() throws Exception {
              // Sample Return Only
              // This will call JSON web service
              return true;
          }
  
      }
  
      public static class SampleCall2 implements Callable&lt;List&lt;String&gt;&gt; {
  
          @Override
          public List&lt;String&gt; call() throws Exception {
              // Sample Return Only
              // This will call Database
              return Collections.EMPTY_LIST;
          }
  
      }
  
      public static class SampleCall3 implements Callable&lt;Long&gt; {
  
          @Override
          public Long call() throws Exception {
              // Sample Return Only
              // This will access some file system
              return 1L;
          }
  
      }
  
  }
  </code></pre>
  
  <p>My question is what is the proper way to write a unit test for this.  I am gathering some advice on how I can unit test this class?
  I am not sure what to mock in my junit/mockito instance.
  Should I mock each callable? And then create a test case for MyRunner.</p>
  
  <p>I worry about the dependency..as I am connecting into a database/a web service/and file system so I would like to ask for some advice. </p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <pre><code>package com.test.executor;
  
  import java.util.Collections;
  import java.util.List;
  import java.util.concurrent.Callable;
  import java.util.concurrent.ExecutionException;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Future;
  import java.util.concurrent.TimeUnit;
  import java.util.concurrent.TimeoutException;
  
  public class MyRunner implements Runnable {
  
      private ExecutorService executor;
  
      public MyRunner(ExecutorService executor) {
          this.executor = executor;
      }
  
      @Override
      public void run() {
          executeTasks1();
          // Do other things
  
          executeTasks2();
          // Do other things
  
  
          executeTasks3();
  
      }
  
      public boolean executeTasks1(){
          // TODO Auto-generated method stub
          Future&lt;Boolean&gt; ret1 = executor.submit(new SampleCall1());
  
          try {
              return ret1.get(5, TimeUnit.MINUTES);
          } catch (InterruptedException | ExecutionException | TimeoutException e) {
              e.printStackTrace();
          }
  
      }
  
      public List&lt;String&gt; executeTasks2(){
          Future&lt;List&lt;String&gt;&gt; ret2 = executor.submit(new SampleCall2());
  
          try {
              return ret2.get(5, TimeUnit.MINUTES);
          } catch (InterruptedException | ExecutionException | TimeoutException e) {
              e.printStackTrace();
          }
      }
  
      public Long executeTasks3(){
          Future&lt;Long&gt; ret3 = executor.submit(new SampleCall3());
  
          try {
              return ret3.get(5, TimeUnit.MINUTES);
          } catch (InterruptedException | ExecutionException | TimeoutException e) {
              e.printStackTrace();
          }
      }
  
      public static class SampleCall1 implements Callable&lt;Boolean&gt; {
  
          @Override
          public Boolean call() throws Exception {
              // Sample Return Only
              // This will call JSON web service
              return true;
          }
  
      }
  
      public static class SampleCall2 implements Callable&lt;List&lt;String&gt;&gt; {
  
          @Override
          public List&lt;String&gt; call() throws Exception {
              // Sample Return Only
              // This will call Database
              return Collections.EMPTY_LIST;
          }
  
      }
  
      public static class SampleCall3 implements Callable&lt;Long&gt; {
  
          @Override
          public Long call() throws Exception {
              // Sample Return Only
              // This will access some file system
              return 1L;
          }
  
      }
  
  }
  </code></pre>