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
Vegaln1Vegaln1 

Help with writting Test method for controller extension .

Hello ... I have a very simple VF page that uses a standard controller extension. The Apex class returns a list of records that is populated in VF page. At present, I'm at a loss as to writting a test method for the APEX class.

 

Notice that in the apex I'm getting the record that has been selected by the user. So, in this simple case I'm returning to the VF page this selected record only. Can someone advise me how I can do a test method. Currently I'm creating a Account, opportunity, contract and assets in the test method. I don't  believe I'm defining the controller correctly since I always see 0% code covered in 'Run all tests' under Apex classes. So what do I need to do to get a valid test method written? Any help would be appreciated.

Regards,

 

Test Method:

     static testMethod void testToolsAEApex()  
    {
        Test.setCurrentPageReference(new PageReference('Page.ToolsAE'));
          ApexPages.standardController controller = new ApexPages.standardController(new Asset_Entitlement__c());
        Account a = new Account(Name='Becky Test');
        insert a;
        
        Opportunity  opp = new Opportunity();
        opp.Name='Test';
        opp.CurrencyIsoCode = 'USD';    
        opp.AccountId = a.Id;
        opp.Type = 'Term';
        opp.Type_2__c = 'Tools';
        opp.Sector__c = 'Academic';
        opp.CloseDate = system.today();
        opp.StageName = 'Proposal';
        opp.ForecastCategoryName = 'Pipeline';
        opp.OwnerId = '00550000000n1k8';
        
        insert opp;
        
        Contracts__c  con = new Contracts__c();
        con.Contract_Name__c='Test';
        con.CurrencyIsoCode = 'USD';
        con.Opportunity__c = opp.Id;    
 
        insert con;
        
        Asset_Entitlement__c asset = new Asset_Entitlement__c();  
        asset.Account_del__c = opp.AccountId;              
        asset.Opportunity_Owner__c = opp.OwnerId;      
        asset.CurrencyIsoCode = opp.CurrencyIsoCode;       
        //asset.Product__c = pricebookMap.get(oppLines.PricebookEntryId).Product2.Id;                               
        asset.End_Date__c = system.today() - 10;        
        asset.Invoice_Date__c = system.today();        
        asset.Unit_Price__c = 12;        
        asset.Opportunity__c = opp.Id;
        asset.Start_Date__c = system.today();                   
        asset.Status__c = 'Expired';
        asset.Entitlement_Status__c = 'Current';
        asset.Term__c = 12;        
        asset.RecordTypeId = '012500000001C2f';
        asset.Contracts__c = con.Id;                 
        asset.Quantity__c = 1;
        asset.DNR__c = false;
        asset.Renew_Flag__c = false;  
        asset.Part_Number__c = '2091Test-MNT';  

        asset.License_Type__c = 'Term';  
        insert asset;
        ApexPages.currentPage().getParameters().get(con.Id);

 

 

Here's the APEX Class:

 

public class ToolsAE {
  private final Asset_Entitlement__c AE;
  String i = '' ;
  String conId ='';
  String noRec = 'N';
  public ToolsAE(ApexPages.StandardSetController controller)
  {   
       List<Asset_Entitlement__c> aeId = (List<Asset_Entitlement__c>) controller.getSelected();
    Parent_Opty_Aggregate__c, Group_Number__c,  from Asset_Entitlement__c where id IN :aeId]; //IN :aeId
    for(Asset_Entitlement__c j:aeId)
    {
     i = j.Id;
    }
     if(i == null || i ==''){
          noRec = 'Y';
          conId = ApexPages.currentPage().getParameters().get('id'); // If no records get the current Contract to return to.
     }
   }
 
  public List <Asset_Entitlement__c> aeList
  {
     get {
         List<Asset_Entitlement__c> aeList = new List<Asset_Entitlement__c>();
         for (Asset_Entitlement__c a:[select id, Contracts__c, Name, Parent_Opty_Aggregate__c, Group_Number__c,
            Account_del__c, Entitlement_Status__c,Distrib_Method__c, Start_Date__c, End_Date__c,Term__c,Unit_Price__c,Total_Price__c
               from Asset_Entitlement__c where id  = :i])
        {
              aeList.add(a);
              conId = a.Contracts__c; //Grab the Contract Id so we can use it later to return to
        }
        if (noRec == 'Y')
       {
          ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'No Header Aggregate Records  were selected.');
           ApexPages.addMessage(myMsg);
           return null;
       }
         else
        return aeList;
     }
     set;
   
 } 
 
   public PageReference ret() {
          PageReference conPage = new PageReference('/' + conId);//Return to Contract Page
          conPage.setRedirect(true);
          return conPage;
     }
}

Best Answer chosen by Admin (Salesforce Developers) 
Vegaln1Vegaln1
The solution to this is:


List<Asset_Entitlement__c> aeList = [select id, Contracts__c, Name, Parent_Opty_Aggregate__c, Group_Number__c, Account_del__c, Entitlement_Status__c, Start_Date__c, End_Date__c,Term__c,Unit_Price__c from Asset_Entitlement__c where Id = :asset.Id]; ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(aeList);

 Since the class being tested was using StandardSetController:

public ToolsAE(ApexPages.StandardSetController controller)

 Thanks for everyone's input.

 

Regards,


 

All Answers

gv007gv007

Can re orgasnizer yours code and post.

 

Where are you calling yours testmethod .It is inside the extension controller class or you are calling it in a separate calls.I saw in yours code your are not calling any method .(i.e your are not calling any methods using an object variable.

 

see the documentation it will help you or reoranize the code and post it.

Vegaln1Vegaln1

Hello Thank you for the suggestion: Here's the latest test script. What I'm getting now when I save the test method is 'Constructor is not defined:[ToolsAE].<Constructor>(ApexPages.StandardController)'. Highlighted in red below.

 

In the test method I'm creating a Account,opportunity, contact and asset. Then trying to instaniate the extension controller. If I can get past this error what would I use to actually test the extension?

 

Thanks.

 

 static testMethod void testToolsAEApex() 
    {
       
      //    ApexPages.standardController controller = new ApexPages.standardController(new Asset_Entitlement__c());
        Account a = new Account(Name='Becky Test');
        insert a;
       
        Opportunity  opp = new Opportunity();
        opp.Name='Test';
        opp.CurrencyIsoCode = 'USD';   
        opp.AccountId = a.Id;
        opp.Type = 'Term';
        opp.Type_2__c = 'Tools';
        opp.Sector__c = 'Academic';
        opp.CloseDate = system.today();
        opp.StageName = 'Proposal';
        opp.ForecastCategoryName = 'Pipeline';
        opp.OwnerId = '00550000000n1k8';
       
        insert opp;
       
        Contracts__c  con = new Contracts__c();
        con.Contract_Name__c='Test';
        con.CurrencyIsoCode = 'USD';
        con.Opportunity__c = opp.Id;   
 
        insert con;
       
        Asset_Entitlement__c asset = new Asset_Entitlement__c(); 
        asset.Account_del__c = opp.AccountId;             
        asset.Opportunity_Owner__c = opp.OwnerId;     
        asset.CurrencyIsoCode = opp.CurrencyIsoCode;      
        //asset.Product__c = pricebookMap.get(oppLines.PricebookEntryId).Product2.Id;                              
        asset.End_Date__c = system.today() - 10;       
        asset.Invoice_Date__c = system.today();       
        asset.Unit_Price__c = 12;       
        asset.Opportunity__c = opp.Id;
        asset.Start_Date__c = system.today();                  
        asset.Status__c = 'Expired';
        asset.Entitlement_Status__c = 'Current';
        asset.Term__c = 12;       
        asset.RecordTypeId = '012500000001C2f';
        asset.Contracts__c = con.Id;                
        asset.Quantity__c = 1;
        asset.DNR__c = false;
        asset.Renew_Flag__c = false; 
        asset.Part_Number__c = '2091Test-MNT';   //THS 3/5/2009
        asset.License_Type__c = 'Term'; 
        insert asset;
        PageReference pageRef = Page.ToolsAE;
        //Test.setCurrentPageReference(new PageReference('Page.ToolsAE'));
        Test.setCurrentPageReference(pageRef);
        //Create an instance of the controller extension
        //LeadExtension leadExt = new LeadExtension(new ApexPages.StandardController(newLead));
       
        //ToolsAE ae = new ToolsAE(new ApexPages.StandardController(asset));
        //controller.autoRun(); // This also fails with same error
      
        ApexPages.StandardController controller = new ApexPages.StandardController(asset);
        ToolsAE TestExtension = new ToolsAE(controller);

       
        //ApexPages.currentPage().getParameters().put('id', leadId);
                ApexPages.currentPage().getParameters().get(con.Id);
   
   }

dmchengdmcheng

Hello.  Why do you have these two lines commented out?  I used similar coding in my extension testing and it worked fine.  Also - I think second line is supposed to have "ae" instead of "controller" that you originally had.

 

ToolsAE ae = new ToolsAE(new ApexPages.StandardController(asset)); ae.autoRun();

 

 

 

Vegaln1Vegaln1

Hello dmcheng... Those two lines were commented out because they also gave the same error I am getting with the current invocation. I was trying different methods to try to correct the issue.

 

And yes, you are correct it should use 'ae.autoRun()'.

\

Regards,

dmchengdmcheng

Hmm.  Well, the VF page I built overrides the Lead conversion process, so I wanted it to invoke my Apex code automatically.

 

Here's a portion of my Lead extension code:

 

public class LeadExtension { private final ApexPages.StandardController jsLeadController; public LeadExtension(ApexPages.StandardController stdController) { jsLeadController = stdController; } // Code we will invoke on page load. public PageReference autoRun() { //// ... //// [all of my production code omitted] //// ...

 

And my VF page is very simple:

 

<apex:page standardController="Lead" extensions="LeadExtension" action="{!autoRun}"> <apex:outputText >Please wait ... lead conversion in progress ...</apex:outputText> </apex:page>

 

 I don't know if this applies to your situation.

 

BTW - your code would be easier to read if you used the Insert Code button when composing your messages.

 

 

 

Vegaln1Vegaln1
Thank you. I forgot about using the 'insert code'. Thanks for the reminder...
Vegaln1Vegaln1
The solution to this is:


List<Asset_Entitlement__c> aeList = [select id, Contracts__c, Name, Parent_Opty_Aggregate__c, Group_Number__c, Account_del__c, Entitlement_Status__c, Start_Date__c, End_Date__c,Term__c,Unit_Price__c from Asset_Entitlement__c where Id = :asset.Id]; ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(aeList);

 Since the class being tested was using StandardSetController:

public ToolsAE(ApexPages.StandardSetController controller)

 Thanks for everyone's input.

 

Regards,


 

This was selected as the best answer