• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Test Classes

Test Classes in Salesforce

We have three types of testing

  • Unit testing:

Testing each components individually is called unit testing it is a job of developer.

  • Integration testing:

This is the testing which is combination of unit testing.

  • System testing

Once the integration testing complets system testing is the testing after rectifying some of errors in integration testing.

Sample Test Classes

Test Class for Triggers Senario 1:

@isTest
 Public Class UpdateHandoffAttachedTest{
 Static testMethod void HandoffAttached(){
 Account a = new Account();
 a.Name='Test Account';
 a.phone='8686864286';
 a.Rating='Hot';
 insert a;
 //Create a new Opportunity
 Opportunity opp=new Opportunity();
 opp.Name='Test Oppty';
 Opp.StageName='Prospecting';
 Date myDate = Date.newinstance(‘2012, 2, 17’);
 opp.CloseDate=myDate;
 opp.Accountid=a.id;
 insert opp;
 //Create Top X Designation with Positive scenario
 Top_X_Designation__c t = new Top_X_Designation__c();
 t.Type__c='Contract Flow Down/Handoff';
 t.Document_Attached__c=True;
 t.Opportunity__c=opp.id;
insert t;
 Opportunity o1 = “Select Handoff_Attached__c from Opportunity where id=:opp.id “;
 System.assertEquals('Yes',o1.Handoff_Attached__c);
 //Update Top X Designation with Negative scenario
 t.Type__c='Contract Flow Down/Handoff';
 t.Document_Attached__c=False;
 t.Opportunity__c=opp.id;
 Update t;
 Opportunity o2 = “Select Handoff_Attached__c from Opportunity where id=:opp.id “;
 System.assertEquals('No',o2.Handoff_Attached__c);
 delete t;
 Opportunity o3 = “Select Handoff_Attached__c from Opportunity where id=:opp.id “;
 System.assertEquals(null,o3.Handoff_Attached__c);
 }
 }

Test Class for Triggers Senario 2:

@isTest
 public class DuplicateAccountTestClass {
 public static testMethod void testAccountDuplicateTrigger() {
 String addError;
 Account a1= new Account(Name='Test Account');
 insert a1;
 Account a2= new Account(Name='Test Account');
 insert a2;
 System.assertequals('You Cannot Create the Duplicate Account', addError);
 try {
 insert a2;
 }
 catch (Exception e) {
 System.debug('We want to see this. This means the trigger is working.');
 }
 }
 }

Test Class for Triggers Senario 3:

@isTest
 private class HelloWorldTestClass {
 static testMethod void validateHelloWorld() {
 Account a = new Account(name='T1 Account');
 // Insert account
 insert a;
 // Retrieve account
 a = “SELECT hello__c FROM account WHERE Id =:a.id”;
// Test that HelloWorld program correctly added the value
 // "World" to the Hello field
 System.assertEquals('World', a.hello__c);
 }
 }

Test Class for Triggers Senario 4:

@isTest
 Public Class PrefixDoctorTest{
 Public static testMethod void PreDoctor(){
 Lead l=new Lead();
 l.LastName='Anil';
 l.company='Wipro';
 l.Status='Open - Not Contacted';
 insert l;//inserting a single record
 }
 }

Test Class for Triggers Senario 5:

@isTest
 Public Class OppTeamTest{
 static testMethod void CreateOppOwnerintoSalesTeam(){
 //inserting an opportunity record
 Opportunity opp= new Opportunity();
 opp.Name='Test Opportunity';
 Date myDate = Date.newinstance(2012,6,24);
 opp.CloseDate=myDate;
 opp.StageName='Prospecting';
 insert opp;
 Opportunity opty=“Select OwnerId from Opportunity where id=:opp.id”;
 OpportunityTeamMember otm=“Select UserId from OpportunityTeamMember where Op
 portunityId=:opp.id”;
 System.assertEquals(opty.OwnerId,otm.UserId);
 }
 }

Enthusiastic about exploring the skill set of Salesforce? Then, have a look at the blog post to gather 
additional knowledge on Salesforce Online Training.

Test Class for Triggers Senario 6:

@isTest
 Public Class DiscountTriggerTest{
Public static testMethod void Discount(){
 Book__c b = new Book__c();
 b.Name='Test';
 b.Price__c=100;
 insert b;
 Book__c b2 = “Select Price__c From Book__c where id=:b.id”;
 System.assertEquals(90,b2.Price__c);
 }
 }

Test Class for Triggers Senario 7:

@isTest
 Public Class ContactsCreationTest{
 static testMethod void CreateContacts(){
 Account a = new Account();
 a.name='Test Account';
 a.phone='9052757414';
 a.Rating='Hot';
 a.Number_of_Locations__c=4;
 insert a;
 List<Contact> lc=“Select id from Contact where AccountId=:a.id”;
 System.assertEquals(a.Number_of_Locations__c, lc.size());
 }
 }

Test Class for Triggers Senario 8:

@isTest
 Public Class UpdateCPActivenOnOpptyTest{
 static testMethod void updateCustomerProjectOnopty(){
 //Inserting first Opportunity
 Opportunity opp1= new Opportunity();
 opp1.Name='Test Opportunity 1';
 Date myDate = Date.newinstance(2012,6,24);
 opp1.CloseDate=myDate;
 opp1.StageName='Prospecting';
 insert opp1;
 //Inserting a Customer Project with Status equal to Active for above opp1
 Customer_Project__c cp1=new Customer_Project__c();
 cp1.Name='Test 1';
 cp1.Status__c='Active';
 cp1.Opportunity__c=opp1.id;
 insert cp1;
 Opportunity o1=“Select Active_Customer_Project__c from Opportunity where id=:opp1.i
d”;
 System.assertEquals(true,o1.Active_Customer_Project__c);
 //Inserting first Opportunity
 Opportunity opp2= new Opportunity();
 opp2.Name='Test Opportunity 1';
 Date d = Date.newinstance(2012,6,24);
 opp2.CloseDate=d;
 opp2.StageName='Prospecting';
 insert opp2;
 //Inserting a Customer Project with Status equal to Inactive for above opp2
 Customer_Project__c cp2=new Customer_Project__c();
 cp2.Name='Test 2';
 cp2.Status__c='Inactive';
 cp2.Opportunity__c=opp2.id;
 insert cp2;
 Opportunity o2=“Select Active_Customer_Project__c from Opportunity where id=:opp2.i
 d”;
 System.assertEquals(false,o2.Active_Customer_Project__c);
 }
 }

Test Class for Triggers Senario 9:

@isTest
 Public Class CreateCRonContactCreationTest{
 static testMethod void CreateContact(){
 //Inserting Contacts with Postive Scenario
 Contact c1 = new contact();
 c1.LastName='Anil Reddy';
 c1.Contact_Relationship__c=True;
 insert c1;
 List<Contact_Relationship__c> ConRel1 = “SELECT Id FROM Contact_Relationship__
 c WHERE Contact__r.Id =:c1.id”;
 //verify that Contact Relationship was created for above contact
 System.assertEquals(1,ConRel1.size());
 // Inserting Contacts with Negative Scenario
 Contact c2=new Contact();
 c2.lastName='Sateesh Reddy';
 c2.Contact_Relationship__c=false;
 insert c2;
 List<Contact_Relationship__c> ConRel2 = “SELECT Id FROM Contact_Relationship__
 c WHERE Contact__r.Id =:c2.id”;
 System.assertEquals(0,ConRel2.size());
 }
 }

Test Class for Triggers Senario 10:

@isTest
 Public Class UpdateCROwnerNameTest{
 static testMethod void updateCROwnerName(){
 //inserting a user
 Profile pId= “select id from Profile where name =: 'System Administrator'“;
 User u= new User();
 u.LastName = 'Test User ';
 u.Alias = 'ta';
 u.Isactive =true;
 u.Username = 'testuseraccount@test.com';
 u.Email = 'testuser@test.com';
 u.CommunityNickname= 'TestCN';
 u.TimeZoneSidKey = 'America/Los_Angeles';
 u.LocaleSidKey='en_US';
 u.EmailEncodingKey= 'ISO-8859-1';
 u.ProfileId = pId.Id;
 u.LanguageLocaleKey = 'en_US';
 insert u;
 //inserting a Contact
 Contact c = new Contact();
 c.LastName='Anil';
 insert c;
 //inserting a Contact Relationship
 Contact_Relationship__c cr= new Contact_Relationship__c();
 cr.Name='Test Contact Relationship';
 cr.Contact__c=c.id;
 insert cr;
 //updating the Owner Name
 Contact_Relationship__c cr1=“Select OwnerId from Contact_relationship__c where id =:
 cr.id”;
 cr1.Ownerid=u.id;
 update cr1;
 Contact_Relationship__c cr2=“Select OwnerId from Contact_relationship__c where id =:
 cr1.id”;
 System.assertEquals(u.id,cr2.OwnerId);
 }
 }

Test Class for Triggers Senario 11:

@isTest
 Public Class InsertAccountTeamTest{
 static testMethod void AccountMangerInsertIntoAccountTeam(){
 //inserting a user
 Profile pId= “select id from Profile where name =: 'System Administrator'“;
 User u= new User();
 u.LastName = 'Test User ';
 u.Alias = 'ta';
 u.Isactive =true;
 u.Username = 'testuseraccount@test.com';
 u.Email = 'testuser@test.com';
 u.CommunityNickname= 'TestCN';
 u.TimeZoneSidKey = 'America/Los_Angeles';
 u.LocaleSidKey='en_US';
 u.EmailEncodingKey= 'ISO-8859-1';
 u.ProfileId = pId.Id;
 u.LanguageLocaleKey = 'en_US';
 insert u;
 //Inserting an Account
 Account a = new Account();
 a.Name='Test Account';
 insert a;
 //inserting Customer for above account
 Customer__c c=new Customer__c();
 c.Name='Test';
 c.Account__c=a.id;
 c.Account_Manager__c=u.id;
 insert c;
 AccountTeamMember atm=“Select UserId from AccountTeamMember where AccountId
 =:a.id”;
 System.assertEquals(u.id,atm.UserId);
 }
 }

For indepth understandingng on Salesforce click on

Summary
Review Date
Reviewed Item
Test Classes
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Test Classes"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.