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
jeba mjeba m 

Test class for the following code....

Hi please help me to write the test class for the following visualforce page.......Here the field candidate__c is a lookup to a object candidate__c and the field position__c is lookup to the object position__c......

PLEASE PROVIDE ME THE TEST CLASS


public class profileshortlists {

Id id = ApexPages.currentPage().getParameters().get('id');
public List<Profiles_Shortlisting__c> profilelists{get; set;}



public profileshortlists(ApexPages.StandardController controller) {
        profilelists = new List<Profiles_Shortlisting__c>([select name,Candidate__c,Position__c,Candidate_Status__c,resume__c from Profiles_Shortlisting__c where id =: id]);
      
        profilelists.add(new Profiles_Shortlisting__c(name='temp'));
   
   
        }
     public PageReference save() {
  insert profilelists;
  System.PageReference pageReference = new System.PageReference('/a02/o');

   return  PageReference;
   //return new PageReference('/'+id);
  }

}



<apex:page standardController="Profiles_Shortlisting__c" extensions="profileshortlists">
<apex:form >
    <apex:pageBlock title="profile shortlist details" id="thePageBlock" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}"  value="Save"></apex:commandButton>
            <apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Profile shortlist details" >
       
        <apex:pageblockTable value="{!Profilelists}" var="ps" id="table">
 
        <apex:column headerValue="Candidate">
             <apex:inputfield value="{!ps.Candidate__c}">
            </apex:inputfield>
            </apex:column>
            <apex:column headerValue="Candidate Status">
        <apex:inputfield value="{!ps.Candidate_Status__c}">
            </apex:inputfield>
            </apex:column>
            <apex:column headerValue="Position">
         <apex:inputfield value="{!ps.Position__c}">
            </apex:inputfield> 
         </apex:column>
  
                        </apex:pageblockTable>
            </apex:pageBlockSection>

            </apex:pageblock>
</apex:form>
</apex:page>
bob_buzzardbob_buzzard
You've asked for help to write this class, but I can't see any indication that you've had a go at writing this yourself.  If you can write the Apex controller, I don't understand why you can't at least have a stab at writing the test class and post here when you hit problems.

All you need to do is insert an instance of a Candidate__c, Position__c and Profiles_Shortlisting__c record, set the id into the parameters of the testing page, instantiate the controller, update some fields and then execute the save method.

But I'm genuinely interested in how you can end up in the situation where you are comfortable writing Visualforce and an extension controller, but not how to begin to write a a test class - is it that the online resources are lacking?
Mani RenusMani Renus
@isTest
public class Testprofileshortlists{
   
   static testMethod void myUnitTest() {
          Profiles_Shortlisting__c ps=new Profiles_Shortlisting__c();
          ps.name='test';
          //like insert manadaroty fields of 'Profiles_Shortlisting__c' object
          insert ps;
          ApexPages.StandardController sc = new ApexPages.StandardController(ps);       
          profileshortlists pfs=new profileshortlists(sc);
          PageReference pageRef = Page.PageName;
          pageRef.getParameters().put('id', String.valueOf(ps.Id));
          Test.setCurrentPage(pageRef);
          ps.save();
    }
}

      
         
jeba mjeba m
but it shows the following error...(Method does not exist or incorrect signature: [SOBJECT:Profiles_Shortlisting__c].save() at line 16 column 11)
jeba mjeba m
Ok here is my test class but it shows me error in save method......


@isTest
global class Testprofileshortlists implements system.InstallHandler {
global void onInstall(InstallContext context) {}
  static testMethod void testsave(){
 
  Job_Request__c pos = new Job_Request__c();
        pos.Name  = 'Job_Request';
       // pos.Hiring_Manager__c  = 'test';      
        insert pos;
      
        Candidate__c can = new Candidate__c();
        can.Name = 'test';
        can.Country__c = 'test';  
       insert can;
 
  Profiles_Shortlisting__c ps = new Profiles_Shortlisting__c();
  ps.name = 'temp';
  ps.candidate__c=can.id;
  ps.Position__c=pos.id;
  insert ps;
 
  PageReference ref = new PageReference('/apex/Profilesupdatepage?psId=' + ps.Id);
    Test.setCurrentPage(ref);
   
    Test.startTest();
    Apexpages.StandardController stdController = new Apexpages.StandardController(ps);
    profileshortlists myController = new profileshortlists(stdController);
    string nextpage=mycontroller.save().getUrl();
    PageReference ref2 = page.profileupdate;
    nextPage = mycontroller.save().getUrl();
   // ps.save();
    Test.stopTest();

    }
}
Mani RenusMani Renus
@isTest
public class Testprofileshortlists{
  
   static testMethod void myUnitTest() {
          Profiles_Shortlisting__c ps=new Profiles_Shortlisting__c();
          ps.name='test';
          //like insert manadaroty fields of 'Profiles_Shortlisting__c' object
          insert ps;
          ApexPages.StandardController sc = new ApexPages.StandardController(ps);      
          profileshortlists pfs=new profileshortlists(sc);
          PageReference pageRef = Page.PageName;
          pageRef.getParameters().put('id', String.valueOf(ps.Id));
          Test.setCurrentPage(pageRef);         
    }
   static testMethod void myUnitTest2() {
           profileshortlists ps1=new profileshortlists();
           ps1.save();
   }
}



Cheers
Mani