0

I'm trying to test for an exception with JUnit 4.11. I saw a similar post here (JUnit expected tag not working as expected) and I've attempted everything they said, but still not working.

Here's my code:

import org.junit.*;

public class MaybeException {
  @Test(expected = Exception.class)
  public void ME1() { throw new Exception(); }
}

but bash is still telling me error: unreported exception Exception; must be caught or declared to be thrown.

Community
  • 1
  • 1

1 Answers1

2

Exception is a checked exception class. That means that you fully expect to be able to recover from the error condition, or force another method to deal with the error condition.

You have to declare in your method that you're throwing it.

@Test(expected = Exception.class)
public void ME1() throws Exception {
    throw new Exception();
}
Makoto
  • 104,088
  • 27
  • 192
  • 230