Expecting an exception JUnit
When expecting exceptions in the code instead of
@Test(expected=Exception.class)
public void doFooTest(){...}
do something like this
public class FooTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void doFooTest() {
Bar bar = new Bar();
exception.expect(IndexOutOfBoundsException.class);
bar.doStuff();
}
}
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
Written by Gautam
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Exceptions
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#