Skip to content

Instantly share code, notes, and snippets.

@melissajhansen
Last active November 16, 2018 22:00
Show Gist options
  • Save melissajhansen/723711339f51f7bf168875c9f5d6e7b8 to your computer and use it in GitHub Desktop.
Save melissajhansen/723711339f51f7bf168875c9f5d6e7b8 to your computer and use it in GitHub Desktop.
@isTest
private class HandleBadMethodsTest {
@isTest
static void testCustomException() {
//validate that our method will reject negative integers
Boolean exceptionWasThrown = false;
try {
SampleBadMethods.iAddUpPositiveIntegers(-1,5);
} catch (SampleBadMethods.SampleCustomException e) {
//This assert verifies the correct error message was thrown
System.assertEquals(e.getMessage(), 'I only take positive integers!');
exceptionWasThrown = true;
}
//This assert verifies that an exception was indeed thrown. The above assert is insufficient,
//because the test would pass even if no exception was thrown at all
System.assertEquals(exceptionWasThrown, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment