+ Start a Discussion
ranga babu vangalapudi 2ranga babu vangalapudi 2 

Need Test class for HTTP Get method in Rest API

Hi, 
Below is my Rest api
@RestResource (urlMapping='/Outletmasters/*')
global with sharing class OutletRestClass {  
    @HttpGet
    global static list<Account> getAccounts(){
        String query = 'Select Id,Name, Active__c,Address__c,Beat_Code__r.Name, Beat_Name__c,Channel_type__c,Cluster__c,Cluster_Code__r.Cluster_Code__c,Contract_type__c, Country_liquor_sold__c,District__c,Geo_Location__latitude__s,Geo_Location__longitude__s,USL_Outlet_ID__c FROM Account  where (LastModifiedDate = TODAY OR CreatedDate = TODAY) AND Ipas_Flag__c != true LIMIT 1700';
        RestRequest req = RestContext.request; 
        RestResponse res = RestContext.response; 
        list<Account> acc_tosend = new list<Account>();
           list<Account> accList = Database.query(query);
        for(Account ac: accList){
            if(ac.Geo_Location__Latitude__s == NULL || ac.Geo_Location__Longitude__s == NULL || ac.Group_Name__c == NULL || ac.Lower_Prestige__c == NULL){
                ac.Geo_Location__Latitude__s = null;
                ac.Geo_Location__Longitude__s = null;
                ac.Group_Name__c ='null';
                ac.Lower_Prestige__c ='null';
                   acc_tosend.add(ac);
            }
        }
        return acc_tosend;
    }
}

Below is the Test class, it is covering only 30% of the code:

@isTest
public class OutletMastersRestClass {
    public static testMethod void getOutletDetails() {
        Test.startTest();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/Outletmasters'; 
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
        list<Account> acc = OutletRestClass.getAccounts();
   Test.stopTest();   
    }
}

your help is greatly appreciated.

Thanks,
Ranga.V