In this module, you write tests for the RejectDoubleBooking trigger you created in module 6.
In the Developer Console, click File > New > Apex Trigger
Specify RejectDoubleBooking as the trigger name, Session_Speaker__c as the sObject, and click Submit
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');
}
}
}
Save the file
Test the trigger:
In the Developer Console, select File > New > Apex Class, specify TestRejectDoubleBooking as the class name and click OK
Make the class private, and add the @isTest class annotation:
@isTest
private class TestRejectDoubleBooking{
}
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());
}
Save the file
Click Run Test in the upper right corner of the code editor
Click the Tests tab at the bottom of the code editor, and examine the test results.
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());
}
Save the file
Click Run Test in the upper right corner of the code editor
Click the Tests tab at the bottom of the code editor, and examine the test results.