• YY_m(_ _)m
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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

 

 

 

 

 

 

 

 

 

 

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