You need to sign in to do that
Don't have an account?
Salesforce Dev199
TEST class for Rest API
Can anyone help me how to write a test class for REST API? below is my code.. Please help guys.
@RestResource(urlMapping='/Course__c/*') global class WebServiceExpose { public String Name; public String sectionKey; public String profkey; public String schoolKey; public String Group1; public String coursecreated; public String startDate; public String endDate; public Double Prod_Capsim360; public Double Prod_GlobalDNA; public static WebServiceExpose parse(String Json) { return (WebServiceExpose) System.JSON.deserialize(json, WebServiceExpose.class); } @HttpPost global static ID doPost() { RestRequest req = RestContext.request; Blob body = req.requestBody; String requestString = body.toString(); WebServiceExpose we= (WebServiceExpose)JSON.deserialize(requestString, WebServiceExpose.class); System.debug('-->>'+we); String Name1=we.Name; String sectionKey1=we.sectionKey; String profkey1=we.profkey; String Group1=we.Group1; Date coursecreated=Date.valueOf(we.coursecreated); Datetime startDate=Date.valueOf(we.startDate); Datetime endDate=Date.valueOf(we.endDate); String schoolkey1=we.schoolKey; Double Prod_Capsim360c=we.Prod_Capsim360; Double Prod_GlobalDNA1=we.Prod_GlobalDNA; List<Contact> con=new List<Contact>(); con=[select id, Professor_Key__c,Contact.AccountId from Contact where Professor_Key__c=: profkey1 LIMIT 1]; System.debug('-'+con); List<AccountContactRelation> accon=new List<AccountContactRelation>(); accon=[select contactId, accountId,account.School_Key__c from AccountContactRelation where account.School_Key__c=: schoolkey1]; Course__c course=new Course__c(Name=Name1, Contact__c=con[0].Id, Account__c=accon[0].AccountId, Group__c= Group1, Course_Created_Date__c=coursecreated,End_Date__c= endDate, Start_Date__c=startDate, Prod_Capsim360__c= Prod_Capsim360c,Prod_GlobalDNA__c=Prod_GlobalDNA1,Section_Key__c=sectionKey1); insert course; System.debug('Course details '+course); return course.Id; } //public String sectionKey; public String studentcount; @HttpPatch //Method to update Course fields global static ID updateRecord() { RestRequest req = RestContext.request; Blob body = req.requestBody; String requestString = body.toString(); WebServiceExpose we= (WebServiceExpose)JSON.deserialize(requestString, WebServiceExpose.class); String sectionKey1=we.sectionKey; String studentcount1=we.studentcount; List<Course__c> course1= new List<Course__c>(); course1=[select id, Section_Key__c, Number_of_Students__c From Course__c where Section_Key__c= :sectionKey1 ]; course1[0].Number_of_Students__c=Integer.valueOf(studentcount1); update course1; return course1[0].id; } }
Apex Class
Test Class
If you find this information helpful, please mark this answer as Best. It may help others in the community
Thanks,
Anudeep