Last Updated: February 25, 2016
· gautamnitish

Expecting an exception JUnit

When expecting exceptions in the code instead of

public void doFooTest(){...}

do something like this

public class FooTest {
  public ExpectedException exception = ExpectedException.none();

  public void doFooTest() {
    Bar bar = new Bar();


because the test will fail if the exception is not thrown in the second case , but in the first even if the exception is not thrown it will succeed