I have a class ExampleThread 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 JUnit test for this class. I have tried like below
public class ExampleThreadTest {
@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionForInvalidNumber() {
ExampleThread exThread = new ExampleThread(-1);
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(exThread);
}
}
but this does not work. Is there any way I can test this class to cover all code?