title: How to unit test a class that implements Runnable
body: <p>I have a class ExampleThread that implements the Runnable interface.</p>
  
  <pre><code>public class ExampleThread implements Runnable {
  
      private int myVar;
  
      public ExampleThread(int var) {
          this.myVar = var;
      }
  
      @Override
      public void run() {
          if (this.myVar &lt; 0) {
              throw new IllegalArgumentException("Number less than Zero");
          } else {
              System.out.println("Number is " + this.myVar);
          }
      }
  }
  </code></pre>
  
  <p>How can I write JUnit test for this class. I have tried like below</p>
  
  <pre><code>public class ExampleThreadTest {
  
      @Test(expected = IllegalArgumentException.class)
      public void shouldThrowIllegalArgumentExceptionForInvalidNumber() {
          ExampleThread exThread = new ExampleThread(-1);
  
          ExecutorService service = Executors.newSingleThreadExecutor();
          service.execute(exThread);
      }
  }
  </code></pre>
  
  <p>but this does not work. Is there any way I can test this class to cover all code?</p>