+ Start a Discussion
SalesforceCrm AccountCRMSalesforceCrm AccountCRM 

how to call string.valueof() in a test class

Can any one help me out.The code coverage is getting low.In the code i have a String.value of(),how to call this in a test class.Any help very much appreciated.
Trigger :
trigger UpdateRenewableFields on Contract(after insert) 
{
    Set<Id> oliIds = new Set<Id>(); 
    Map<string, Contract> contractMap = new Map<string, Contract>();
    
    List<OpportunityLineItem> oliIdsToUpdate = new List<OpportunityLineItem>();
    
    for(Contract c: Trigger.new)
    {
        if(c.Opportunity_Product_Id__c != null && c.Opportunity_Product_Id__c != '')
        {
            oliIds.add(c.Opportunity_Product_Id__c); 
            if(c.Parent_Opportunity_Product_Id__c != null && c.Parent_Opportunity_Product_Id__c != '')
            {       
                contractMap.put(c.Parent_Opportunity_Product_Id__c , c);
            }
        }
    }
    if(oliIds.size() > 0)
    {
        //Get Parent OLI Ids//
        List<OpportunityLineItem> parentOliList = [Select Id,Parent_Opportunity_Product_Id__c, Contract__c from OpportunityLineItem where Id in: oliIds];
        Set<Id> parentOlis = new Set<Id>();
        Map<Id, OpportunityLineItem> oliMap = new Map<Id, OpportunityLineItem>();
        for(OpportunityLineItem oli:parentOliList)
        {   
            oliMap.put(oli.id, oli);///Map use to update OLI
            parentOlis.add(oli.Parent_Opportunity_Product_Id__c);
        }
        
        List<Contract> parentContractList = [Select Id,Renewal_Contracts_Numbers__c,Opportunity_Name__c,Opportunity_Product_Id__c,  Renewal_Status__c,Renewal_Amount__c,Renewal_ARR__c from Contract where Opportunity_Product_Id__c in: parentOlis];
        
        List<Contract> contractsToUpdate = new List<Contract>();
        
        for(Contract parent: parentContractList)
        {
            if(parent.Opportunity_Product_Id__c != null && parent.Opportunity_Product_Id__c != '' && contractMap.containsKey(String.valueOf(parent.Opportunity_Product_Id__c)))
            {
                Contract child = contractMap.get(String.valueOf(parent.Opportunity_Product_Id__c));
                //parent.Renewal_Contract__c = true ;
                parent.Renewal_Contracts_Numbers__c = child.Id ;
                parent.Renewal_Status__c = 'Renewed';
                parent.Renewal_Amount__c = child.Subtotal_Price__c ;
                parent.Renewal_ARR__c = child.Annual_Recurring_Revenue__c ;
                
                if(parent.Multi_year_Sub_extension_contract__c)
                {
                    parent.Actual_renewal_date__c = child.Actual_renewal_date__c;
                }
                contractsToUpdate.add(parent);
            }
        }
        System.debug('contractsToUpdate::' + contractsToUpdate);
        
        ///Updating Opportunity Line Item with Contract Number////
        for(Contract c: Trigger.new)
        {
            if(oliMap.containsKey(c.Opportunity_Product_Id__c))
            {
                OpportunityLineItem oli = oliMap.get(c.Opportunity_Product_Id__c);
                oli.Contract__c = c.Id;
                oliIdsToUpdate.add(oli);
            }
        }
        /////////////////////////////////////////////////////////
        try
        {
            if(contractsToUpdate.size() > 0)
            {
                update contractsToUpdate;
            }
            if(oliIdsToUpdate.size() > 0)
            {
                update oliIdsToUpdate; 
            }
        }
        catch(Exception e)
        {
            System.debug('ERROR::' + e.getMessage());
        }
    }
}
Test Class :
@IsTest
public class TestUpdateRenewableFields{
static testmethod void testUpdateRenewable(){
Account acc10 = new Account(Name='Testing SubscriptionClone', BillingStreet='Banjara hills', BillingCity='Hyd', BillingState='TS',
                                    BillingPostalCode = '500084', BillingCountry = 'India', Phone = '100', Industry = 'Banking',
                                    Type = 'Paid', Customer_Type__c = 'Customer', Customer_List__c = true,PPAS__c = false,
                                    PPCD__c = false,PPSP__c = false, PGSQL__c = false,PPAS_Developer_Sub__c = false,      
                                    PPSP_Developer_Sub__c = false, PGSQL_Developer_Sub__c = false,RDBA__c = false,  
                                    Services__c = false,Training__c = false,SteelEye__c = false,Jump_Start_Subscription__c = false    );  
        insert acc10 ;
        
        
        contact cc10 = new contact(FirstName ='Test Contact Subscription', LastName ='Opptyclone', Role__c='Subscription Administrator',AccountId=acc10.Id);
        insert cc10;
        
        Opportunity opty10 = new Opportunity(Name = 'Test SubscriptionOppty', StageName ='Proposal/Price Quote', Probability =60, 
                                            CloseDate = System.Today(), Type = 'Existing Customer', Assigned_Sales_Engineer__c = null, 
                                            Update_Complete__c = true, Partner__c = 'None', Anchor__c='Non Anchor', AccountId = acc10.Id,
                                            Payment_Type__c = 'Order Form', LeadSource = 'Subscription Renewal');
        insert opty10;
        
        
        
        
        list<OpportunityContactRole> opptyConRolelist15 =  new list<OpportunityContactRole>{
            new OpportunityContactRole(ContactId = cc10.Id, OpportunityId = opty10.Id, IsPrimary = false, Role = 'Subscription Administrator')
        };   
        insert opptyConRolelist15;
        
        product2 p2 = new product2(Name ='Postgres Plus Enterprise Edition Unicores', Product_Name1__C='Product - Subscription', Product_Group__c='PPAS',
                                    IsActive = true, Description='Postgres Plus Enterprise Edition Unicores', ARR_Impact1__c = 'Yes');
        insert p2;
        
        PricebookEntry pbey = new PricebookEntry(Product2ID=p2.id, Pricebook2ID= test.getStandardPricebookId(), UnitPrice=50, isActive=true);
        insert pbey;
        
        Opportunitylineitem ooli10 = new Opportunitylineitem(OpportunityId = opty10.Id, PricebookEntryId = pbey.Id, Quantity =4, 
                                    Start_Date__c = System.today(), End_Date__c =  System.today() + 100, Type_of_Contract__c='None',
                                    Term__c =12, Type_Of_Product__c='Single Year Subscription', Type_of_ARR__c= 'New Addition ARR',
                                    TotalPrice=1750, No_ARR__c = false, In_Year_Billing__c = 10);
        insert ooli10;
        
        Opportunitylineitem ooli15 = new Opportunitylineitem(OpportunityId = opty10.Id, PricebookEntryId = pbey.Id, Quantity =4, 
                                    Start_Date__c = System.today(), End_Date__c =  System.today() + 100, Type_of_Contract__c='None',
                                    Term__c =12, Type_Of_Product__c='Single Year Subscription', Type_of_ARR__c= 'New Addition ARR',
                                    TotalPrice=1750, No_ARR__c = false, In_Year_Billing__c = 10, Parent_Opportunity_Product_Id__c=ooli10.Id);
        insert ooli15;
        
       // list<Contract> contrlist = new list<Contract>{
          Contract contrlist= new Contract(CurrencyIsoCode='USD', Annual_Contract_Value__c =5000, Type_Of_Product__c='Single Year Subscription',
                                        Notes__c='Contract is getting created', Opportunity_Product_Id__c= ooli10.Id, Contracts_In_Year_Billing__c=1500,
                                       AccountId=acc10.Id);
                                       
         
        insert contrlist;
        
        Contract contr1 = new Contract(CurrencyIsoCode='USD', Annual_Contract_Value__c =5000, Type_Of_Product__c='Single Year Subscription',
                                        Notes__c='Contract is getting created', Opportunity_Product_Id__c= ooli10.Id, Contracts_In_Year_Billing__c=1500,
                                       AccountId=acc10.Id);
          
          insert contr1;                          
         Test.StartTest();
        contrlist.Renewal_Contracts_Numbers__c=contr1.id;
        contrlist.Renewal_ARR__c=contr1.Annual_Recurring_Revenue__c;
        contrlist.Renewal_Amount__c=contr1.Subtotal_Price__c;
        contrlist.Renewal_Status__c='Renewed';
        update contr1;
        Test.StopTest();
        
        }
        }
The lines which are not getting covered :
{
            if(parent.Opportunity_Product_Id__c != null && parent.Opportunity_Product_Id__c != '' && contractMap.containsKey(String.valueOf(parent.Opportunity_Product_Id__c)))
            {
                Contract child = contractMap.get(String.valueOf(parent.Opportunity_Product_Id__c));
                //parent.Renewal_Contract__c = true ;
                parent.Renewal_Contracts_Numbers__c = child.Id ;
                parent.Renewal_Status__c = 'Renewed';
                parent.Renewal_Amount__c = child.Subtotal_Price__c ;
                parent.Renewal_ARR__c = child.Annual_Recurring_Revenue__c ;
                
                if(parent.Multi_year_Sub_extension_contract__c)
                {
                    parent.Actual_renewal_date__c = child.Actual_renewal_date__c;
                }
                contractsToUpdate.add(parent);
            }
        }
Any help very much appreciated.


 
Best Answer chosen by SalesforceCrm AccountCRM
SalesforceCrm AccountCRMSalesforceCrm AccountCRM
@JeffreyStevens: i tried adding this field  [Parent_Opportunity_Product_Id__c = string.valueOf(ooli10.Id)] in contr1 ,and moved the Test.startTest() above the insert contrlist :and now the code coverage has increased and reached to (97%).Thanks for the needful help.
@IsTest
public class TestUpdateRenewableFields{
static testmethod void testUpdateRenewable(){
    
        Account acc10 = new Account(Name='Testing SubscriptionClone', BillingStreet='Banjara hills', BillingCity='Hyd', BillingState='TS',
                                        BillingPostalCode = '500084', BillingCountry = 'India', Phone = '100', Industry = 'Banking',
                                        Type = 'Paid', Customer_Type__c = 'Customer', Customer_List__c = true,PPAS__c = false,
                                        PPCD__c = false,PPSP__c = false, PGSQL__c = false,PPAS_Developer_Sub__c = false,      
                                        PPSP_Developer_Sub__c = false, PGSQL_Developer_Sub__c = false,RDBA__c = false,  
                                        Services__c = false,Training__c = false,SteelEye__c = false,Jump_Start_Subscription__c = false    );  
        insert acc10 ;
        
        
        contact cc10 = new contact(FirstName ='Test Contact Subscription', LastName ='Opptyclone', Role__c='Subscription Administrator',AccountId=acc10.Id);
        insert cc10;
        
        Opportunity opty10 = new Opportunity(Name = 'Test SubscriptionOppty', StageName ='Proposal/Price Quote', Probability =60, 
                                            CloseDate = System.Today(), Type = 'Existing Customer', Assigned_Sales_Engineer__c = null, 
                                            Update_Complete__c = true, Partner__c = 'None', Anchor__c='Non Anchor', AccountId = acc10.Id,
                                            Payment_Type__c = 'Order Form', LeadSource = 'Subscription Renewal');
        insert opty10;

        list<OpportunityContactRole> opptyConRolelist15 =  new list<OpportunityContactRole>{
            new OpportunityContactRole(ContactId = cc10.Id, OpportunityId = opty10.Id, IsPrimary = false, Role = 'Subscription Administrator')
        };   
        insert opptyConRolelist15;
        
        product2 p2 = new product2(Name ='Postgres Plus Enterprise Edition Unicores', Product_Name1__C='Product - Subscription', Product_Group__c='PPAS',
                                    IsActive = true, Description='Postgres Plus Enterprise Edition Unicores', ARR_Impact1__c = 'Yes');
        insert p2;
        
        PricebookEntry pbey = new PricebookEntry(Product2ID=p2.id, Pricebook2ID= test.getStandardPricebookId(), UnitPrice=50, isActive=true);
        insert pbey;
        
        Opportunitylineitem ooli10 = new Opportunitylineitem(OpportunityId = opty10.Id, PricebookEntryId = pbey.Id, Quantity =4, 
                                    Start_Date__c = System.today(), End_Date__c =  System.today() + 100, Type_of_Contract__c='None',
                                    Term__c =12, Type_Of_Product__c='Single Year Subscription', Type_of_ARR__c= 'New Addition ARR',
                                    TotalPrice=1750, No_ARR__c = false, In_Year_Billing__c = 10);
        insert ooli10;
        
        Opportunitylineitem ooli15 = new Opportunitylineitem(OpportunityId = opty10.Id, PricebookEntryId = pbey.Id, Quantity =4, 
                                    Start_Date__c = System.today(), End_Date__c =  System.today() + 100, Type_of_Contract__c='None',
                                    Term__c =12, Type_Of_Product__c='Single Year Subscription', Type_of_ARR__c= 'New Addition ARR',
                                    TotalPrice=1750, No_ARR__c = false, In_Year_Billing__c = 10, Parent_Opportunity_Product_Id__c=ooli10.Id);
        insert ooli15;
        
      // list<Contract> contrlist = [Select Id,Renewal_Contracts_Numbers__c,Opportunity_Name__c,Opportunity_Product_Id__c,  Renewal_Status__c,Renewal_Amount__c,Renewal_ARR__c from Contract where Opportunity_Product_Id__c in:ooli10.Id];
         Contract parentcontrlist= new Contract(CurrencyIsoCode='USD', Annual_Contract_Value__c =5000, Type_Of_Product__c='Single Year Subscription',
                                        Notes__c='Contract is getting created', Opportunity_Product_Id__c= ooli10.Id, Contracts_In_Year_Billing__c=1500,
                                       AccountId=acc10.Id);
                                       
        Test.StartTest();
        insert parentcontrlist;
        
        Contract childcontr1 = new Contract(CurrencyIsoCode='USD', Annual_Contract_Value__c =5000, Type_Of_Product__c='Single Year Subscription',
                                        Notes__c='Contract is getting created',Parent_Opportunity_Product_Id__c = string.valueOf(ooli10.Id), Opportunity_Product_Id__c= ooli15.Id, Contracts_In_Year_Billing__c=1500,
                                       AccountId=acc10.Id);
          
          insert childcontr1;  
          
          
           //Map<string, Contract> contractMap = new Map<string, Contract>();
          // contractMap.put(contrlist.Parent_Opportunity_Product_Id__c , contrlist);
                                   
         
        parentcontrlist.Renewal_Contracts_Numbers__c=childcontr1.id;
        parentcontrlist.Renewal_ARR__c=childcontr1.Annual_Recurring_Revenue__c;
        parentcontrlist.Renewal_Amount__c=childcontr1.Subtotal_Price__c;
        parentcontrlist.Renewal_Status__c='Renewed';
        parentcontrlist.Actual_renewal_date__c = childcontr1.Actual_renewal_date__c;
        update childcontr1;
        Test.StopTest();
        
        }
        }

All Answers

JeffreyStevensJeffreyStevens
string.valueOf isn't your issue.  You need to make sure that parent.Opportunity_Product_Id__c has a value, and that contractMap contains that value. 
GauravTrivediGauravTrivedi
Hey, Jeffrey is right you need to check the size of you map(contractMap), if contractMap has some values which is equal to parent.Opportunity_Product_Id__c then it will definately cover your code. Try to create test data for this scenario. If you need quick soloution use Test.isRunningTest(), it will cover your code when you are testing.

You have to user like this :
if((parent.Opportunity_Product_Id__c != null && parent.Opportunity_Product_Id__c != '' && contractMap.containsKey(String.valueOf(parent.Opportunity_Product_Id__c))) || Test.isRunningTest())

 
SalesforceCrm AccountCRMSalesforceCrm AccountCRM
@JeffreyStevens ,@GauravTrivedi :Thanks for your response.
@GauravTrivedi :As Suggested i have tried using the Test.isRunningTest() ,but the code coverage remains the same.Any help very much appreciated.
@JeffreyStevens:The Opportunity_Product_Id__c has the value assigned.Any suggestion please.
JeffreyStevensJeffreyStevens
so, if line#35 is geting executed, and #37 isn't - then put some debug statements before 37 and verify the values that you are testing.  LIke these lines...
system.debug('parent.Opportunity_Product_Id__c='+parent.Opportunity_Product_Id__c);
system.debug('contractMap.keyset()='+contractmap.keyset());

Your code looks correct - but obviously parent.Opportunity_Product_Id__c and/or contractMap don't have the values you think they have.  

SalesforceCrm AccountCRMSalesforceCrm AccountCRM
@JeffreyStevens: i tried adding this field  [Parent_Opportunity_Product_Id__c = string.valueOf(ooli10.Id)] in contr1 ,and moved the Test.startTest() above the insert contrlist :and now the code coverage has increased and reached to (97%).Thanks for the needful help.
@IsTest
public class TestUpdateRenewableFields{
static testmethod void testUpdateRenewable(){
    
        Account acc10 = new Account(Name='Testing SubscriptionClone', BillingStreet='Banjara hills', BillingCity='Hyd', BillingState='TS',
                                        BillingPostalCode = '500084', BillingCountry = 'India', Phone = '100', Industry = 'Banking',
                                        Type = 'Paid', Customer_Type__c = 'Customer', Customer_List__c = true,PPAS__c = false,
                                        PPCD__c = false,PPSP__c = false, PGSQL__c = false,PPAS_Developer_Sub__c = false,      
                                        PPSP_Developer_Sub__c = false, PGSQL_Developer_Sub__c = false,RDBA__c = false,  
                                        Services__c = false,Training__c = false,SteelEye__c = false,Jump_Start_Subscription__c = false    );  
        insert acc10 ;
        
        
        contact cc10 = new contact(FirstName ='Test Contact Subscription', LastName ='Opptyclone', Role__c='Subscription Administrator',AccountId=acc10.Id);
        insert cc10;
        
        Opportunity opty10 = new Opportunity(Name = 'Test SubscriptionOppty', StageName ='Proposal/Price Quote', Probability =60, 
                                            CloseDate = System.Today(), Type = 'Existing Customer', Assigned_Sales_Engineer__c = null, 
                                            Update_Complete__c = true, Partner__c = 'None', Anchor__c='Non Anchor', AccountId = acc10.Id,
                                            Payment_Type__c = 'Order Form', LeadSource = 'Subscription Renewal');
        insert opty10;

        list<OpportunityContactRole> opptyConRolelist15 =  new list<OpportunityContactRole>{
            new OpportunityContactRole(ContactId = cc10.Id, OpportunityId = opty10.Id, IsPrimary = false, Role = 'Subscription Administrator')
        };   
        insert opptyConRolelist15;
        
        product2 p2 = new product2(Name ='Postgres Plus Enterprise Edition Unicores', Product_Name1__C='Product - Subscription', Product_Group__c='PPAS',
                                    IsActive = true, Description='Postgres Plus Enterprise Edition Unicores', ARR_Impact1__c = 'Yes');
        insert p2;
        
        PricebookEntry pbey = new PricebookEntry(Product2ID=p2.id, Pricebook2ID= test.getStandardPricebookId(), UnitPrice=50, isActive=true);
        insert pbey;
        
        Opportunitylineitem ooli10 = new Opportunitylineitem(OpportunityId = opty10.Id, PricebookEntryId = pbey.Id, Quantity =4, 
                                    Start_Date__c = System.today(), End_Date__c =  System.today() + 100, Type_of_Contract__c='None',
                                    Term__c =12, Type_Of_Product__c='Single Year Subscription', Type_of_ARR__c= 'New Addition ARR',
                                    TotalPrice=1750, No_ARR__c = false, In_Year_Billing__c = 10);
        insert ooli10;
        
        Opportunitylineitem ooli15 = new Opportunitylineitem(OpportunityId = opty10.Id, PricebookEntryId = pbey.Id, Quantity =4, 
                                    Start_Date__c = System.today(), End_Date__c =  System.today() + 100, Type_of_Contract__c='None',
                                    Term__c =12, Type_Of_Product__c='Single Year Subscription', Type_of_ARR__c= 'New Addition ARR',
                                    TotalPrice=1750, No_ARR__c = false, In_Year_Billing__c = 10, Parent_Opportunity_Product_Id__c=ooli10.Id);
        insert ooli15;
        
      // list<Contract> contrlist = [Select Id,Renewal_Contracts_Numbers__c,Opportunity_Name__c,Opportunity_Product_Id__c,  Renewal_Status__c,Renewal_Amount__c,Renewal_ARR__c from Contract where Opportunity_Product_Id__c in:ooli10.Id];
         Contract parentcontrlist= new Contract(CurrencyIsoCode='USD', Annual_Contract_Value__c =5000, Type_Of_Product__c='Single Year Subscription',
                                        Notes__c='Contract is getting created', Opportunity_Product_Id__c= ooli10.Id, Contracts_In_Year_Billing__c=1500,
                                       AccountId=acc10.Id);
                                       
        Test.StartTest();
        insert parentcontrlist;
        
        Contract childcontr1 = new Contract(CurrencyIsoCode='USD', Annual_Contract_Value__c =5000, Type_Of_Product__c='Single Year Subscription',
                                        Notes__c='Contract is getting created',Parent_Opportunity_Product_Id__c = string.valueOf(ooli10.Id), Opportunity_Product_Id__c= ooli15.Id, Contracts_In_Year_Billing__c=1500,
                                       AccountId=acc10.Id);
          
          insert childcontr1;  
          
          
           //Map<string, Contract> contractMap = new Map<string, Contract>();
          // contractMap.put(contrlist.Parent_Opportunity_Product_Id__c , contrlist);
                                   
         
        parentcontrlist.Renewal_Contracts_Numbers__c=childcontr1.id;
        parentcontrlist.Renewal_ARR__c=childcontr1.Annual_Recurring_Revenue__c;
        parentcontrlist.Renewal_Amount__c=childcontr1.Subtotal_Price__c;
        parentcontrlist.Renewal_Status__c='Renewed';
        parentcontrlist.Actual_renewal_date__c = childcontr1.Actual_renewal_date__c;
        update childcontr1;
        Test.StopTest();
        
        }
        }
This was selected as the best answer