You need to sign in to do that
Don't have an account?
Rajesh Singh 88
Test class code coverage for HttpPost method
Hi
I am new to writting apex for integration. I have written the logic and it's working fine. Can someone please help me to write test class the below class as I am unable to do so. I have tried multiple times but the class is not getting covered.
@RestResource(urlMapping='/AccountBiilingDate/*')
global class AccountBiilingDateUpdateClass{
@HttpPost
global static List<ResponseWrapper> updateAccBillDate(){
RestRequest accBillDateRequest = RestContext.request;
String jsonResponse = '';
RestResponse res = RestContext.response;
String requestBody = accBillDateRequest.requestBody.toString();
List<AccountBiilingDate> accRecsReceived = (List<AccountBiilingDate>) JSON.deserialize(requestBody, List<AccountBiilingDate>.class);
List<ResponseWrapper> wrperlist = new List<ResponseWrapper>();
List<Account> accListToUpdt = new List<Account>();
Set<Account> accSetToUpdate = new Set<Account>();
for(AccountBiilingDate accBillDate : accRecsReceived){
Account acc = new Account();
acc.Id = Id.valueOf(accBillDate.ACC_ID);
If(accBillDate.BILLDATE != ''){
acc.Billing_Date__c = Date.valueOf(accBillDate.BILLDATE);
}else{
acc.Billing_Date__c = NULL;
}
accSetToUpdate.add(acc);
}
accListToUpdt .addALL(accSetToUpdate);
Database.SaveResult[] srList = database.update(accListToUpdt,false);
for (Database.SaveResult sr : srList){
if (sr.isSuccess()){
jsonResponse = 'Successfully updated';
res.responseBody = blob.valueOf(jsonResponse);
wrperlist.add(new ResponseWrapper(sr.getId(),jsonResponse,200));
}else {
for(Database.Error err : sr.getErrors()) {
res.responseBody = Blob.valueOf(err.getMessage());
wrperlist.add(new ResponseWrapper(sr.getId(),err.getMessage(),400));
}
}
}
return wrperlist;
}
global class AccountBiilingDate{
public String ACC_ID; //Account Id
public String BILLDATE; //yyyy-mm-dd format
}
}
----------------------------------------------------------------------------------
global class ResponseWrapper{
public String Id ;
public string Status;
public Integer Statuscode;
public ResponseWrapper(String Id, String statusMsg,Integer statusCode){
this.Id = Id;
this.Status = statusMsg;
this.Statuscode = statusCode;
}
}
I am new to writting apex for integration. I have written the logic and it's working fine. Can someone please help me to write test class the below class as I am unable to do so. I have tried multiple times but the class is not getting covered.
@RestResource(urlMapping='/AccountBiilingDate/*')
global class AccountBiilingDateUpdateClass{
@HttpPost
global static List<ResponseWrapper> updateAccBillDate(){
RestRequest accBillDateRequest = RestContext.request;
String jsonResponse = '';
RestResponse res = RestContext.response;
String requestBody = accBillDateRequest.requestBody.toString();
List<AccountBiilingDate> accRecsReceived = (List<AccountBiilingDate>) JSON.deserialize(requestBody, List<AccountBiilingDate>.class);
List<ResponseWrapper> wrperlist = new List<ResponseWrapper>();
List<Account> accListToUpdt = new List<Account>();
Set<Account> accSetToUpdate = new Set<Account>();
for(AccountBiilingDate accBillDate : accRecsReceived){
Account acc = new Account();
acc.Id = Id.valueOf(accBillDate.ACC_ID);
If(accBillDate.BILLDATE != ''){
acc.Billing_Date__c = Date.valueOf(accBillDate.BILLDATE);
}else{
acc.Billing_Date__c = NULL;
}
accSetToUpdate.add(acc);
}
accListToUpdt .addALL(accSetToUpdate);
Database.SaveResult[] srList = database.update(accListToUpdt,false);
for (Database.SaveResult sr : srList){
if (sr.isSuccess()){
jsonResponse = 'Successfully updated';
res.responseBody = blob.valueOf(jsonResponse);
wrperlist.add(new ResponseWrapper(sr.getId(),jsonResponse,200));
}else {
for(Database.Error err : sr.getErrors()) {
res.responseBody = Blob.valueOf(err.getMessage());
wrperlist.add(new ResponseWrapper(sr.getId(),err.getMessage(),400));
}
}
}
return wrperlist;
}
global class AccountBiilingDate{
public String ACC_ID; //Account Id
public String BILLDATE; //yyyy-mm-dd format
}
}
----------------------------------------------------------------------------------
global class ResponseWrapper{
public String Id ;
public string Status;
public Integer Statuscode;
public ResponseWrapper(String Id, String statusMsg,Integer statusCode){
this.Id = Id;
this.Status = statusMsg;
this.Statuscode = statusCode;
}
}
Refer the below links will help you to proceed further on your test class.
https://www.infallibletechie.com/2018/07/sample-inbound-rest-api-test-class-in.html
https://dfc-org-production.force.com/forums/?id=9062I000000BmWxQAK
Thanks!!