function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MycodexMycodex 

triggering get set in test method [SOLVED]

So i've been at this for quite some time already and can not see any references to triggering a get/set. I was following the Blog tutorial on visualforce and have the pages working perfectly. Now I want to promote them to production. I have 27% coverage so far, but I can't seem to figure out how to fire off my getStage, setStage, and deselectCheckboxes functions. Can anyone provide a resource or example?

Code:
public class OppControllerExtension {

    /* The standard controller object which will be used later for navigation and to invoke
       it's save action method to create the new Opportunity. */
    ApexPages.StandardController controller;
 
    /* The Opportunity property */
    public Opportunity o {get;set;}
  
    public OppControllerExtension(ApexPages.StandardController c) {
        controller = c;
        o = (Opportunity) c.getRecord();
    }
 
    Opportunity opp;
    
    String stage;
        
    /* Setter for the stage value */
    public void setStage(String s) { this.stage = s;}
    
    /* Getter for the stage value */
    public String getStage() { return this.stage; }
    
    /* Getter which generates the options for the stage selectList */
    public List<SelectOption> getStages() {
      List<SelectOption> optionList = new List<SelectOption>();
      /* Add a null option to force the user to make a selection. */
      optionList.add(new SelectOption('','- None -'));

      /* Loop through the feature_category__c records creating a selectOption
         for each result with the record ID as the value and the name as the label 
         displayed in the selectList */
      for (Reason_Stage__c rs : [select name from Reason_Stage__c order by Name]){
        optionList.add(new SelectOption(rs.name,rs.name));
      }
      return optionList;     
    }
    
    /* Invoke the standard controller save method which returns the pageReference class 
           that will be used in the navigation, i.e. send the user to the expected location based on
           standard navigation rules provided by salesforce.com. Modified to save the downdown values. */
    public PageReference save() {
        
        o.stagename = this.stage;
        o.reason__c = this.reason;
        o.industry_group__c = this.industry;
        o.industry_subgroup__c = this.industry_subgroup;
        PageReference p = controller.save();
        
        /* Return the page reference generated by the standard controller save, which usually drops the user
           on the detail page for the newly created object. */
        return p;   
    }
    
    /* Used on the edit page to populate the dropdowns with their respective database values. Only references
        the needed dropdown fields to increase performance */
    public void initializeDropdowns() {
        opp = [select id, reason__c, stagename, industry_group__c, industry_subgroup__c 
                from Opportunity where id = :ApexPages.currentPage().getParameters().get('id') limit 1];
        this.stage = opp.stagename;
        this.industry = opp.industry_group__c; 
        this.industry_subgroup = opp.industry_subgroup__c; 
        this.reason = opp.reason__c;
    }
    
    /* Method used to reset all checkboxes to a non-checked value. New checkboxes must be added below. */
    public PageReference deselectCheckboxes()  { 
      o.Competition_True__c = false;
      return null;
    }

/* BEGIN TEST METHODS FOR ABOVE CLASS */
    /* ================================== */
    public static testMethod void OppControllerTest() {
        Opportunity o = setupTestOpportunity();
        ApexPages.StandardController con = new ApexPages.StandardController(new Opportunity());
        Test.startTest();
        OppControllerExtension ext = new OppControllerExtension(con);
        PageReference result = ext.save();
        Test.stopTest();
    }

 private static Opportunity setupTestOpportunity() {

        /* Create an account */
        Account a = new Account();
        a.name    = 'TEST';
        Database.insert(a);
        
  /* Setup a basic opportunity */
        Opportunity o  = new Opportunity();
        o.Name         = 'TEST';
        o.AccountId    = a.id;
        o.CloseDate    = Date.today();
        o.Stagename    = 'Quote lost';
                
        /* Create the opportunity */
        Database.insert(o);
        
        system.debug(a.id);
        
        /* Set the request parameter that the constructor for quoteExt is expecting */
        PageReference pref = Page.DynamicInsuredNew;
        pref.getParameters().put('oppid',o.id);
        Test.setCurrentPage(pref);

        return o;
        
 }

}


Message Edited by Mycodex on 09-03-2008 02:07 PM
devNut!devNut!
Did you try adding lines similar to:

ext.setStage('test123');
String val = ext.getStage();
?
MycodexMycodex
Thanks devNuts! 100% coverage

In case anyone is interested. I don't have any asserts, but I will add those later. This code is trimmed down a bit so it fits here. Hope this helps someone out since I could not find much information about this stuff.

Extension Code:
public class OppControllerExtension {

    /* The standard controller object which will be used later for navigation and to invoke
       it's save action method to create the new Opportunity. */
    ApexPages.StandardController controller;
 
    /* The Opportunity property */
    public Opportunity o {get;set;}
  
    public OppControllerExtension(ApexPages.StandardController c) {
        controller = c;
        o = (Opportunity) c.getRecord();
    }
    
    String stage;
        
    /* Setter for the stage value */
    public void setStage(String s) { this.stage = s;}
    
    /* Getter for the stage value */
    public String getStage() { return this.stage; }
       
    /* Getter which generates the options for the stage selectList */
    public List<SelectOption> getStages() {
      List<SelectOption> optionList = new List<SelectOption>();
      /* Add a null option to force the user to make a selection. */
      optionList.add(new SelectOption('','- None -'));

      /* Loop through the feature_category__c records creating a selectOption
         for each result with the record ID as the value and the name as the label 
         displayed in the selectList */
      for (Reason_Stage__c rs : [select name from Reason_Stage__c order by Name]){
        optionList.add(new SelectOption(rs.name,rs.name));
      }
      return optionList;     
    }

    /* Invoke the standard controller save method which returns the pageReference class 
           that will be used in the navigation, i.e. send the user to the expected location based on
           standard navigation rules provided by salesforce.com. Modified to save the downdown values. */
    public PageReference save() {
        o.stagename = this.stage;
        PageReference p = controller.save();
        return p;   
    }
    
    /* Used on the edit page to populate the dropdowns with their respective database values. Only references
        the needed dropdown fields to increase performance */
    public void initializeDropdowns() {
        Opportunity opp = [select id, stagename from Opportunity 
   where id = :ApexPages.currentPage().getParameters().get('id') limit 1];
        this.stage = opp.stagename;
    }
    
    /* Method used to reset all checkboxes to a non-checked value. New checkboxes must be added below. */
    public PageReference deselectCheckboxes()  { 
      o.Competition_True__c = false;
      return null;
    }

 


Test method Code:
    /* BEGIN TEST METHODS FOR ABOVE CLASS */
    /* ================================== */
    public static testMethod void OppControllerTest() {
        Opportunity o = setupTestOpportunity();
        //Opportunity opp;
        
        ApexPages.StandardController con = new ApexPages.StandardController(new Opportunity());
        OppControllerExtension ext = new OppControllerExtension(con);
        
        List<SelectOption> optionList1 = new List<SelectOption>();
        
        Test.startTest();        
        
        ext.setStage('Quote lost');
        String stage = ext.getStage();
        optionList1 = ext.getStages();
        
        ext.deselectCheckboxes();
        ext.initializeDropdowns();
        
        PageReference result = ext.save();
        Test.stopTest();
    }

    private static Opportunity setupTestOpportunity() {
        /* Create an account */
        Account a = new Account();
        a.name    = 'TEST';
        Database.insert(a);
        
        /* Setup a basic opportunity */
        Opportunity o  = new Opportunity();
        o.Name         = 'TEST';
        o.AccountId    = a.id;
        o.CloseDate    = Date.today();
        o.Stagename    = 'Quote lost';
        Database.insert(o);
        
        /* Create the stage object */
        Reason_Stage__c reason_stage1 = new Reason_Stage__c();
        reason_stage1.name = 'Quote lost';
        Database.insert(reason_stage1);
                
        /* Set the request parameter that the constructor DynamicInsuredNew is expecting */
        PageReference pref = Page.DynamicInsuredEdit;
        pref.getParameters().put('id',o.id);
        Test.setCurrentPage(pref);
        return o;
    }
}


Message Edited by Mycodex on 09-03-2008 02:07 PM