Module 7: Testing

In this module, you write tests for the RejectDoubleBooking trigger you created in module 6.

Step 1: Create a Trigger that Rejects Double Bookings

  1. In the Developer Console, click File > New > Apex Trigger

  2. Specify RejectDoubleBooking as the trigger name, Session_Speaker__c as the sObject, and click Submit

  3. Implement the trigger as follows:

    trigger RejectDoubleBooking on Session_Speaker__c (before insert, before update) {
    
        for(Session_Speaker__c sessionSpeaker : trigger.new) {
    
            // Retrieve session information including session date and time
            Session__c session = [SELECT Id, Session_Date__c FROM Session__c
                                    WHERE Id=:sessionSpeaker.Session__c];
    
            // Retrieve conflicts: other assignments for that speaker at the same time
            List<Session_Speaker__c> conflicts =
                [SELECT Id FROM Session_Speaker__c
                    WHERE Speaker__c = :sessionSpeaker.Speaker__c
                    AND Session__r.Session_Date__c = :session.Session_Date__c];
    
            // If conflicts exist, add an error (reject the database operation)
            if(!conflicts.isEmpty()){
                sessionSpeaker.addError('The speaker is already booked at that time');
            }
    
        }
    
    }
    
  4. Save the file

  5. Test the trigger:

    • Assign a speaker to a session scheduled at a time the speaker is available and make sure it still works
    • Assign a speaker to a session scheduled at the same time as another session the speaker is already assigned to: you should see the error message

Step 2: Create a Test Class

  1. In the Developer Console, select File > New > Apex Class, specify TestRejectDoubleBooking as the class name and click OK

  2. Make the class private, and add the @isTest class annotation:

    @isTest
    private class TestRejectDoubleBooking{
    
    }
    

Step 3: Add a Test Method to Test Single Bookings

  1. Add a TestSingleBooking() method to the TestRejectDoubleBooking class to make sure the trigger does not prevent a valid speaker booking:

    static testmethod void TestSingleBooking() {
        Datetime now = System.now();
    
        Speaker__c speaker = new Speaker__c(First_Name__c='John', Last_Name__c='Smith');
        insert speaker;
    
        Session__c session = new Session__c(Name='Some Session', Session_Date__c=now);
        insert session;
    
        Session_Speaker__c assignment =
            new Session_Speaker__c(Session__c=session.Id, Speaker__c=speaker.Id);
        Test.startTest();
        Database.SaveResult result = Database.insert(assignment, false);
        Test.stopTest();
    
        System.assert(result.isSuccess());
    }
    
  2. Save the file

  3. Click Run Test in the upper right corner of the code editor

  4. Click the Tests tab at the bottom of the code editor, and examine the test results.

Step 4: Add a Test Method to Test Double Bookings

  1. Add a TestDoubleBooking() method to the TestRejectDoubleBooking class to make sure trigger actually rejects double bookings:

    static testmethod void TestDoubleBooking() {
        Datetime now = System.now();
    
        Speaker__c speaker = new Speaker__c(First_Name__c='John', Last_Name__c='Smith');
        insert speaker;
    
        Session__c session1 = new Session__c(Name='Session 1', Session_Date__c=now);
        insert session1;
        Session__c session2 = new Session__c(Name='Session 2', Session_Date__c=now);
        insert session2;
    
        Session_Speaker__c assignment1 =
            new Session_Speaker__c(Session__c=session1.Id, Speaker__c=speaker.Id);
        insert assignment1;
    
        Session_Speaker__c assignment2 =
            new Session_Speaker__c(Session__c=session2.Id, Speaker__c=speaker.Id);
        Test.startTest();
        Database.SaveResult result = Database.insert(assignment2, false);
        Test.stopTest();
    
        System.assert(!result.isSuccess());
    }
    
  2. Save the file

  3. Click Run Test in the upper right corner of the code editor

  4. Click the Tests tab at the bottom of the code editor, and examine the test results.

comments powered by Disqus