global static List<HHSurvey> getHHSurveyList() {

    RestRequest req = RestContext.request;
    RequestData  mRequestData = (RequestData)JSON.deserialize(req.requestBody.toString(),RequestData.class);
    List<HH_Survey__c> hhSList = new List<HH_Survey__c>();
    Map<String,List<HH_Housing__c>>hhHList=new Map<String,List<HH_Housing__c>>();
    List<HHSurvey> hhSurveyList = new List<HHSurvey>();
    Set<String> hhSurveyIds=new Set<String>();
    DateTime FromDate=null;
    DateTime ToDate=null;

    if(mRequestData!=null&& mRequestData.AgentMobileNo!=null&&mRequestData.AgentMobileNo!=''



      **hhSList** = [SELECT Id,Family_s_monthly_income__c,Do_you_have_your_own_house__c,Saral_Jeevan_channel_is_accessible__c,Need_Remedy_Help1__c,Irrigated_land_In_Acre__c,AgentMobileNo__c,Main_occupation_of_the_family__c,Household_Serial_Number__c,Consent_Status__c,Hamlet_Segment_Camp__c from HH_Survey__c where AgentMobileNo__c=:mRequestData.AgentMobileNo AND AgentMobileNo__c<>null And CreatedDateWithNoTime__c<> null
               AND CreatedDateWithNoTime__c=: FromDate.Date() order by CreatedDateWithNoTime__c Desc ];


     hhSList = [SELECT Id,Family_s_monthly_income__c,Do_you_have_your_own_house__c,Saral_Jeevan_channel_is_accessible__c,Dry_land_In_Acre__c,Mobile1__c,Loan_Amount__c,Respondent_Name__c,Landmark__c from HH_Survey__c where AgentMobileNo__c=:mRequestData.AgentMobileNo AND AgentMobileNo__c<>null And CreatedDateWithNoTime__c<> null
               AND CreatedDateWithNoTime__c>=: FromDate.Date() AND CreatedDateWithNoTime__c<=:ToDate.Date()   order by CreatedDateWithNoTime__c Desc ]; 


     if(hhSList !=null&&hhSList.size()>0 )

     for(HH_Survey__c hhSurvey :hhSList ){




     for(HH_Survey__c hhs : hhSList ){
         HHSurvey hs = new HHSurvey();
         hs.Id = hhs.Id;
         hs.FamilyMonthlyIncome= hhs.Family_s_monthly_income__c;
         hs.Doyouhaveyourownhouse= hhs.Do_you_have_your_own_house__c;
         hs.Remarks= hhs.Remarks__c;
         hs.TV= hhs.Do_you_have_TV_in_your_house__c ;
         hs.SurveyNo= hhs.HH_Survey_Id__c;

         List<HHHouse> setHH=null;
         if(hhHList!=null &&hhHList.size()>0 &&hhHList.containsKey(hs.Id)){



      return hhSurveyList ;


global class HHHouse{
  String Id;
  String HHSurveyId;
  Decimal ApproxSize;
  Decimal HouseType;
  Decimal HouseAge;  

global class RequestData{

  String AgentMobileNo;
  String FromDate;
  String ToDate;       


    global class HHSurvey{
    String Id;
    Decimal FamilyMonthlyIncome;
    String Doyouhaveyourownhouse;
    String SaralJeevanChannelAccess;
    String Remarks;
    String Village;
    Decimal DryLandInAcre;
    String MobileOne;
    Decimal Houshold;
    Decimal BalanceLoanAmountOutstanding;
    Decimal TotalLandHoldingAcre;
    Decimal SolutionDate;
    Decimal DOBOfFamilyHead;
    String NameofHouseholdHead;
    Decimal SurveyDate;
    String HSC;
    String ConsentStatus;
   List<HHHouse> HouseList=new List<HHHouse>();


Test class

static testMethod void SurveylistTest()              
    JSONGenerator gen = JSON.createGenerator(true);

    gen.writeStringField('AgentMobileNo', '7022834238');  
    gen.writeStringField('FromDate', '1527964200000');  
    gen.writeStringField('ToDate', '1527964200000');  

    String json =gen.getAsString();

    RestRequest req = new RestRequest(); 
    RestResponse res = new RestResponse();

    req.requestURI = 'https://cs5.salesforce.com/services/apexrest/getHHSurveyList/';  
    req.httpMethod = 'POST';
    req.requestBody= blob.valueOf(json);
    RestContext.request = req;
    RestContext.response = res;        

    List<WS_GetHHSurveyList.HHSurvey> surveylist = WS_GetHHSurveyList.getHHSurveyList();
    System.assert(surveylist.size() ==0);

  • 2
    Hello and welcome to stackexchange, please refer asking a question documentation. Instead of requesting a test class for custom code, give it a try and comeback with any specific question; with that you'd learn on how to solve the same problem again. This answer has some trailhead links which are quite good and help you with the start. – Raul Feb 11 '19 at 8:55
  • 2
    As @Raul says, it is best to have a go at the problem yourself, and ask about the specifics of where you get stuck. However, I don't think that the question he has linked to is the most useful one here, what you are doing is testing a REST endpoint in SF. So a question such as this one might help: salesforce.stackexchange.com/questions/161581/… – Aidan Feb 11 '19 at 9:11
  • 2
    You can directly call your getHHSurveyList method from your test, and pass in the JSON string by making a call to JSON.serialize and setting the result in the requestBody. Google for examples. But if you haven't already, first work through these Apex Testing Trailhead units. – Keith C Feb 11 '19 at 9:18
  • Hie i written Test class for the same but **hhSList ** give null values ... how to fetch hhSList in Test class – user63974 Feb 11 '19 at 12:26