function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Muna Jamous5Muna Jamous5 

trigger coverage

HI,
I am getting 76% coverage for my Trigger, below is my trigger and test class, the uncovered code his underlined, any help is highly appreciated

trigger updateEquipmentDeatilField on Equipment_in_Room__c (before insert, before update) {
    try {
        List<Equipment_Specification__c> EquipmentDetails = new List<Equipment_Specification__c>();
        For (Equipment_in_Room__c r: Trigger.New) {
            if (r.Hospital_Name__c.equalsIgnoreCase('Dartmouth General Hospital')) {
                EquipmentDetails = [Select id, Name, Equipment_in_Room__c from Equipment_Specification__c where id =: r.Plan_Id__c limit 1];
                for (Equipment_Specification__c es : EquipmentDetails ) {
                    if (es.Equipment_in_Room__c == null ) { 
                        es.Equipment_in_Room__c = 'DG,'  ;
                    } else if (es.Equipment_in_Room__c.indexOf('DG') == -1 ) {
                        es.Equipment_in_Room__c = es.Equipment_in_Room__c + 'DG,'  ;

             }
                    update EquipmentDetails;
                }
                
            } else if (r.Hospital_Name__c.equalsIgnoreCase('Halifax Infirmary')) {
                EquipmentDetails = [Select id, Name, Equipment_in_Room__c from Equipment_Specification__c where id =: r.Plan_Id__c limit 1];
                for (Equipment_Specification__c es : EquipmentDetails ) {
                    if (es.Equipment_in_Room__c == null ) { 
                        es.Equipment_in_Room__c = 'HI,'  ;
                    } else if (es.Equipment_in_Room__c.indexOf('HI') == -1 ) {
                        es.Equipment_in_Room__c = es.Equipment_in_Room__c + 'HI,'  ;
                    }
                    update EquipmentDetails;
                }
            }
        }  // For 
    } catch(Exception e){
        System.Trigger.new[0].addError('Error in updateEquipmentDeatilField: '+e.getMessage());
   }

}


Test Class
@isTest
public class updateEquipmentDetailFieldTest {
    static testMethod void toUpdateEquipmentDetailFieldTest() {
        Boolean result = false; 
        // Equipment Plan ID
        try{
            
            Equipment_Specification__c es0 = new Equipment_Specification__c(name='Test-11');
            insert es0 ; 
            es0.Description__c = 'Test desc';
            es0.Price__c = 123.12;
            es0.Equipment_in_Room__c='HI';        
            
            
            Equipment_Specification__c es1 = new Equipment_Specification__c(name='Test-1aa');
            insert es1 ;
            es1.Description__c = 'Test desc1';
            es1.Price__c = 123.12;        
             
            
            Equipment_Specification__c es2 = new Equipment_Specification__c(name ='Test-1b');
            insert es2 ; 
            es2.Description__c = 'Test desc';
            es2.Price__c = 123.12;        
            es2.Equipment_in_Room__c='DG';
                         
            Equipment_Specification__c es3 = new Equipment_Specification__c(name ='Test-1c');
            insert es3 ; 
            es3.Description__c = 'Test desc';
            es3.Price__c = 123.12;        
            es3.Equipment_in_Room__c='HI';            
            
            Equipment_Specification__c es4 = new Equipment_Specification__c(name ='Test-1d' );
            insert es4 ; 
            es4.Description__c = 'Test desc';
            es4.Price__c = 123.12;        
            es4.Equipment_in_Room__c='DG';
                
            Equipment_Specification__c es5 = new Equipment_Specification__c(name ='Test-1e');
            insert es5 ; 
            es5.Description__c = 'Test desc';
            es5.Price__c = 123.12;                 
            
            Hospital_Room_No__c hr1 = new Hospital_Room_No__c();
            hr1.Room_No__c= '123';
            hr1.Department__c= 'ATRIUM';
            hr1.Room_Name__c = 'ALCOVE';
            hr1.Floor_No__c='LEVEL 1';
            hr1.RecordTypeId ='0126A000000HwTfQAK'; //Dartmouth 
            insert hr1;
            
            Hospital_Room_No__c hr2 = new Hospital_Room_No__c();
            hr2.Room_No__c = '1234';
            hr2.Department__c= 'ATRIUM';
            hr2.Room_Name__c = 'ALCOVE';
            hr2.Floor_No__c='LEVEL 1';
            hr2.RecordTypeId ='0126A000000HwTk'; //HI 
            insert hr2;

            // equipment in a rooms            
            Equipment_in_Room__c erd1= new Equipment_in_Room__c();
            insert erd1;
            erd1.Hospital_Room__c = hr1.Id;
            erd1.Plan_Id__c = es0.id;
            erd1.Room_Qty__c=1;
            erd1.Qty__c=2;
          
            Equipment_in_Room__c erd3= new Equipment_in_Room__c();
            insert erd3;
            erd3.Hospital_Room__c = hr1.Id;
            erd3.Plan_Id__c = es1.id;
            erd3.Room_Qty__c=1;
            erd3.Qty__c=2;

            Equipment_in_Room__c erd4= new Equipment_in_Room__c();
            insert erd4;
            erd4.Hospital_Room__c = hr1.Id;
            erd4.Plan_Id__c = es2.id;
            erd4.Room_Qty__c=1;
            erd4.Qty__c=2;
            
            Equipment_in_Room__c erh1= new Equipment_in_Room__c();
            insert erh1;
            erh1.Hospital_Room__c = hr2.Id;
            erh1.Plan_Id__c = es3.id;
            erh1.Room_Qty__c=1;
            erh1.Qty__c=2;

            Equipment_in_Room__c erh2= new Equipment_in_Room__c();
            insert erh2;
            erh2.Hospital_Room__c = hr2.Id;
            erh2.Plan_Id__c = es4.id; 
            erh2.Room_Qty__c=1;
            erh2.Qty__c=2;
            
            Equipment_in_Room__c erh3= new Equipment_in_Room__c();
            insert erh3;  
            erh3.Hospital_Room__c = hr2.Id;
            erh3.Plan_Id__c = es5.id;
            erh3.Room_Qty__c=1;
            erh3.Qty__c=2;
                                    
         }catch(DmlException ex){ result = true;}
              System.assert(result);         
    }                  
}
Best Answer chosen by Muna Jamous5
Abdul KhatriAbdul Khatri
Please find the test class for the same. I am not fond of doing this way but because of lack of time and late in reply we can go with it but try to check the best practise of writing test class in this link 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm
 
@isTest 
public class updateEquipmentDetailFieldTest {

    static testMethod void test_Dartmouth_General_Hospital() {

            Equipment_Specification__c es0 = new Equipment_Specification__c(name='Test-11');
            es0.Description__c = 'Test desc';
            es0.Price__c = 123.12;
            es0.Equipment_in_Room__c='DG'; 
            insert es0;            
                        
            Equipment_Specification__c es1 = new Equipment_Specification__c(name='Test-1aa');
            es1.Description__c = 'Test desc1';
            es1.Price__c = 123.12;
            es1.Plan_ID_Usage__c = '111';
            insert es1 ;
            
            Hospital_Room_No__c hr1 = new Hospital_Room_No__c();
            hr1.Room_No__c= '123';
            hr1.Department__c= 'ATRIUM';
            hr1.Room_Name__c = 'ALCOVE';
            hr1.Floor_No__c='LEVEL 1';
            hr1.RecordTypeId =[SELECT Id FROM RecordType WHERE DeveloperName = 'Dartmouth_General_Hospital' AND SobjectType = 'Hospital_Room_No__c' LIMIT 1].Id;
            insert hr1;
            
            // equipment in a rooms            
            Equipment_in_Room__c erd1= new Equipment_in_Room__c();
            erd1.Hospital_Room__c = hr1.Id;
            erd1.Plan_Id__c = es0.id;
            erd1.Room_Qty__c=1;
            erd1.Qty__c=2;
            insert erd1;            
          
            Equipment_in_Room__c erd2= new Equipment_in_Room__c();
            erd2.Hospital_Room__c = hr1.Id;
            erd2.Plan_Id__c = es1.id;
            erd2.Room_Qty__c=1;
            erd2.Qty__c=2;
            insert erd2;            
       
    }
    
    static testMethod void test_Halifax_Infirmary() {

            Equipment_Specification__c es0 = new Equipment_Specification__c(name='Test-11');
            es0.Description__c = 'Test desc';
            es0.Price__c = 123.12;
            es0.Equipment_in_Room__c='HI'; 
            insert es0;            
                        
            Equipment_Specification__c es1 = new Equipment_Specification__c(name='Test-1aa');
            es1.Description__c = 'Test desc1';
            es1.Price__c = 123.12;
            es1.Plan_ID_Usage__c = '111';
            insert es1 ;
            
            Hospital_Room_No__c hr1 = new Hospital_Room_No__c();
            hr1.Room_No__c= '123';
            hr1.Department__c= 'ATRIUM';
            hr1.Room_Name__c = 'ALCOVE';
            hr1.Floor_No__c='LEVEL 1';
            hr1.RecordTypeId =[SELECT Id FROM RecordType WHERE DeveloperName = 'Halifax_Infirmary' AND SobjectType = 'Hospital_Room_No__c' LIMIT 1].Id;
            insert hr1;
            
            // equipment in a rooms            
            Equipment_in_Room__c erd1= new Equipment_in_Room__c();
            erd1.Hospital_Room__c = hr1.Id;
            erd1.Plan_Id__c = es0.id;
            erd1.Room_Qty__c=1;
            erd1.Qty__c=2;
            insert erd1;            
          
            Equipment_in_Room__c erd2= new Equipment_in_Room__c();
            erd2.Hospital_Room__c = hr1.Id;
            erd2.Plan_Id__c = es1.id;
            erd2.Room_Qty__c=1;
            erd2.Qty__c=2;
            insert erd2;            
       
    }    
    
}

 

All Answers

v varaprasadv varaprasad
Hi Muna,

Update following 2 records in test class and check once.


 
Equipment_Specification__c es2 = new Equipment_Specification__c(name ='Test-1b');
            insert es2 ; 
            es2.Description__c = 'Test desc';
            es2.Price__c = 123.12;        
            es2.Equipment_in_Room__c='DG';
			update es2;
                         
            Equipment_Specification__c es3 = new Equipment_Specification__c(name ='Test-1c');
            insert es3 ; 
            es3.Description__c = 'Test desc';
            es3.Price__c = 123.12;        
            es3.Equipment_in_Room__c='HI';  
            update es3;

Please follow balesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required 
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .






Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Maharajan CMaharajan C
Hi Muna,
 
To Cover those lines you data should not meet the if logic and it have to meet else Iff logic

To avoid the if logic your data should be like below then only the result will be -1:

           Equipment_Specification__c es0 = new Equipment_Specification__c(name='Test-11');
            insert es0 ; 
            es0.Description__c = 'Test desc';
            es0.Price__c = 123.12;
            es0.Equipment_in_Room__c='RA';        
            
            
            Equipment_Specification__c es1 = new Equipment_Specification__c(name='Test-1aa');
            insert es1 ;
            es1.Description__c = 'Test desc1';
            es1.Price__c = 123.12;    
            es1.Equipment_in_Room__c='MU';        
   
Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
Abdul KhatriAbdul Khatri
Before fixing the test coverage, I would like to inform you that your code is error prone and it is likely to break for the bulk. I also see design weakness. I am really surprise to hear that you still get 76% of converage.

I would try to give you refactor the code and would likely ask you to test as per requirement. 

Please do not put this code in Prodcution. That is a serious suggestion else is up to you.
Muna Jamous5Muna Jamous5
Hi Abdul,
Thank you for your reply, please provide me with the refactor code, you help is highly appreciated

Regards
Muna
Abdul KhatriAbdul Khatri
Please read the following article
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

Here are the issues I have with your code.
  1. ​It is not bulkify, mean
    1. SOQL Query define in a for loop
    2. DML Operation in the for loop
  2. Strange design for example 
    1. Equipment_in_Room__c Field in the custom object Equipment_Specification__c have same name to the Custom Object Equipment_in_Room__c. Do they have any relationship.
  3. No where in the Test Class you have mentioned the Hospital_Name__c which you are using the class so How it is matching
  4. You are using Hospital_Room__c custom object Id in the Equipment_in_Room__c in the test class so not sure how is that being used.
  5. Also not sure what you are trying to do in the below code where in the SOQL you are put LIMIT 1 and then in the next line you are runing a for loop. Can you please how that is possible when you are saying give me only 1 record. Do we need for loop?
 
EquipmentDetails = [Select id, Name, Equipment_in_Room__c from Equipment_Specification__c where id =: r.Plan_Id__c limit 1];
                for (Equipment_Specification__c es : EquipmentDetails ) {
                    if (es.Equipment_in_Room__c == null ) {
Anyway best of my understanding to your code I come up with this logic. I am not sure if it is as per you requirement but based on the code you have provided it should mimic what your current code provide. This is written with best practices. Once you confirm the code below I will help you with the test coverage.
 
trigger updateEquipmentDeatilField on Equipment_in_Room__c (before insert, before update) {

	Set<Id> idPlanList = new Set<Id>();
    List<Equipment_Specification__c> esListToUpdate =  new List<>(Equipment_Specification__c);
    
	For (Equipment_in_Room__c r: Trigger.New) {
    	idPlanList.add(r.Plan_Id__c);
    }
	
    if(idPlanList.isEmpty()) return;

	Map<Id, Equipment_Specification__c> SpecMap = [Select id, Name, Equipment_in_Room__c from Equipment_Specification__c where id =: idPlanList];
    
    try {

        For (Equipment_in_Room__c r: Trigger.New) {
            
			Equipment_Specification__c es = SpecMap.get(r.Plan_Id__c);
            
            if(r.Hospital_Name__c.equalsIgnoreCase('Dartmouth General Hospital')) {
                
                if(es.Equipment_in_Room__c == null) {
                    es.Equipment_in_Room__c == 'DG';
                } else {			
					es.Equipment_in_Room__c = es.Equipment_in_Room__c + 'DG,';                
                }
                
            } else if(r.Hospital_Name__c.equalsIgnoreCase('Halifax Infirmary')) {
                
                if(es.Equipment_in_Room__c == null) {
                    es.Equipment_in_Room__c == 'HI';
                } else {
					es.Equipment_in_Room__c = es.Equipment_in_Room__c + 'HI,';
                }
            }
            
			esListToUpdate.add(es);
        }
        
        if(!esListToUpdate.isEmpty())
            update esListToUpdate;

	} catch(Exception e){
        System.Trigger.new[0].addError('Error in updateEquipmentDeatilField: '+e.getMessage());
   }
}

 
Muna Jamous5Muna Jamous5
I did the changes are sugested had to do minor changes to make the code work, the end result is as follows, just a note, I changed the field name Equipment_in_Room__c , as for the Hospital_Name__c it is a formula field, you thoughts are highly appreciated

trigger updateEquipmentDeatilField on Equipment_in_Room__c (before insert, before update) {
 Set<Id> idPlanList = new Set<Id>();
    List<Equipment_Specification__c> esListToUpdate =  new List<Equipment_Specification__c>();
   
 For (Equipment_in_Room__c r: Trigger.New) {
     idPlanList.add(r.Plan_Id__c);
    }
 
    if(idPlanList.isEmpty()) return;
 Map<Id, Equipment_Specification__c> SpecMap = new Map<Id, Equipment_Specification__c>([Select id, Name, Plan_ID_Usage__c from Equipment_Specification__c where id in :idPlanList]);
   
    try {
        For (Equipment_in_Room__c r: Trigger.New) {
           
   Equipment_Specification__c es = SpecMap.get(r.Plan_Id__c);
           
            if(r.Hospital_Name__c.equalsIgnoreCase('Dartmouth General Hospital')) {
               
                if(es.Plan_ID_Usage__c == null) {
                    es.Plan_ID_Usage__c = 'DG,';
                } else {   
     es.Plan_ID_Usage__c = es.Plan_ID_Usage__c + 'DG,';               
                }
               
            } else if(r.Hospital_Name__c.equalsIgnoreCase('Halifax Infirmary')) {
               
                if(es.Plan_ID_Usage__c == null) {
                    es.Plan_ID_Usage__c = 'HI,';
                } else {
     es.Plan_ID_Usage__c = es.Plan_ID_Usage__c + 'HI,';
                }
            }
           
   esListToUpdate.add(es);
        }
       
        if(!esListToUpdate.isEmpty())
            update esListToUpdate;
 } catch(Exception e){
        System.Trigger.new[0].addError('Error in updateEquipmentDeatilField: '+e.getMessage());
   }
}
Abdul KhatriAbdul Khatri
You mentioned 

Hospital_Name__c it is a formula field,

Can you share the formula in their?
Muna Jamous5Muna Jamous5
Hi Abdul, The formula is Hospital_Room__r.RecordType.Name Thanks Muna
Abdul KhatriAbdul Khatri
Please find the test class for the same. I am not fond of doing this way but because of lack of time and late in reply we can go with it but try to check the best practise of writing test class in this link 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm
 
@isTest 
public class updateEquipmentDetailFieldTest {

    static testMethod void test_Dartmouth_General_Hospital() {

            Equipment_Specification__c es0 = new Equipment_Specification__c(name='Test-11');
            es0.Description__c = 'Test desc';
            es0.Price__c = 123.12;
            es0.Equipment_in_Room__c='DG'; 
            insert es0;            
                        
            Equipment_Specification__c es1 = new Equipment_Specification__c(name='Test-1aa');
            es1.Description__c = 'Test desc1';
            es1.Price__c = 123.12;
            es1.Plan_ID_Usage__c = '111';
            insert es1 ;
            
            Hospital_Room_No__c hr1 = new Hospital_Room_No__c();
            hr1.Room_No__c= '123';
            hr1.Department__c= 'ATRIUM';
            hr1.Room_Name__c = 'ALCOVE';
            hr1.Floor_No__c='LEVEL 1';
            hr1.RecordTypeId =[SELECT Id FROM RecordType WHERE DeveloperName = 'Dartmouth_General_Hospital' AND SobjectType = 'Hospital_Room_No__c' LIMIT 1].Id;
            insert hr1;
            
            // equipment in a rooms            
            Equipment_in_Room__c erd1= new Equipment_in_Room__c();
            erd1.Hospital_Room__c = hr1.Id;
            erd1.Plan_Id__c = es0.id;
            erd1.Room_Qty__c=1;
            erd1.Qty__c=2;
            insert erd1;            
          
            Equipment_in_Room__c erd2= new Equipment_in_Room__c();
            erd2.Hospital_Room__c = hr1.Id;
            erd2.Plan_Id__c = es1.id;
            erd2.Room_Qty__c=1;
            erd2.Qty__c=2;
            insert erd2;            
       
    }
    
    static testMethod void test_Halifax_Infirmary() {

            Equipment_Specification__c es0 = new Equipment_Specification__c(name='Test-11');
            es0.Description__c = 'Test desc';
            es0.Price__c = 123.12;
            es0.Equipment_in_Room__c='HI'; 
            insert es0;            
                        
            Equipment_Specification__c es1 = new Equipment_Specification__c(name='Test-1aa');
            es1.Description__c = 'Test desc1';
            es1.Price__c = 123.12;
            es1.Plan_ID_Usage__c = '111';
            insert es1 ;
            
            Hospital_Room_No__c hr1 = new Hospital_Room_No__c();
            hr1.Room_No__c= '123';
            hr1.Department__c= 'ATRIUM';
            hr1.Room_Name__c = 'ALCOVE';
            hr1.Floor_No__c='LEVEL 1';
            hr1.RecordTypeId =[SELECT Id FROM RecordType WHERE DeveloperName = 'Halifax_Infirmary' AND SobjectType = 'Hospital_Room_No__c' LIMIT 1].Id;
            insert hr1;
            
            // equipment in a rooms            
            Equipment_in_Room__c erd1= new Equipment_in_Room__c();
            erd1.Hospital_Room__c = hr1.Id;
            erd1.Plan_Id__c = es0.id;
            erd1.Room_Qty__c=1;
            erd1.Qty__c=2;
            insert erd1;            
          
            Equipment_in_Room__c erd2= new Equipment_in_Room__c();
            erd2.Hospital_Room__c = hr1.Id;
            erd2.Plan_Id__c = es1.id;
            erd2.Room_Qty__c=1;
            erd2.Qty__c=2;
            insert erd2;            
       
    }    
    
}

 
This was selected as the best answer
Muna Jamous5Muna Jamous5
HI Abdul, It worked 😊 100% coverage, Thank you so much. Regards, Muna