I wrote a JUnit test for my method.
This is my method:
public static int delimit(int value, int min, int max) throws IllegalArgumentException { if (min > max) throw new IllegalArgumentException("Min value was greater than max value."); return Math.min(max, Math.max(min, value)); } This is the test:
public class DMathTest extends TestCase { @Rule public ExpectedException exception = ExpectedException.none(); @Test public void testDelimit() { ... exception.expect(IllegalArgumentException.class); DMath.delimit(1, 3, 2); } } The IllegalArgumentException is indeed thrown, but JUnit marks it as an error in the test. It tells me that the test failed, because of the (expected) exception. There is no AssertionError or anything, it just shows my IllegalArgumentException, as if the whole expectation code wasn't there. I then removed the rule and all the expectation code and it was exactly the same result (of course, this is how it should be). So the rule and the call
exception.expect(IllegalArgumentException.class); didn't do anything.
I am using Eclipse and I include JUnit using Maven. The dependency element looks like this:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> What did I do wrong? Why is the expecting of the exception not working?