title: How to unit test a code snippet running inside executor service, instead waiting on Thread.sleep(time)
body: <p>How to unit test a code that is running in executor service?
  In my situation,</p>
  
  <pre><code>public void test() {
      Runnable R = new Runnable() {
          @Override
          public void run() {
              executeTask1();
              executeTask2();
          }
      };
  
      ExecutorService executorService = Executors.newSingleThreadExecutor();
      executorService.submit(R);
  }
  </code></pre>
  
  <p>When I am unit testing, I would like to make some validations that method executes.</p>
  
  <p>I am executing this in an executor service, as it makes some network operations.</p>
  
  <p>In my unit testing, I had to wait until this method finishes execution. Is there a better way I can do this, instead of waiting for <code>Thread.sleep(500)</code>.</p>
  
  <p>Unit testing code snippet:</p>
  
  <pre><code>@Test
  public void testingTask() {
      mTestObject.test();
      final long threadSleepTime = 10000L;
      Thread.sleep(threadSleepTime);
      verify(abc, times(2))
              .acquireClient(a, b, c);
      verify(abd, times(1)).addCallback(callback);
  }
  </code></pre>
  
  <p>Note: I am passing an executor service object into this constructor class.
  I would like to know if there is a good way of testing instead of waiting for sleep time.</p>