You need to sign in to do that
Don't have an account?

Please do help with test class
HI Folks
I got strucked with test class, Please help with Test Class for below class.
I got strucked with test class, Please help with Test Class for below class.
@RestResource(urlMapping='/api/IVR/*') global class IVRRestAPI { @HttpPost global static List<IVR__c> CreateIVR(List<createIVR> icretIVR) { try{ Set<String> loanId = new Set<String>(); List<Loan__c> loanlst = new List<Loan__c>(); Map<String,String> mpidtoname = new Map<String,String>(); List<IVR__c> IVRlst = new List<IVR__c>(); for(createIVR icret:icretIVR) { if(icret.LoanID != null) { loanId.add(icret.LoanID); } } System.debug('loanId '+loanId); if(loanId.size() > 0) { loanlst = [SELECT Id,Name FROM Loan__c WHERE Name IN:loanId]; } System.debug('loanlst '+loanlst); if(loanlst.size() > 0) { for(Loan__c iloan:loanlst) { mpidtoname.put(iloan.Name,iloan.Id); } } System.debug('mpidtoname '+mpidtoname); for(createIVR icret:icretIVR) { IVR__c IVR = new IVR__c(); IVR.Name = icret.IVRId; IVR.Disbursed_Amount__c = Decimal.valueOf(icret.DisbursedAmount); IVR.Merchant_Name__c = icret.MerchantName; IVR.Disbursed_Date_Time__c = date.ValueOf(icret.DisbursedDateTime); IVR.IVR_Number__c = icret.IVRNumber; if(mpidtoname.containskey(icret.LoanID)) { IVR.Loan_Id__c = mpidtoname.get(icret.LoanID); } IVRlst.add(IVR); } if(IVRlst.size() > 0) { insert IVRlst; return IVRlst; } return null; }catch(exception e){System.debug(e.getmessage() + e.getlinenumber());return null;} } global class createIVR { public String IVRId; public String LoanID; public String DisbursedAmount; public String MerchantName; Public String DisbursedDateTime; Public String IVRNumber; } }
I have tried with test class to get test code but unfortunately i got just "0" Please do help.
@istest public class IVRRestAPITest{ static testMethod void IVRRestAPI(){ IVR__c TestOpp=new IVR__c (); TestOpp.name='TestOpp1'; TestOpp.Disbursed_Amount__c=234567; TestOpp.IVR_Number__c ='2345678'; TestOpp.Merchant_Name__c ='Test'; insert TestOpp; IVRRestAPI reqst=new IVRRestAPI(); String JsonMsg=JSON.serialize(reqst); Test.startTest(); RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = '/services/apexrest/api/IVR/CreateIVR'; //Request URL req.httpMethod = 'POST'; req.requestBody = Blob.valueof(JsonMsg); RestContext.request = req; RestContext.response= res; Test.stopTest(); } }
Please do help.
Thanks in Advance.
icretIVRList.add(newIVRRestAPI.CreateIVR('Name',loan.Name,'DisbursedAmount','SanctionAmount','MerchantName','IVRNumber') );
copy and paste this in your test class at line 23
All Answers
Actually you're not passing parameters to your method. Because of that method is not called.
You need to do few changes: Here:
createIVR = Your wrapper class
CreateIVR = Your global method name.
Hope it helps.
Mahalo,
Aamir AH Khan
Lead Salesforce Lightning Developer
IVRRestAPI.CreateIVR(pass the icretIVR list);
Hi I have added, But am getting error
Error: Compile Error: Method does not exist or incorrect signature: void CreateUTR(String, String, String, String, String) from the type UTRRestAPI at line 30 column 43
Please do help.List<createIVR> icretIVRList = new List<createIVR> icretIVR();
icretIVR.add('some value');
then
IVRRestAPI.CreateIVR(icretIVRList );
Hi manj_SFDC
Can you please do modify my code and give corrected code.
icretIVRList.add(new CreateIVR('Name','Disbursed_Amount__c','Disbursed_Date_Time__c',IVR_Number__c','Merchant_Name__c') );
IVRRestAPI.CreateIVR(icretIVRList );
add it after line 24
Hope it helps.
Mahalo,
Aamir AH Khan
Lead Salesforce Lightning Developer
getting error,Please help.
Error: Compile Error: Missing closing quote character ' on string. at line 30 column 144
for this string IVR_Number__c add it
Manj_SFDC
Getthing This error now
Error: Compile Error: Constructor not defined: [IVRRestAPI.createIVR].<Constructor>(String, String, String, String, String) at line 30 column 32
add this in your createIVR wrapper class in IVRRestAPI class
am getting errors again.
If you dont mind Can you please give me the complete corrected code,
Test Class
Error : Error: Compile Error: Constructor not defined: [UTRRestAPI.createUTR].<Constructor>(String, String, String, String, String) at line 30 column 32
Where shoud i add Constructor.
Please do help with corrected code.
global class IVRRestAPI
{
@HttpPost
global static List<IVR__c> CreateIVR(List<createIVR> icretIVR)
{
try{
Set<String> loanId = new Set<String>();
List<Loan__c> loanlst = new List<Loan__c>();
Map<String,String> mpidtoname = new Map<String,String>();
List<IVR__c> IVRlst = new List<IVR__c>();
for(createIVR icret:icretIVR)
{
if(icret.LoanID != null)
{
loanId.add(icret.LoanID);
}
}
System.debug('loanId '+loanId);
if(loanId.size() > 0)
{
loanlst = [SELECT Id,Name FROM Loan__c WHERE Name IN:loanId];
}
System.debug('loanlst '+loanlst);
if(loanlst.size() > 0)
{
for(Loan__c iloan:loanlst)
{
mpidtoname.put(iloan.Name,iloan.Id);
}
}
System.debug('mpidtoname '+mpidtoname);
for(createIVR icret:icretIVR)
{
IVR__c IVR = new IVR__c();
IVR.Name = icret.IVRId;
IVR.Disbursed_Amount__c = Decimal.valueOf(icret.DisbursedAmount);
IVR.Merchant_Name__c = icret.MerchantName;
IVR.Disbursed_Date_Time__c = date.ValueOf(icret.DisbursedDateTime);
IVR.IVR_Number__c = icret.IVRNumber;
if(mpidtoname.containskey(icret.LoanID))
{
IVR.Loan_Id__c = mpidtoname.get(icret.LoanID);
}
IVRlst.add(IVR);
}
if(IVRlst.size() > 0)
{
insert IVRlst;
return IVRlst;
}
return null;
}catch(exception e){System.debug(e.getmessage() + e.getlinenumber());return null;}
}
global class createIVR
{
public String IVRId;
public String LoanID;
public String DisbursedAmount;
public String MerchantName;
Public String DisbursedDateTime;
Public String IVRNumber;
public createIVR(String IVRId,String LoanID,String DisbursedAmount,String MerchantName,String DisbursedDateTime,String IVRNumber){}
}
}
---------------------------------------------------------------------------------------------------
@istest
public class IVRRestAPITest{
static testMethod void IVRRestAPI(){
IVR__c TestOpp=new IVR__c ();
TestOpp.name='TestOpp1';
TestOpp.Disbursed_Amount__c=234567;
TestOpp.IVR_Number__c ='2345678';
TestOpp.Merchant_Name__c ='Test';
insert TestOpp;
IVRRestAPI reqst=new IVRRestAPI();
String JsonMsg=JSON.serialize(reqst);
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/services/apexrest/api/IVR/CreateIVR'; //Request URL
req.httpMethod = 'POST';//HTTP Request Type
req.requestBody = Blob.valueof(JsonMsg);
RestContext.request = req;
RestContext.response= res;
Test.startTest();
List<createIVR> icretIVRList = new List<createIVR>();
icretIVRList.add(new CreateIVR('Name','Disbursed_Amount__c','Disbursed_Date_Time__c','IVR_Number__c','Merchant_Name__c') );
IVRRestAPI.CreateIVR(icretIVRList );
Test.stopTest();
}
}
manj_SFDC,
Error: Compile Error: Invalid type: createIVR at line 25 column 5
HI manj_SFDC,
After added and executed i got 63%. please do help to get %
Please do help.
Hi manj_SFDC,
Below lines are not covered in Test class.
Lines :
18 loanId.add(icret.LoanID);
24 loanlst = [SELECT Id,Name FROM Loan__c WHERE Name IN:loanId];
29 for(Loan__c iloan:loanlst)
30 {
31 mpidtoname.put(iloan.Name,iloan.Id);
41 IVR.Loan_Id__c = mpidtoname.get(icret.LoanID);
49 IVR.Disbursed_Date_Time__c = date.ValueOf(icret.DisbursedDateTime);
50 IVRlst.add(IVR);
51 }
52 if(IVRlst.size() > 0)
53 {
54 insert IVRlst;
55 return IVRlst;
56 }
57 return null;
Please help
Thanks In Advance.
Loan__c loan = new Loan__c();
//add the required fields
insert loan;
icretIVRList.add(new IVRRestAPI.CreateIVR('Name',loan.id,'DisbursedAmount','MerchantName','DisbursedDateTime','IVRNumber') ); IVRRestAPI.CreateIVR(icretIVRList); Test.stopTest();
Mark this as the answer if it helps you !
icretIVRList.add(newIVRRestAPI.CreateIVR('Name',loan.id,'DisbursedAmount','DisbursedDateTime','MerchantName','IVRNumber') );
change your wrapper class constructor in the calss to
public createIVR(String IVRId,String LoanID,String DisbursedAmount,String MerchantName,String DisbursedDateTime,String IVRNumber){
this.IVRId = IVRId;
this.LoanID = LoanID;
this.DisbursedAmount = DisbursedAmount;
this.MerchantName = MerchantName;
this.DisbursedDateTime = DisbursedDateTime;
this.IVRNumber = IVRNumber;
}
Now code coverage is 74%, Please check updated Class and Test class
Test Class
Below lines are not covered in Test class
28 for(Loan__c iloan:loanlst)
29 {
30 mpidtoname.put(iloan.Name,iloan.Id);
40 IVR.Loan_Id__c = mpidtoname.get(icret.LoanID);
47 IVR.Disbursed_Amount__c = Decimal.valueOf(icret.DisbursedAmount);
48 IVRlst.add(IVR);
49 }
50 if(IVRlst.size() > 0)
51 {
52 insert IVRlst;
53 return IVRlst;
54 }
55 return null;
change line 23 to i
cretIVRList.add(newIVRRestAPI.CreateIVR('Name',loan.Id,'DisbursedAmount','SanctionAmount','MerchantName','IVRNumber') );
you should bot have the quote for loan.Id, I had pinged this earlier too , copy and paste above line
copy and paste this in your test class at line 23
i have already did the same thing but code coverage not increased.
Updated test class
Below Lines was not covered.
28 for(Loan__c iloan:loanlst)
29 {
30 mpidtoname.put(iloan.Name,iloan.Id);
40 IVR.Loan_Id__c = mpidtoname.get(icret.LoanID);
47 IVR.Disbursed_Amount__c = Decimal.valueOf(icret.DisbursedAmount);
48 IVRlst.add(IVR);
49 }
50 if(IVRlst.size() > 0)
51 {
52 insert IVRlst;
53 return IVRlst;
54 }
55 return null;
it should be
loanlst = [SELECT Id,Name FROM Loan__c WHERE Id IN:loanId];
icretIVRList.add(newIVRRestAPI.CreateIVR('Name',loan.Name,'DisbursedAmount','SanctionAmount','MerchantName','IVRNumber') );
copy and paste this in your test class at line 23
i have change loan.id to loan.name then its increased to 84%.
icretUTRList.add(new UTRRestAPI.CreateUTR('Name',loan.Name,'DisbursedAmount','SanctionAmount','MerchantName','UTRNumber') );
Now below lines are not covered
47 IVR.Disbursed_Amount__c = Decimal.valueOf(icret.DisbursedAmount);
48 IVRlst.add(IVR);
49 }
50 if(IVRlst.size() > 0)
51 {
52 insert IVRlst;
53 return IVRlst;
54 }
55 return null;