function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
R R MR R M 

Please do help with test class

HI Folks

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.

Best Answer chosen by R R M
Manj_SFDCManj_SFDC
if you dont want to change that SOQL then you can do this 

icretIVRList.add(newIVRRestAPI.CreateIVR('Name',loan.Name,'DisbursedAmount','SanctionAmount','MerchantName','IVRNumber') );

copy and paste this in your test class at line 23

All Answers

Aamir KhanAamir Khan
Aloha,

Actually you're not passing parameters to your method. Because of that method is not called.

You need to do few changes:
Test.startTest(); 
IVRRestAPI.createIVR results = IVRRestAPI.CreateIVR("Pass the parameter i.e. List<createIVR>"); 
Test.stopTest();
Here:
createIVR = Your wrapper class
CreateIVR = Your global method name.

Hope it helps.

Mahalo,
Aamir AH Khan
Lead Salesforce Lightning Developer
Manj_SFDCManj_SFDC
Create a List of icretIVRs and and after line 24 you can add this
IVRRestAPI.CreateIVR(pass the icretIVR list);
R R MR R M

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

Test.startTest(); 
IVRRestAPI.createIVR results = IVRRestAPI.CreateIVR('Name','Disbursed_Amount__c','Disbursed_Date_Time__c',IVR_Number__c','Merchant_Name__c'); 
Test.stopTest();
Please do help.
Manj_SFDCManj_SFDC
you need to create a list  like
List<createIVR> icretIVRList  = new List<createIVR> icretIVR();
icretIVR.add('some value');
then
IVRRestAPI.CreateIVR(icretIVRList );

 
R R MR R M

Hi manj_SFDC

Can you please do modify my code and give corrected code.

Manj_SFDCManj_SFDC
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 );

add it after line 24
 
Aamir KhanAamir Khan
Try this:
Test.startTest(); 
IVRRestAPI.createIVR results = new IVRRestAPI.createIVR ('Name','Disbursed_Amount__c','Disbursed_Date_Time__c',IVR_Number__c','Merchant_Name__c'); 
Test.stopTest();

Hope it helps.

Mahalo,
Aamir AH Khan
Lead Salesforce Lightning Developer
Manj_SFDCManj_SFDC
IVRRestAPI.createIVR results = new IVRRestAPI.createIVR ('Name','Disbursed_Amount__c','Disbursed_Date_Time__c','IVR_Number__c','Merchant_Name__c');
Manj_SFDCManj_SFDC
enclose this in Test.startTest() and Test.stopTest()
R R MR R M
HI Aamir khan,

getting error,Please help. 
Error: Compile Error: Missing closing quote character ' on string. at line 30 column 144
Manj_SFDCManj_SFDC
you are missing a starting quote(') 
for this string IVR_Number__c add it
Manj_SFDCManj_SFDC
it should be 'IVR_Number__c'
R R MR R M

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

Manj_SFDCManj_SFDC
public createIVR(String IVRId,String LoanID,String DisbursedAmount,String MerchantName,String DisbursedDateTime,String IVRNumber){}

add this in your createIVR wrapper class in IVRRestAPI class 
R R MR R M
manj_SFDC, 
am getting errors again. 
If you dont mind Can you please give me the complete corrected code,
 
Manj_SFDCManj_SFDC
can you please paste your updated class and Test class code and eror your facing
 
R R MR R M
@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;
    }
}


Test Class

@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(); 
IVRRestAPI.createIVR results = new IVRRestAPI.createIVR ('Name','Disbursed_Amount__c','Disbursed_Date_Time__c','IVR_Number__c','Merchant_Name__c'); 
Test.stopTest();
   }
}

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.

 

Manj_SFDCManj_SFDC
@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;
        
        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();
   }
}
R R MR R M

manj_SFDC,

Error: Compile Error: Invalid type: createIVR at line 25 column 5

@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_SFDCManj_SFDC
Change it to IVRRestAPI.createIVR from createIVR
R R MR R M

HI manj_SFDC, 

After added and executed i got 63%. please do help to get %

@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();
                         
            if(mpidtoname.containskey(icret.LoanID))
            {
                IVR.Loan_Id__c = mpidtoname.get(icret.LoanID);
            }
            
            IVR.Name = icret.IVRId;
            IVR.Merchant_Name__c = icret.MerchantName; 
            IVR.IVR_Number__c = icret.IVRNumber;
            IVR.Disbursed_Amount__c = Decimal.valueOf(icret.DisbursedAmount);
            
            IVR.Disbursed_Date_Time__c = date.ValueOf(icret.DisbursedDateTime);
            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  IVRRestAPITest(){

   IVRRestAPI reqst=new IVRRestAPI();
   String JsonMsg=JSON.serialize(reqst);

   RestRequest req = new RestRequest();   
    req.addHeader('Content-Type', 'application/json'); 
    req.requestURI = '/services/apexrest/api/IVR/CreateIVR';
    req.httpMethod = 'POST';
    req.requestBody = Blob.valueof(JsonMsg);
    
    RestResponse res = new RestResponse();
    RestContext.request = req;
    RestContext.response= res;

    Test.startTest();
    List<IVRRestAPI.createIVR> icretIVRList  = new List<IVRRestAPI.createIVR>();
    icretIVRList.add(new IVRRestAPI.CreateIVR('Name','LoanID','DisbursedAmount','MerchantName','DisbursedDateTime','IVRNumber') );
    IVRRestAPI.CreateIVR(icretIVRList);
    Test.stopTest();
   }
}

Please do help.
Manj_SFDCManj_SFDC
can you let me know which lines are not getting covered, you can check it from dev console
R R MR R M

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.

 

Manj_SFDCManj_SFDC
Test.startTest(); List<IVRRestAPI.createIVR> icretIVRList = new List<IVRRestAPI.createIVR>();
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 !
R R MR R M
Added but Lines was not covered, still % is 63 only.
Manj_SFDCManj_SFDC
paste yor updated Test Class
R R MR R M
@istest
public class IVRRestAPITest{
 static testMethod void  IVRRestAPITest(){
   
   IVRRestAPI reqst=new IVRRestAPI();
   String JsonMsg=JSON.serialize(reqst);

   RestRequest req = new RestRequest(); 
    req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
    req.requestURI = '/services/apexrest/api/IVR/CreateIVR';  //Request URL
    req.httpMethod = 'POST';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    
    RestResponse res = new RestResponse();
    RestContext.request = req;
    RestContext.response= res;

Test.startTest();

    List<IVRRestAPI.createIVR> icretIVRList  = new List<IVRRestAPI.createIVR>();
    Loan__c loan = new Loan__c();
    loan.name='Test';
    insert loan;
    icretIVRList.add(new IVRRestAPI.CreateIVR('Name','LoanID','DisbursedAmount','DisbursedDateTime','MerchantName','IVRNumber') );
    IVRRestAPI.CreateIVR(icretIVRList);
    Test.stopTest();

   }
}

 
Manj_SFDCManj_SFDC
change line 24 to 

icretIVRList.add(newIVRRestAPI.CreateIVR('Name',loan.id,'DisbursedAmount','DisbursedDateTime','MerchantName','IVRNumber') );
R R MR R M
Changed but % not increased.
Manj_SFDCManj_SFDC
paste your line 24 here, again the same lines not covered?
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;
        
        }
R R MR R M

Now code coverage is 74%, Please check updated Class and Test 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();
                        
            if(mpidtoname.containskey(icret.LoanID))
            {
                IVR.Loan_Id__c = mpidtoname.get(icret.LoanID);
            }
            
            IVR.Name = icret.IVRId;
            IVR.Merchant_Name__c = icret.MerchantName; 
            IVR.IVR_Number__c = icret.IVRNumber;
            IVR.Sanction_Amount__c = Decimal.valueOf(icret.SanctionAmount);
            IVR.Disbursed_Amount__c = Decimal.valueOf(icret.DisbursedAmount);
            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 SanctionAmount;
        public String MerchantName;
        //Public String DisbursedDateTime;
        Public String IVRNumber;
        
        public createIVR(String IVRId,String LoanID,String DisbursedAmount,String MerchantName,String SanctionAmount,String IVRNumber){
            this.IVRId = IVRId;
            this.LoanID = LoanID;
            this.DisbursedAmount = DisbursedAmount;
            this.MerchantName = MerchantName;
            this.SanctionAmount= SanctionAmount;
            this.IVRNumber = IVRNumber;       
        }        
    }
}


Test Class 
@istest
public class IVRRestAPITest{
 static testMethod void  IVRRestAPITest(){

   IVRRestAPI reqst=new IVRRestAPI();
   String JsonMsg=JSON.serialize(reqst);

   RestRequest req = new RestRequest();  
    req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
    req.requestURI = '/services/apexrest/api/IVR/CreateIVR';  //Request URL
    req.httpMethod = 'POST';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    
    RestResponse res = new RestResponse();
    RestContext.request = req;
    RestContext.response= res;

    Test.startTest();
    List<IVRRestAPI.createIVR> icretIVRList  = new List<IVRRestAPI.createIVR>();
    Loan__c loan = new Loan__c();
    loan.name='Test';
    insert loan;
    icretIVRList.add(new IVRRestAPI.CreateIVR('Name','Loan.ID','DisbursedAmount','SanctionAmount','MerchantName','IVRNumber') );
    IVRRestAPI.CreateIVR(icretIVRList);
    Test.stopTest();
   }
}
R R MR R M

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;

 

Manj_SFDCManj_SFDC
you are doing it incorreclty in the test class
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
Manj_SFDCManj_SFDC
icretIVRList.add(newIVRRestAPI.CreateIVR('Name',loan.Id,'DisbursedAmount','SanctionAmount','MerchantName','IVRNumber') );

copy and paste this in your test class at line 23
R R MR R M

i have already did the same thing but code coverage not increased. 

Updated test class 

@istest
public class IVRRestAPITest{
 static testMethod void  IVRRestAPITest(){
 
   IVRRestAPI reqst=new IVRRestAPI();
   String JsonMsg=JSON.serialize(reqst);

   RestRequest req = new RestRequest();  
    req.addHeader('Content-Type', 'application/json'); // Add a JSON Header as it is validated
    req.requestURI = '/services/apexrest/api/IVR/CreateIVR';  //Request URL
    req.httpMethod = 'POST';//HTTP Request Type
    req.requestBody = Blob.valueof(JsonMsg);
    
    RestResponse res = new RestResponse();
    RestContext.request = req;
    RestContext.response= res;

    Test.startTest();
    List<IVRRestAPI.createIVR> icretIVRList  = new List<IVRRestAPI.createIVR>();
    Loan__c loan = new Loan__c();
    loan.name='Test';
    insert loan;
      icretIVRList.add(new IVRRestAPI.CreateIVR('Name',loan.Id,'DisbursedAmount','SanctionAmount','MerchantName','IVRNumber') );
    IVRRestAPI.CreateIVR(icretIVRList);
    Test.stopTest();
   }
}

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;
Manj_SFDCManj_SFDC
your SOQL in the class is incorrect
it should be 
loanlst = [SELECT Id,Name FROM Loan__c WHERE Id IN:loanId];
Manj_SFDCManj_SFDC
if you dont want to change that SOQL then you can do this 

icretIVRList.add(newIVRRestAPI.CreateIVR('Name',loan.Name,'DisbursedAmount','SanctionAmount','MerchantName','IVRNumber') );

copy and paste this in your test class at line 23
This was selected as the best answer
R R MR R M

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;

 

Manj_SFDCManj_SFDC
great, please mark my answer as the best answer
R R MR R M
Thanks alot Manj_SFDC for the great help