Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Test Class for Trigger RestrictContactByName #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions TestRestrictContactByName.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
@isTest
private class TestRestrictContactByName {

@isTest
static void TestPositive(){

Contact SingleTestPositiveContact= new Contact(FirstName='Lovely', LastName='InvalidName',Email='[email protected]');

//Action
Test.startTest();
Database.SaveResult result = Database.insert(SingleTestPositiveContact, false);
Test.stopTest();

//Assert

System.assert(!result.isSuccess());
System.assert(result.getErrors().size()>0);
System.assertEquals('The Last Name "'+SingleTestPositiveContact.LastName+'" is not allowed for DML' ,
result.getErrors()[0].getMessage());
system.debug(result.getErrors()[0].getMessage());
}

@isTest
static void TestNegative(){

Contact SingleTestNegativeContact= new Contact(FirstName='Lovely', LastName='InvalidName1', Email='[email protected]');
//Action
Test.startTest();
Database.SaveResult result = Database.insert(SingleTestNegativeContact, false);
Test.stopTest();

//Assert

System.assert(result.isSuccess());
System.assert(result.getErrors().size()==0);
system.debug('Successfully Inserted:'+result.getId());
}

@isTest
static void TestforBulk(){

List<Contact> TestContactList = new List<Contact>{

new Contact(FirstName='Happy', LastName='InvalidName', Email='[email protected]'),
new Contact(FirstName='NotSoHappy', LastName='ValidName',Email='[email protected]'),
new Contact(Firstname='Fine', LastName='InvalidName1', Email='[email protected]'),
new Contact(Firstname='Fine', LastName='FineLastName', Email='[email protected]')};

//Action

Test.startTest();
Database.SaveResult[] result = Database.insert(TestContactList, false);
Test.stopTest();

// Iterate through each returned result
for (Database.SaveResult sr :result) {
if (sr.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully inserted Contact. Contact ID: ' + sr.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Contact fields that affected this error: ' + err.getFields());
}
}
}

}

@isTest
static void TestforUpdateContact(){


Contact TestCon = new Contact(FirstName='Superb', Lastname='FineLastName', Email='[email protected]');
insert TestCon;
system.debug(TestCon.Id+': '+TestCon.LastName);
TestCon.LastName='InvalidName';

//Action
Test.startTest();
Database.SaveResult result = Database.update(TestCon, false);
Test.stopTest();

System.assert(!result.isSuccess());
System.assert(result.getErrors().size()>0);
System.assertEquals('The Last Name "'+TestCon.LastName+'" is not allowed for DML' ,
result.getErrors()[0].getMessage());
system.debug(result.getErrors()[0].getMessage());
}
}