You need to sign in to do that
Don't have an account?
YY_m(_ _)m
APEX Trigger Test
Hello everyone.
I have been trying to test trigger code but I cannot reach to over 75% coverage. Can you give me tips to help me out, please?
This is this trigger does:
・This trigger runs when new Opportunity with certain record type is created.
・This trigger updates a field called "Application Progress" on Contact.
Here is trigger code
trigger updateAppProgress2CS on Opportunity (after insert) { //updateAppProgress2CS ID recType = '0121000000008C7AAI'; //Study Abroad Opportunity RecordType try{ //obtain Opportunity ID when new Opportunity is created for(Opportunity opp : Trigger.new){ //check if opportunity record type is study abroad or not Opportunity oppRec = [SELECT RECORDTYPEID FROM Opportunity WHERE ID = :opp.id]; if(oppRec.RECORDTYPEID == recType){ //obtain contact id from OpportunityContactRole OpportunityContactRole OppRole = [SELECT ContactId FROM OpportunityContactRole WHERE OpportunityId = :opp.id]; //Check if there is any contact record associated with Contact Role in Opportunity if(OppRole != null) { //get Application Progress value Contact con = [SELECT AppProg__c FROM Contact WHERE Id = :OppRole.ContactId]; //set Application Progress value con.AppProg__c = 'Course selection'; //update contact update con; }//end if }//end if }//end for }catch(Exception e){ } }
Here is a test code
@isTest private class updateAppProgress2CSTest{ static testMethod void runSuccessCase(){ //get existing account id (Account record for testing) Account acc = [select id from Account where name ='【ダミー】EIKOKU Hanako']; try{ //create an Opportunity(Study Abroad record type) Opportunity opp = new Opportunity(Name = 'Test Opportunity', AccountID = acc.Id, CloseDate=date.newInstance(1986,4,27), StageName='商品ご案内', RECORDTYPEID='0121000000008C7AAI'); //execute updateAppProgress2CS trigger. insert opp; //get contact id from ContactRole OpportunityContactRole OppRole = [SELECT ContactId FROM OpportunityContactRole WHERE Opportunity.Id = :Opp.Id]; system.assert(OppRole != null); //get Application Progress value Contact con = [SELECT AppProg__c FROM Contact WHERE Id = :OppRole.ContactId]; system.assert(con.AppProg__c == 'Course selection'); }catch(Exception e){ } } }
According to Testing result, my test code does not cover the code in the trigger(line 16~) and the coverage is 63%.
if(OppRole != null) { //get Application Progress value Contact con = [SELECT AppProg__c FROM Contact WHERE Id = :OppRole.ContactId]; //set Application Progress value con.AppProg__c = 'Course selection'; //update contact update con; }//end if
It could be that when you query the OpportunityContactRole in the trigger it is not returning a value which will throw and error when trying to put in an object. Better to use a list and then check if the list is empty. Like this:
btw, never a good idea to hardcode Ids in your classes as they can change from org to org like from sandbox to production.
You are going to run into another problem when you try and deploy ... you have hard-coded the recordTypeId in the trigger. This id is not going to be the same in your dev edition/sandbox compared to PROD.
You need to write the trigger to fetch the recordTypeId by using a SOQL select statement; something like this:
Hi Eric
Thank you for your comments.
I understand and fixed this hard-coded ID.
However, I still don't have any progress on testing. The coverage is still 63% and error says that the following code is not tested yet.
My trigger is fine and works as I expect though...
Is there anything that I'm missing in Test code?
YY_m(_ _)m
YY_m(_ _)m:
Your testmethod needs to Insert an OpportunityContactRole in order so this Select statement returns non-null:
Testmethods have to be super explicit and build up all the database objects you need to test.