Is it possible to @Test if an appropriate exception was thrown in the main code function eaven if it was catch in a try / catch block?
ex:
public int maxSum(int a, int b) {
try {
if (a + b > 100)
throw new TooMuchException("Too much! Sum reduced to 50");
} catch (TooMuchException e) {
System.out.println(e);
return 50;
}
return a + b;
}
To be tested by somethink like this
@Test
void maxSum_TooMuchExceptionThrowedAndCatchedWhenSumOfNumbersIsOver100() {
Service service = new Service();
assertThatThrownBy(() -> {
service.maxSum(55, 66);
}).isInstanceOf(TooMuchException.class)
.hasMessageContaining("Too much! Sum reduced to 50");
}
.
public class TooMuchException extends Throwable {
public TooMuchException(String message) {
super(message);
}
}
Test Exception not the message.
I care about this because I want to be able to catch exceptions in function without crashing the program.