+ Start a Discussion
aressaress 

Test Class for Rest Service on Lead create update delete

Need Test class for following Rest Service class to insert update delete Lead record.
Can anyone help? please provide positive negative bulk test scenarios.
@RestResource(urlMapping='/Lead')
global class LeadManipulationService {
    
    global class Result {
        public Boolean isSuccess;
        public String lead;
        public String status;
    }
    
  @HttpPost
    global static Result createLead(String firstName, String lastName, String email, String phone, String company) {
        Result res= new Result();
        res.isSuccess = false;
        res.status = 'Error';
        
        Lead l = new Lead();
        l.FirstName = firstName;
        l.LastName = lastName;
        l.Email = email;
        l.Phone = phone;
        l.Company = company;
        
        
        try {
            insert l;
        }catch(DMLException e) {
            return res;
        }
        
     res.isSuccess = true;
        res.status = 'Success';
        res.lead = l.Id;
        return res;   
        
    }
    
    
    @HttpPut
    global static Result updateLead(String firstName, String lastName, String email, String phone) {
        Result res= new Result();
        res.isSuccess = false;
        res.status = 'Error';
        
        List<Lead> l = [
            SELECT
              FirstName,
              LastName,
              Email,
              Phone
            FROM
              Lead
            WHERE
        FirstName LIKE :firstName
            AND
              LastName LIKE :lastName
            LIMIT 1
        ];
        l[0].FirstName = firstName;
        l[0].LastName = lastName;
        l[0].Email = email;
        l[0].Phone = phone;
        
        
        try {
            update l;
        }catch(DMLException e) {
            return res;
        }
        
     res.isSuccess = true;
        res.status = 'Success';
        res.lead = l[0].Id;
        return res;   
        
    }
    
    
    
    @HttpDelete
   global static Result deleteLead() {
        Result res= new Result();
        res.isSuccess = false;
        res.status = 'Error';
        
        String firstName = RestContext.request.params.get('firstName');
        String lastName = RestContext.request.params.get('lastName');
        String email = RestContext.request.params.get('email');
        String phone = RestContext.request.params.get('phone');
        

        List<Lead> leadList = [
            SELECT
                id
            FROM
                Lead
            WHERE
                FirstName = :firstName 
            AND
                LastName = :lastName
            AND 
                Email = :email
            AND
                Phone = :phone
        ];
        
        try {
            delete leadList;
        }catch(DMLException e) {
            return res;
        }
        
     res.isSuccess = true;
        res.status = 'Success';
        res.lead = leadList[0].Id;
        return res;   
        
    }
 
    
}
Best Answer chosen by aress
v varaprasadv varaprasad
Hi Ayisha,

Please check once sample code below. 
 
@isTest
public class restTest{

public static testmethod void testService(){
 RestRequest req = new RestRequest();
 RestResponse res = new RestResponse();

req.requestURI = '/services/apexrest/Lead';  //Request URL
req.httpMethod = 'POST';//HTTP Request Type
RestContext.request = req;
RestContext.response= res;

Result res = new Result();
res.isSuccess = True;
res.lead = 'Lead';
res.status = 'Success';

 
LeadManipulationService lm = new LeadManipulationService()
Result res1 = lm.createLead('Vara', 'prasad', 'vara@abc.com', '9090909090','ABC'); 
Result res2 = lm.updateLead('Vara', 'prasad', 'vara@abc.com', '9090909090'); 
Result res3 = lm.deleteLead();

system.assert(res3.get('isSuccess')==true);//Similarly assert rest as well

}
}
If the above code will not work then create 3testmethods(post,put,delete,) and try. 

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support  : varaprasad4sfdc@gmail.com
Blog                  : http://salesforceprasad.blogspot.com/
Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 

All Answers

v varaprasadv varaprasad
Hi Ayisha,

Please check once sample code below. 
 
@isTest
public class restTest{

public static testmethod void testService(){
 RestRequest req = new RestRequest();
 RestResponse res = new RestResponse();

req.requestURI = '/services/apexrest/Lead';  //Request URL
req.httpMethod = 'POST';//HTTP Request Type
RestContext.request = req;
RestContext.response= res;

Result res = new Result();
res.isSuccess = True;
res.lead = 'Lead';
res.status = 'Success';

 
LeadManipulationService lm = new LeadManipulationService()
Result res1 = lm.createLead('Vara', 'prasad', 'vara@abc.com', '9090909090','ABC'); 
Result res2 = lm.updateLead('Vara', 'prasad', 'vara@abc.com', '9090909090'); 
Result res3 = lm.deleteLead();

system.assert(res3.get('isSuccess')==true);//Similarly assert rest as well

}
}
If the above code will not work then create 3testmethods(post,put,delete,) and try. 

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support  : varaprasad4sfdc@gmail.com
Blog                  : http://salesforceprasad.blogspot.com/
Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1

 
This was selected as the best answer
v varaprasadv varaprasad


Result res = new Result(); res.isSuccess = True; res.lead = 'Lead'; res.status = 'Success';

remove above code and add below code.


LeadManipulationService.Result res = new LeadManipulationService.Result();
res.isSuccess = True;
res.lead = 'Lead';
res.status = 'Success';
aressaress
can anyone provide Mock test class that implement MockCallout for same code.