I'm having a simple code where I'm cloning an opportunity object with the necessary child elements. When I'm writing a test class for it, the clone method is not called and it is going into catch block which in turn is not covering the class.

Here is the code


    public class OpportunityClone_Controller 
    public Opportunity opp{get;set;}

    public OpportunityClone_Controller (ApexPages.StandardController controller) 
       opp = (Opportunity)controller.getRecord();

 public PageReference save() 

         Opportunity newOpp = new Opportunity();
                newOpp = opp.clone(false);
                newOpp.recordTypeId = opp.recordTypeId;               

                insert newOpp;

                //Cloning Opportunity Product 
                List<Opportunity_Product__c> oppProduct= new List<Opportunity_Product__c>();
  for(Opportunity_Product__c newproduct : product)
                    Opportunity_Product__c cloneOP= new Opportunity_Product__c();
                    cloneOP= newproduct.clone(false,true);
                    cloneOP.opportunity__c = newOpp.id;

                insert oppProduct;
 catch (Exception e)
            return null;
         PageReference pg = new PageReference('/'+newOpp.id);
         ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Please ensure addition of assets for the relationship have been updated accordingly for this cloned opportunity.'));

         return pg;

My test class for this is

private class Test_OpportunityClone 
     testMethod static void testOpportunityClone()
          Opportunity opp = UnitTestObjC.CreateOpportunityObject();
          insert opp;
  opportunity_Product__c op = new Opportunity_Product__c(Product_Family__c = 'banking',product__c = pr.id,opportunity__c = opp.id);
          insert op;
          List<opportunity_Product__c> listOppProd = new List<opportunity_Product__c>(); 
 ApexPages.StandardController Scontroller = new ApexPages.StandardController(opp);                        
          OpportunityClone_Controller opportunityCloneObj = new OpportunityClone_Controller(Scontroller);
          PageReference pageRef=page.VF_OpportunityClonePage;
          opportunityCloneObj.opp = opp; 
 PageReference Ref = opportunityCloneObj.save();

In order to improve my code, try block should be covered.

Here is the image of my uncovered code:

enter image description here

  • For everyone else, it's important to note that Eagerin Sf isn't using OpportunityLineItem, but rather a Custom Object, Opportunity_Product__c. Before I realized this, part of the answer I was writing was going to mention that the OP would need to create a Product2 and then a PricebookEntry.
    – Derek F
    Oct 27 '15 at 13:55
  • 2
    The error is coming when you insert the opp. What is the error message in the debug log? comment out the try line and catch block and run the test and you will easily see what the error is and be able to report back
    – Eric
    Oct 27 '15 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.