Test Edge Cases and Exceptions
By the end of this lesson, you'll be able to:
- Test the exact validation boundaries (credit score 300 and 850)
- Test a batch containing both valid and invalid applications together
- Explain what happens to the whole batch when one record fails validation
Prerequisites: "Write Comprehensive Tests"
Testing the exact validation boundaries
@isTest
static void creditScoreOf300IsValid() {
LoanValidationService validator = new LoanValidationService();
validator.validate(10000, 50000, 300); // should not throw
System.assert(true);
}
@isTest
static void creditScoreOf299IsInvalid() {
LoanValidationService validator = new LoanValidationService();
try {
validator.validate(10000, 50000, 299);
System.assert(false, 'Expected a LoanValidationException');
} catch (LoanValidationException e) {
System.assert(e.getMessage().contains('Credit score'));
}
}
@isTest— marks the first test.static void creditScoreOf300IsValid() {—300— the exact edge of the valid range.LoanValidationService validator = new LoanValidationService();— creates the validator.validator.validate(10000, 50000, 300);— should not throw.System.assert(true);— confirms it didn't.}— closes this test.- (blank line)
@isTest— marks the second test.static void creditScoreOf299IsInvalid() {— the value one below the boundary.LoanValidationService validator = new LoanValidationService();— creates the validator.try {— wraps the call expected to throw.validator.validate(10000, 50000, 299);—299— precisely where a<vs<=mistake inLoanValidationService.validate()would hide.System.assert(false, 'Expected a LoanValidationException');— reached only if it didn't throw.} catch (LoanValidationException e) {— catches the expected failure.System.assert(e.getMessage().contains('Credit score'));— verifies the right error, exactly like the599/600boundary from the previous lesson.}— closes thetry/catch.}— closes this test.
A batch with one invalid application
@isTest
static void oneInvalidApplicationInABatchBlocksTheWholeInsert() {
List<Loan_Application__c> applications = new List<Loan_Application__c>{
LoanTestDataFactory.createQualifyingApplication(),
LoanTestDataFactory.createApplication(-5000, 50000, 720) // invalid: negative amount
};
try {
insert applications;
System.assert(false, 'Expected a DmlException from the invalid second record');
} catch (DmlException e) {
System.assert(e.getMessage().contains('amount'));
}
System.assertEquals(0, [SELECT COUNT() FROM Loan_Application__c]);
}
@isTest— marks the test method.static void oneInvalidApplicationInABatchBlocksTheWholeInsert() {— the test.List<Loan_Application__c> applications = new List<Loan_Application__c>{— a batch with one good and one bad record.LoanTestDataFactory.createQualifyingApplication(),— a perfectly valid application.LoanTestDataFactory.createApplication(-5000, 50000, 720)— an invalid one, with a negative amount.};— closes the list literal.- (blank line)
try {— wraps the insert expected to fail.insert applications;— becausebeforeInsertthrows an exception (viavalidate()) for the second application, the entireinsertstatement fails — not just the bad record.System.assert(false, 'Expected a DmlException from the invalid second record');— reached only if it unexpectedly succeeded.} catch (DmlException e) {— catches the expected failure.System.assert(e.getMessage().contains('amount'));— verifies the right error.}— closes thetry/catch.- (blank line)
System.assertEquals(0, [SELECT COUNT() FROM Loan_Application__c]);— confirms this explicitly: zero applications were saved, including the perfectly valid first one, exactly the all-or-nothing DML behavior from Module 19.}— closes the test method.
Exercise
Write a test confirming that inserting a batch of 3 valid applications and 1 with an out-of-range credit score (900) throws and saves nothing.
Show hint
Follow the try/System.assert(false)/catch pattern, then assert zero records were saved.
Test Edge Cases and Exceptions Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson tests the genuinely tricky edge cases: the exact validation boundaries, and — critically — what happens when a bulk insert mixes valid and invalid applications in the same batch.