+ Start a Discussion
sfg1sfg1 

code coverage for constructor

In my class i have below constructor, i am not able to cover code coverage.can some pls guide me, thanks. 
Constructor: takes an Opporutnity action, color index and associate file
*/
    public OpportunityAction(Opportunity_Action__c oppAction, Integer colorIndex, Files__c file) {
        this.oppAction = oppAction;
        this.oppPlan = oppAction.Opportunity_Plan__r;
        this.colorIndex = colorIndex;
        this.associatedFile = file;
        planHeader = false;
    }
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
@isTest
public class MyTest{
   public static testmethod void test1(){
        OpportunityAction op=new OpportunityAction(null,4,null);
   }
}

Let us know if it helps.
sfg1sfg1
Hi Bala, thanks for your quick reply, If i use the above code i am getting "System.NullPointerException: Attempt to de-reference a null object" error. AlreadyI have created test data for 'Opportunity_Action__c'.PLease find my main class and guide me. thanks.

Main class:
/**
Inner class for wrapping opp actions with relevant information
also assists in displaying of data (colorIndex, etc) 
*/
public Class OpportunityAction {
    public Opportunity_Action__c oppAction {get;set;}
    public Opportunity_Plan__c oppPlan {get;set;}
    public Boolean checked {get;set;}
    public Boolean planHeader {get;set;}
    public Integer colorIndex {get;set;}
    public Files__c associatedFile {get;set;}
  public static List<String> planColors = new List<String> {'DarkSlateBlue', 'Orange', 'SteelBlue', 'Sienna','DarkSeaGreen','Indigo','LightSeaGreen','DarkGreen','Navy', 'Chocolate', 'DarkCyan', 'CadetBlue', 'Crimson', 'DarkGoldenRod', 'FireBrick', 'ForestGreen'};
      
    public String title {
        get {
            if (title == null && oppAction != null) {
                title = oppAction.Name;
            }
            return title;
        }
        set;
    }
    
    public String actionId {
        get {
            if (oppAction != null && oppAction.ID != null) {
                return oppAction.ID;
            } else {
                return '0';
            }
        }
    }
    
    /**
Constructor: takes an Opporutnity action and color index
*/
    public OpportunityAction(Opportunity_Action__c oppAction, Integer colorIndex) {
        this.oppAction = oppAction;
        this.colorIndex = colorIndex;
        this.oppPlan = oppAction.Opportunity_Plan__r;
        planHeader = false;
    }
    
    /**
Constructor: takes an Opporutnity action, color index and associate file
*/
    public OpportunityAction(Opportunity_Action__c oppAction, Integer colorIndex, Files__c file) {
        this.oppAction = oppAction;
        this.oppPlan = oppAction.Opportunity_Plan__r;
        this.colorIndex = colorIndex;
        this.associatedFile = file;
        planHeader = false;
    }
    
    /**
Constructor: takes an Opporutnity Plan and color index
*/
    public OpportunityAction(Opportunity_Plan__c oppPlan, Integer colorIndex) {
        this.oppPlan = oppPlan;
        this.title = oppPlan.Plan_Template__r.name;
        this.colorIndex = colorIndex;
        oppAction = new Opportunity_Action__c(Name = title, Status__c = oppPlan.Status__c);
        
        planHeader = true;
    }
    
    public String statusColor {
        get {
            if (oppAction != null) {
                if (oppAction.Status__c =='Pending') {
                    return 'gray';
                } else if (oppAction.Status_Category__c == 'Overdue' ) {
                    return 'red';
                } else {
                    return 'black';
                }
            }
            return 'black';
        }
    }
    
    public String planColor {
        get {
            if (colorIndex != null) {
                return OpportunityAction.planColors[colorIndex];
            } else {
                return '';
            }
        }
    }
    
    public String editAction {
        get {
            PageReference ref = Page.OpportunityActionEdit;
            ref.getParameters().put('ID', oppAction.ID);   
            if (oppPlan.Amendment__c != null) {
                ref.getParameters().put('saveURL','/' + oppPlan.Amendment__c);
                ref.getParameters().put('retURL','/' + oppPlan.Amendment__c);
            } else {
                ref.getParameters().put('saveURL','/' + oppPlan.Opportunity__c);
                ref.getParameters().put('retURL','/' + oppPlan.Opportunity__c);
            }
          return ref.getUrl();
        }
        private set;
    }
    
    public String closeAction {
        get {
            PageReference ref = Page.OpportunityActionEdit;
            ref.getParameters().put('ID', oppAction.ID);      
            ref.getParameters().put('st', 'Complete');
            if (oppPlan.Amendment__c != null) {
                ref.getParameters().put('saveURL','/' + oppPlan.Amendment__c);
                ref.getParameters().put('retURL','/' + oppPlan.Amendment__c);
            } else {
                ref.getParameters().put('saveURL','/' + oppPlan.Opportunity__c);
                ref.getParameters().put('retURL','/' + oppPlan.Opportunity__c);
            }
          return ref.getUrl();
        }
        private set;

    }
    
    public String editPlan {
        get {
            PageReference ref = Page.OpportunityPlanEdit;
            ref.getParameters().put('ID', oppPlan.ID);  
            if (oppPlan.Amendment__c != null) {
                ref.getParameters().put('saveURL','/' + oppPlan.Amendment__c);
                ref.getParameters().put('retURL','/' + oppPlan.Amendment__c);
            } else {
                ref.getParameters().put('saveURL','/' + oppPlan.Opportunity__c);
                ref.getParameters().put('retURL','/' + oppPlan.Opportunity__c);
            }
          return ref.getUrl();
        }
        private set;

    }

    public String viewAction {
        get {
            PageReference ref = new ApexPages.StandardController(oppAction).view();
            ref.getParameters().put('ID', oppAction.ID);  
            if (oppPlan.Amendment__c != null) {
                ref.getParameters().put('saveURL','/' + oppPlan.Amendment__c);
                ref.getParameters().put('retURL','/' + oppPlan.Amendment__c);
            } else {
                ref.getParameters().put('saveURL','/' + oppPlan.Opportunity__c);
                ref.getParameters().put('retURL','/' + oppPlan.Opportunity__c);
            }
            return ref.getUrl();
        }
        private set;
    }
    
    /**
    *@description returns true if the action should show by default. Canceled and Cancel Rework actions are hidden by default
  * to alter this behavior, modify the Hide__c formula field on the Opportunity Action object
  */
    public Boolean show {
        get {
            Boolean hideAction = oppAction.Hide__c;
            if (hideAction == true) {
                return false;
            } else {
                return true;
            }
        }
    }

}
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
 
@isTest
public class MyTest{
   public static testmethod void test1(){
        Opportunity_Action__c opact=new Opportunity_Action__c();
        //initialize requred fields if any are there
        try{
           insert opact;        
           OpportunityAction op=new OpportunityAction(opact,4,null);
        }catch(Exception e){
          System.debug(e);
        }
   }
}

 
sfg1sfg1
Bala i tried the above code but it is not working. Please guide me. thanks. 
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
@isTest
public class MyTest{
   public static testmethod void test1(){
        Opportunity_Action__c opact=new Opportunity_Action__c();
        //initialize requred fields of Opportunity_Action__c 
        try{
           insert opact;     
           Files__c f=new Files__c();
           //initialize required fields of Files__c
           insert f;
           OpportunityAction op=new OpportunityAction(opact,4,f);
        }catch(Exception e){
          System.debug(e);
        }
   }
}

Let me know if it helps