You need to sign in to do that
Don't have an account?
sudhirn@merunetworks.com
Test Class for try and catch block
Hi,
I modified below trigger by adding catch exception to handle exception scenarious for some reasons test class which i wrote earlier is getting falied can you please suggest me what is the mistake in the trigger and test class to get this failed.
Trigger
Test Class
Thanks
Sudhir
I modified below trigger by adding catch exception to handle exception scenarious for some reasons test class which i wrote earlier is getting falied can you please suggest me what is the mistake in the trigger and test class to get this failed.
Trigger
trigger lead_territory_lookup on Lead (After Update) { if(checkRecursive_LeadTerritory.runOnce()) { String gcountry; String gstate; Integer gzip; for(Lead l : Trigger.new){ gstate = l.State; gcountry = l.Country; if ( l.postalcode != null) { gzip = Integer.valueof(l.postalcode); } } Territory_Lookup__c TL; if ( gcountry != null ) { Try { if ( trigger.isInsert || trigger.isupdate ) { if ( gcountry != null && gstate != null && gzip != null ) { TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c FROM Territory_Lookup__c WHERE Country__c = :gcountry and State__c = :gstate and Zip_Start__c <= :gzip and Zip_End__c >= :gzip limit 1]; system.debug('Country & State & Zip'); } else if ( gcountry != null && gstate != null ) { TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c FROM Territory_Lookup__c WHERE Country__c = :gcountry and State__c = :gstate limit 1]; system.debug('Country & State'); } else if ( gcountry != null && gzip != null) { TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c FROM Territory_Lookup__c WHERE Country__c = :gcountry and Zip_Start__c <= :gzip and Zip_End__c >= :gzip limit 1]; system.debug('Country & Zip'); } else if ( gcountry != null ) { TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c FROM Territory_Lookup__c WHERE Country__c = :gcountry limit 1]; system.debug('Country'); } else { TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c FROM Territory_Lookup__c WHERE Country__c = :gcountry limit 1]; system.debug('Country'); } system.debug('ID Name:' + TL.ID); system.debug('Country Name:' + TL.Country__c); system.debug('State Name:' + TL.State__c); system.debug('Zip Start:' + TL.Zip_Start__c); system.debug('Zip End:' + TL.Zip_End__c); List<Lead> leds = new List<Lead>(); for(Lead uld : Trigger.new){ Lead uleds = new Lead( Id = uld.id,Territory_Lookup__c = TL.ID); leds.add(uleds); } update leds; } } catch (Exception e) { List<Lead> leds = new List<Lead>(); if ( gcountry != null && gstate != null ) { TL = [SELECT id,Country__c,State__c,Zip_Start__c,Zip_End__c FROM Territory_Lookup__c WHERE Country__c = :gcountry and State__c = :gstate limit 1]; system.debug('Country & State'); } else { TL = null; } for(Lead uld : Trigger.new){ Lead uleds = new Lead( Id = uld.id,Territory_Lookup__c = TL.ID); leds.add(uleds); } update leds; } } } }
Test Class
@isTest private class lead_territory_lookup_Test { static testMethod void protectFields(){ test.startTest(); List<Lead> lstLead = new List<Lead>{ new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open', state='Karnataka',country='India',postalcode='5',LeadSource='Community') }; insert lstLead; List<Territory_Lookup__c> lstTerritory = new List<Territory_Lookup__c>{ new Territory_Lookup__c(Country__c = 'India',State__c ='Goa', Zip_End__c = 50, Zip_Start__c = 51) }; insert lstTerritory; lstLead[0].LastName = 'Mike'; lstLead[0].Territory_Lookup__c = lstTerritory[0].id; update lstLead; test.stopTest(); } }
Thanks
Sudhir
Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NamedAccountOwner: execution of AfterInsert
caused by: System.DmlException: Update failed. First exception on row 0 with id 00Q18000001iVcjEAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, lead_territory_lookup: execution of AfterUpdate
caused by: System.QueryException: List has no rows for assignment to SObject
Trigger.lead_territory_lookup: line 111, column 1: []
Trigger.NamedAccountOwner: line 19, column 1: []Stack TraceClass.lead_territory_lookup_Test.protectFields: line 13, column 1
Test Class not covered
Thanks
Sudhir
Instead use this:
When I run test class I am getting below error message.
Error Message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, contact_territory_lookup: execution of AfterInsert
caused by: System.ListException: List index out of bounds: 0
Trigger.contact_territory_lookup: line 144, column 1: []
Stack Trace Class.Contact_territory_lookup_Test.protectFields: line 26, column 1
Thanks
Sudhir
Thanks
Sudhir
One thing to take into consideration is that you have a very large block of code that run inside try/catch blocks. Example - lines 121-155 are not going to execute unless you get stuff that goes way wrong in the try block which is also fairly large.
In my experience, the less logic you can put in the trigger the easier it is troublshoot, maintain, test.
Right now in order to test lines 121-155 is to force and exception during test exection. Or an alternative might be to put the logic in methods in a TerritoryLookupHelper. Without even really looking at what the code does to try to remove redundency you could simple have two methods
List<Contact> TerritoryLookupHelper.HappyPath(List contacts)
List<Contact> TerritoryLookupHelper.NonHappyPath (List contacts)
Example of method in In a helper class
This allows you to call each of them from the test class and bypass try/catch in the trigger. You can literally just call the methods directly with contacts you've created in the test setup and inspect what contacts each method returns and make your assertions.
What this allows you to do is cleanup the trigger code so that all it does is in the try it calls TerritoryLookupHelper.HappyPath and in the catch it calls TerritoryLookupHelper.NonHappyPath.
Of course you want to give the methods meaningful names - don't use my example as is.
Your trigger then ends up with a lot less code and if you ever need to use this logic in other places you abtracted it out of a trigger which is good design. Does that made sense?
However, If you just want to cover the catch statement, you could try creating a Contact with a non-numerical value:
If you can provide the code that is not covered, that would be helpful.
Please find the test class below can you please help me to modify the code below
Thanks
Sudhir
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
For example, after you inser the account on line 11, I'd add a line right after (even just a temporary debug to spit out Salesforce ID of the new account). Do the same after the insert of the Territory. That way when you execute the test, you can look at the full stack trace.
Can you zip up all the classes/triggers and project and attach it? Just zip the whole source directory along with the objects from Eclipse. If I get time later I'll recreate them in a test org.
what is it that you need help with?
Do you need to know how to create the trigger handler?
Test Factory?
Did you also follow Lars' advice?
Thanks