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