+ Start a Discussion
Eager-2-LearnEager-2-Learn 

How to create a simple test script for extensions

Hello,

 

I found a overly detailed link at SFDC on a test script for controllers and controller extensions; however, I cannot make sense of it all.

 

I have the following code and I created a very basic VF page that I dropped on the Opportunity page.  It displays to the user that they have to enter a Partner record if a partner record is not found for the current opportunity.

 

How can I create a test script to test this and get me passed the limits so I can move it to production?

Do I execute the test script in production before it can be used? I am not sure how this stuff works.

public class OppPartnerExtension {
    private boolean hasPartners = false;    

    public OppPartnerExtension(ApexPages.StandardController controller) {
       if([select count() from OpportunityPartner where opportunityId = :controller.getRecord().id] > 0){
           hasPartners = true;
       }
    }
    
    public boolean getHasPartners(){
        return hasPartners;
    }
}

 

 

 The VF page looks like this:

 

<apex:page standardController="Opportunity" extensions="OppPartnerExtension">

  <apex:outputText style="Color:#FF3300; background-color:yellow; font-style:bold"                    
                   value="Remember to enter partner(s)"
                   rendered="{!NOT(hasPartners)}">
                   <!--
                   value="{!hasPartners}">
                   -->
                   
      <!-- This routine jumps us to the partner screen if a partner
           record does not exists      
           
      <script>
          parent.location.href = '/opp/partneredit.jsp?id={!Opportunity.Id}&fid={!Opportunity.AccountId}&retURL=%2F{!Opportunity.Id}';
      </script>

      -->  
  </apex:outputText>

</apex:page>

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
iBr0theriBr0ther

 

String str = '/apex/QuotePreference' ;
       PageReference pageRef = new PageReference(str);
       Test.setCurrentPage(pageRef);
Quote_Preference__c qp=new Quote_Preference__c(Name='Test99',Terms_and_Conditions__c='Test');  
insert qp;
       QuotePreference qPreference=new QuotePreference(new ApexPages.StandardController(qp));

 

String url = '/apex/yourPageName' ;      

 PageReference pageRef = new PageReference(url);

       

Test.setCurrentPage(pageRef);

Opportunity opp=new Opportunity(PriceBook2Id=p2Id,Name='Test Opportunity',CloseDate=system.today(),StageName='Closed'); 

 insert op;      

 OppPartnerExtension opExt=new OppPartnerExtension(new ApexPages.StandardController(op));

opExt.getHasPartners();

 

 

Is it help?

 

Cheers,

All Answers

iBr0theriBr0ther

 

String str = '/apex/QuotePreference' ;
       PageReference pageRef = new PageReference(str);
       Test.setCurrentPage(pageRef);
Quote_Preference__c qp=new Quote_Preference__c(Name='Test99',Terms_and_Conditions__c='Test');  
insert qp;
       QuotePreference qPreference=new QuotePreference(new ApexPages.StandardController(qp));

 

String url = '/apex/yourPageName' ;      

 PageReference pageRef = new PageReference(url);

       

Test.setCurrentPage(pageRef);

Opportunity opp=new Opportunity(PriceBook2Id=p2Id,Name='Test Opportunity',CloseDate=system.today(),StageName='Closed'); 

 insert op;      

 OppPartnerExtension opExt=new OppPartnerExtension(new ApexPages.StandardController(op));

opExt.getHasPartners();

 

 

Is it help?

 

Cheers,

This was selected as the best answer
Eager-2-LearnEager-2-Learn

This is what I have done, trying to make sense of it all.  Please keep in mind that I am new to Apex and my background is Visual Bsic, MS Access, PL/SQL, and Dbase (now I am telling my age!), for example.

 

The results from the TEST code below stated 100% code coverage.  Does this seem like it would be suffice to move into a Sandbox now and then into production?

 

Can you point me to a link that clearly tells me what to do and what to expect when moving code into production.  In other words, Do I have to actually move the test code to production and run it so that the system detects that I have reached at least 75% code coverage before it can be checked in as deployed.  I have no clue what to expect!!!


Thanks in advanced for all your support.

 

@isTestprivate 
class OppPartnerExtension_TEST {    
   static testMethod void Testing() {        
      String url = 'https://na7.salesforce.com/006A0000001e14A';
      PageReference pageRef = new PageReference(url);
      Test.setCurrentPage(pageRef);        
      //Opportunity opp=new Opportunity(PriceBook2Id=p2 
                                        Id,Name='TestOpportunity',
                                        CloseDate=system.today(),
                                        StageName='Closed');         
      //insert op;              
      //OppPartnerExtension opExt = new OppPartnerExtension(new
                            ApexPages.StandardController(op));        
      //opExt.getHasPartners();       
   }
}