You need to sign in to do that
Don't have an account?
Muna 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);
}
}
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);
}
}
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm
All Answers
Update following 2 records in test class and check once.
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
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
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.
Thank you for your reply, please provide me with the refactor code, you help is highly appreciated
Regards
Muna
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
Here are the issues I have with your code.
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());
}
}
Hospital_Name__c it is a formula field,
Can you share the formula in their?
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm