1

Can anyone help me with the test class for below controller. I am trying to get the values of the vf page on save of a new record. standard controller will be null at that point but i need to access the values so I can save it.

public CA_SamsAccountController(ApexPages.StandardController std) {
  samsid = ApexPages.currentPage().getParameters().get('id');
   accid = ApexPages.currentPage().getParameters().get('AccountId');
   if(samsid!=null)
    cont =  (CA_SamsAccount__c)std.getRecord();  --- this line gives me error whenever I try to run the test class if i remove the above if condition
    }

    public PageReference save() { 
        string acctype = bc.getSamsAccountTypes();
        cont.CA_Sams_Account_Name__c = str;
        cont.CA_Account__c = acc.id;
        cont.CA_Account_name__c = accname;
        cont.CA_ApiLastName__c = '.api';
        cont.CA_Country__c = selectedCountry;
        cont.CA_contract__c = contractid;
        cont.CA_Parent_ID__c = selectedParent;
        cont.CA_MisPassword__c = temp1;
        cont.CA_ApiPassword__c = temp;
        cont.CA_AccountTypeId__c = acctype;
        con = [select lastname,firstname,phone,email from contact where id=: cont.CA_Contact__c limit 1];   --- I always have cont as null and hence not able to fetch contact
        cont.CA_First_Name__c = con.firstname;
        cont.CA_Last_Name__c = con.lastname;
        cont.CA_Telephone__c = con.phone;
        cont.CA_Email__c = con.email;
        insert cont;
    }

     public string getApiorgname()
{
   string apiorgname = '';
   string api = cont.CA_Sams_Account_Name__c;  --- same issues goes here as well
   list<CA_SamsAccount__c> clist = [select CA_ApiFirstName__c from CA_SamsAccount__c where CA_ApiFirstName__c=: api];
   if(clist.size()>0)
       for(CA_SamsAccount__c c : clist)
            uniquename.add(c.CA_ApiFirstName__c);
   apiorgname = checkpattern(api);
   apiorgname = apiorgname + '.api';
   system.debug('api==='+ apiorgname);
   return apiorgname;
}

Test class

static testmethod void mytest(){
        Account a = new account(name = 'test');
        insert a;
        contact c = new contact (lastname='abc', firstname = 'ABC', phone ='987656');
        insert c;
         System.currentPageReference().getParameters().put('AccountId', a.id);
         //get your page reference here. PageReference pageRef = Page.YourPageName;
        //pageRef.getParameters().put('Id', null );

       //pageRef.getParameters().put('AccountId', a.id);
        CA_SamsAccountController cs = new CA_SamsAccountController(null);
        cs.save();
        cs.ContactPopulated();
       list<SelectOption> testoptions = cs.getParent();
        list<SelectOption> testoptions1 = cs.getCountry();
        cs.selectedCountry = 'none';
        cs.selectedParent = 'none';
    }
     static testmethod void mytest1(){
        Account a = new account(name = 'test');
        insert a;
         string samsacc = a.name.replace(' ','_').tolowercase();
        contact c = new contact (lastname='abc', firstname = 'ABC', phone ='987656');
        insert c;
        CA_SamsAccount__c sacc= new CA_SamsAccount__c(CA_Account__c = a.id, CA_Contact__c = c.id, CA_Country__c = 'test', CA_Parent_ID__c = 'test', CA_Sams_Account_Name__c = samsacc );
        insert sacc;
        System.currentPageReference().getParameters().put('id', sacc.id);
         System.currentPageReference().getParameters().put('AccountId', a.id);

        apexPages.standardController newCntr=new apexPages.standardController(sacc);
        CA_SamsAccountController cs = new CA_SamsAccountController(newCntr);
        cs.save();
        cs.ContactPopulated();
       list<SelectOption> testoptions = cs.getParent();
        list<SelectOption> testoptions1 = cs.getCountry();
        cs.selectedCountry = 'none';
        cs.selectedParent = 'none';
         cs.con = c;
         cs.getApiorgname();
         cs.getAPIpassword();
1

1.)You need to get the page reference for the page first as below:

PageReference pageRef = Page.YourPageName;

2.) Set the current page in test class first as below :

Test.setCurrentPage(pageRef);

3.)Then set the parameters as below :

pageRef.getParameters().put('Id', samsid );

pageRef.getParameters().put('AccountId', accid );

4.)Then instantiate standard controller and its extension:

ApexPages.StandardController sc = new ApexPages.StandardController (obj);    
CA_SamsAccountController caSams = new CA_SamsAccountController (sc); 
7
  • @Priya It would be better if you could post your test class.As community will get a better idea of your problem.
    – Rohit C
    Apr 4 '17 at 12:04
  • 1
    Yes Rohit, sure. Apr 5 '17 at 5:32
  • @Priya, Set page reference first and then set its parameters. In your test class , you haven't set the page reference and hence it is unable to find the parameters . Just follow the mentioned steps in my answer and see if it works.
    – Rohit C
    Apr 5 '17 at 5:58
  • @Priyakrishnamurthy , edited your answer
    – Rohit C
    Apr 5 '17 at 6:13
  • I tried the same code.. no luck still it has null value. not sure where am i missing it. Apr 5 '17 at 6:17
0

Try to use below piece of code where is shows how to test the Standard Controller extension and how to setCurrentPage reference and add url parameter.

Contact cObj = new Contact(LastName='TestContact#123')
insert cObj;
ApexPages.StandardController sc = new ApexPages.StandardController(cObj);
NewContactCntrl testAccPlan = new NewContactCntrl(sc);

PageReference pageRef = Page.ContactPage;    //set the Page 
pageRef.getParameters().put('id', String.valueOf(cObj.Id)); //Add URL parameter
Test.setCurrentPage(pageRef);
1
  • Hi, I have tried this but still it has null value. I have posted my test class as well Apr 5 '17 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.