+ Start a Discussion
dmotedmote 

Unit Testing for Custom Controller of VF Page

I am trying to create a test for my VF Page.  Currently, I have 38% coverage.  I have methods in my controller that I though was covered by my test, but apparently isn't.  I am really just winging it.

 

Any suggestions would be appreciated.

 

Here is my controller:

 

 

public class newOpportunityController {

    public PageReference step2() {
      //pageRef.setRedirect(true);       
      //pageRef.setRedirect(false);
      return page.davidPage2;

    }
    

    
   Account account;
   Contact myContact;
   public List<Contact> contact{get;set;}
   public String selectedContact{get; set;}
   public List<SelectOption> contactList{get;set;}
   public Contact contacts{get; set;} 
   public Opportunity opportunity {get; set;}
  /*
    This will create standard getters and setter like the following
    public Opportunity getOpportunity(){
       return this.opportunity;
    }
    public void setOpportunity(Opportunity value){
        this.opportunity = value;
    }

   You need only setter method to accomplish the task like the "setOpportunity" method,
   if you decide to keep the getter you have already coded below.
  */
   OpportunityContactRole role;
   Quote q;


   
   
   public newOpportunityController()
   {      
    opportunity = new Opportunity();
    if (contactList == null)
    {
        List<Contact> contactee = [select id, name, contact.accountid from Contact where contact.accountid = :ApexPages.currentPage().getParameters().get('id')];
        contactList = new List<SelectOption>();
        for (Contact c : contactee)
        {
            contactList.add(new SelectOption( c.id, c.name ));
        }
     }   
   }
   
   
   public Account getAccount() {
        return [select id, Name, Phone from Account
        where id = :ApexPages.currentPage().getParameters().get('id')];
    }
    
      public Contact getMyContact() {
        return [select id, Name from Contact
        where id = :selectedContact];
    }
    
//not needed.
/*      
 public Opportunity getOpportunity() {
      if(opportunity == null) opportunity = new Opportunity();
      return opportunity;
   }

*/

 public OpportunityContactRole getRole() {
      if(role == null) role = new OpportunityContactRole();
      return role;
   }

 public Quote getQ() {
      if(q == null) q = new Quote();
      return q;
   }

   public PageReference test()
   {
    if (selectedContact!=null)
    {
       contacts=[select id, firstName,lastName,email from Contact where id = :selectedContact];
    }
    return null;
   }

   public PageReference save() {

      // Create the opportunity. Before inserting, create   
    
      // another relationship with the account.  
    
      opportunity.accountId = ApexPages.currentPage().getParameters().get('id');
      insert opportunity;

      // Create the junction contact role between the opportunity  
    
      // and the contact.  
    
      role.opportunityId = opportunity.id;
      role.contactId = selectedContact;
      insert role;
      
  //    q.account = ApexPages.currentPage().getParameters().get('id');
      q.name = q.name;
      q.opportunityId = opportunity.id;
      q.contactId = selectedContact;
  //    q.amount = opportunity.amount;
      insert q;
       
      PageReference opptyPage = new ApexPages.StandardController(q).view();
      opptyPage.setRedirect(true);

      return opptyPage;
   }



}

 

and my test class:

 

 

 

public class thecontrollerTests {
public static testMethod void testnewOpportunityController() {
PageReference pageRef = Page.success;
Test.setCurrentPage(pageRef);
Opportunity testOpp; // = new opportunity();

Quote testQ = new quote();
OpportunityContactRole testRole; // = new opportunityContactRole();
newOpportunityController controller = new newOpportunityController();
String nextPage = controller.save().getURL();
// Verify that page fails without parameters
System.assertEquals('/apex/failure?error=noParam', nextPage);
// Add parameters to page URL
ApexPages.currentPage().getParameters().put('id', '001S000000CmJg7');
controller.getAccount();
//controller.getOpportunity();
controller.getRole();
// Instantiate a new controller with all parameters in the page
controller = new newOpportunityController();
testOpp.name = 'testClassOpp';
testOpp.stageName = 'Prorosal/Price Quote';
testRole.role = 'Decision Maker';
testQ.name = 'testQuote';
controller.save();

// Verify that the success page displays
System.assertEquals('/apex/success', nextPage);

}}

 

 

Even just a nudge in the right direction would be appreciated!

 

Thanks,

David

Pradeep_NavatarPradeep_Navatar

Find below the sample code for param tag associated with output text. You can change your task according to this code :

 

                                <apex:page standardController="Account">

                                <apex:outputText value="{0, number, 000,000.00}">

                                <apex:param value="{!Account.AnnualRevenue}" />

                                </apex:outputText>

                                </apex:page>                  

 

Hope this helps.

dmotedmote

I changed my test slightly,

 

 

public static testMethod void testnewOpportunityController() {
System.debug('Starting Test');
PageReference pageRef = page.davidTest2;
Test.setCurrentPage(pageRef);
Opportunity testOpp = new opportunity();
Contact myContact;
String selectedContact;
Quote testQ = new quote();
OpportunityContactRole testRole = new opportunityContactRole();
ApexPages.currentPage().getParameters().put('id', '001S000000CmJg7');
newOpportunityController controller = new newOpportunityController();
pageRef = ApexPages.currentPage();
System.debug('the current url is...' +pageRef );
controller.getAccount();
selectedContact = '003S000000DqOF4';
controller.getMyContact();
controller.getRole();
controller = new newOpportunityController();
testOpp.name = 'testClassOpp';
testOpp.stageName = 'Prorosal/Price Quote';
testRole.role = 'Decision Maker';
testOpp.closeDate = System.today();
testQ.name = 'testQuote';
PageReference pageRef2 = page.davidPage2;
Test.setCurrentPage(pageRef2);
controller.save();

}}

 

 

It keeps failing on getMyContact.  On my page the user is to select a contact from a drop-down list.  How do I select the contact I want in my test?

 

Thanks,

David

aballardaballard

you need to set the contact id in the controller, the way the UI would have done...

e.g.

   controller.selectedContact = '003S000000DqOF4';

before calling controller.getMyContact();

 

 

dmotedmote

Thanks, I did that and it is now finding the contact, but now it fails on the insert of the opportunity.  I get an error message that the name, close date and stage name are all blank.  They are fields that the user would enter on the VF page, and I am setting these fiends in my test.  Does anyone know why this would not work?  Do I have to do something special with input fields in my test?

 

Thanks,

David

aballardaballard

you seem to be setting those fields on a new Opportunity object created in the test.   But that doesn't seem to have anything to do with the opportunity object created by the controller.   

You need to set the fields in the object the controller has created and is going to insert. 

dmotedmote

Okay...I fixed the fields to be the controller object fields. 

 

It works for the opportunity, but when I set controller.testQ.name = 'testQuote' it tells me that the Variable is not visible, and i cannot save the test class.