Advanced 35 min read

Write Comprehensive Tests

By the end of this lesson, you'll be able to:

  • Identify gaps in the test coverage written so far across this module
  • Write tests for the remaining untested scenarios
  • Apply Module 31's full checklist to this project's test suite as a whole

Prerequisites: "Write a Test Data Factory"

Auditing what's covered so far

  • calculateInterestRate — all three tiers plus boundaries. ✓
  • calculateMonthlyPayment — never directly tested yet, only indirectly through the trigger test. ✗
  • validate — several rejection scenarios, one acceptance scenario. ✓
  • determineStatus (inside the trigger) — Pre-Approved and mixed scenarios tested. Manual Review and Declined individually? Only as part of the mixed test.

Closing the calculateMonthlyPayment gap

@isTest
static void monthlyPaymentCalculatesCorrectly() {
    LoanCalculationService service = new LoanCalculationService();
    Decimal payment = service.calculateMonthlyPayment(10000, 6.5, 60);

    // A known correct value for this exact input, calculated independently ahead of time
    System.assertEquals(195.66, payment.setScale(2));
}

Testing calculateMonthlyPayment directly — rather than only indirectly through the trigger test — means a bug in the formula itself gets caught precisely, instead of only surfacing as a vaguer failure somewhere inside a larger trigger test.

Testing the boundary between Manual Review and Declined

@isTest
static void creditScoreOf600GetsManualReview() {
    Loan_Application__c app = LoanTestDataFactory.createApplication(10000, 50000, 600);
    insert app;

    Loan_Application__c saved = [SELECT Status__c FROM Loan_Application__c WHERE Id = :app.Id];
    System.assertEquals('Manual Review', saved.Status__c);
}

@isTest
static void creditScoreOf599GetsDeclined() {
    Loan_Application__c app = LoanTestDataFactory.createApplication(10000, 50000, 599);
    insert app;

    Loan_Application__c saved = [SELECT Status__c FROM Loan_Application__c WHERE Id = :app.Id];
    System.assertEquals('Declined', saved.Status__c);
}

Exactly the boundary-testing habit from Lesson 2, now applied to the trigger's status-decision logic specifically — 600 and 599 are where a real off-by-one bug in determineStatus would actually hide.

Exercise

Write a test confirming a loan amount exactly equal to 40% of income (the boundary) still gets Pre-Approved with a qualifying credit score.

Show hint

income * 0.4 exactly equals amount.

APEX

Write Comprehensive Tests Quiz

1. Why test calculateMonthlyPayment directly, rather than only through the trigger test?

Log in to submit the quiz and save your score.

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 steps back across every test written so far in this module and closes the remaining gaps, using Module 31's closing checklist as the guide.