+ Start a Discussion
Tulasiram ChippalaTulasiram Chippala 

Some lines are missing in test coverage

I worte test class for below rest class, but some return response lines are not covering in code coverage....below are my code...please help me how to include those lines.
Rest class:
@HttpPost 
    global static Lead_API_Wrapper.LeadCreationResposeWrappper doPost(){
        try{
            RestRequest req = RestContext.request;
            Lead_API_Wrapper.LeadCreationRequestWrappper objRequest =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req.requestBody.toString(), 
                                                                        Lead_API_Wrapper.LeadCreationRequestWrappper.class);
            if(objRequest != null && (objRequest.PrimaryEmail != null || objRequest.PhoneNumber != null)){
                List<User> lstUser = [SELECT Id FROM User WHERE UserName = :objRequest.userEmail OR Email =: objRequest.userEmail];
                if(lstUser.isEmpty()) return new Lead_API_Wrapper.LeadCreationResposeWrappper(null, null, false, '', 'User does not exist. Please contact Salesforce Admin!'); 
                List<Lead> lstLead = new List<Lead>{getLeadMapping(objRequest, lstUser[0].Id)};
                Lead dupeLead = null;
                for(Datacloud.FindDuplicatesResult findDupeResult : Datacloud.FindDuplicates.findDuplicates(lstLead)) {
                    for(Datacloud.DuplicateResult dupeResult : findDupeResult.getDuplicateResults()){
                        for(Datacloud.MatchResult matchResult : dupeResult.getMatchResults()){
                            for(Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords()) {
                                dupeLead = (Lead)matchRecord.getRecord();
                            }
                        }
                    }
                }
                dupeLead = dupeLead != null && dupeLead.Id != null ? getLead( dupeLead.Id) : null;            
                if(dupeLead == null){
                    Lead objLead = getLeadMapping(objRequest, lstUser[0].Id);
                    insert objLead;
                    return new Lead_API_Wrapper.LeadCreationResposeWrappper(getLead(objLead.Id), null, true, 'Lead created successfully in Salesforce.', '');
                } else if(dupeLead.OwnerId != lstUser[0].Id){
                    Temp_Lead__c objTempLead = getTempLeadMapping(objRequest, lstUser[0].Id);
                    insert objTempLead;
                    return new Lead_API_Wrapper.LeadCreationResposeWrappper(null, objTempLead, false, '', 'Lead already existed in Salesforce Created By : '+ dupeLead.Owner.Name +' and your Lead Information is submitted for approval');
                } else {
                    return new Lead_API_Wrapper.LeadCreationResposeWrappper(dupeLead, null, false, '', 'Lead already existed in Salesforce');
                }
            }
            return new Lead_API_Wrapper.LeadCreationResposeWrappper(null, null, false, '', 'Invalid Data.');
        }catch(Exception ex){
            return new Lead_API_Wrapper.LeadCreationResposeWrappper(null, null, false, '', ex.getMessage());
        }
    }
    @TestVisible
    private static Lead getLead(String leadID){
        return [SELECT Id, Company, Name, Status, CreatedDate, Owner.Name, OwnerId FROM Lead WHERE Id =: leadID];
    }
    @TestVisible
    private static Temp_Lead__c getTempLeadMapping(Lead_API_Wrapper.LeadCreationRequestWrappper objRequest, String ownID){
        return new Temp_Lead__c(First_Name__c = objRequest.FirstName, Last_Name__c=objRequest.LastName, 
                                Phone_Number__c = objRequest.PhoneNumber, Primary_Email__c = objRequest.PrimaryEmail,
                                Company_Name__c = objRequest.CompanyName, AddressLine1__c = objRequest.AddressLine1, 
                                AddressLine2__c = objRequest.AddressLine2, City__c = objRequest.City,
                                State__c = objRequest.State, ZIPCode__c = objRequest.ZIPCode, 
                                Country__c = objRequest.Country, EstimatedMRR__c = objRequest.EstimatedMRR,
                                EstimatedCloseDate__c = objRequest.EstimatedCloseDate, OwnerId = ownID);
    }
    @TestVisible
    private static Lead getLeadMapping(Lead_API_Wrapper.LeadCreationRequestWrappper objRequest, String ownID){
        String street = objRequest.AddressLine1;
        street = String.isNotBlank(street) && String.isNotEmpty(street) ? + street + ', ' + objRequest.AddressLine2 : objRequest.AddressLine2;
        return new Lead(FirstName = objRequest.FirstName, LastName =objRequest.LastName, 
                        Phone = objRequest.PhoneNumber, Email = objRequest.PrimaryEmail, OwnerId = ownID,
                        Company = objRequest.CompanyName, Street = street, City = objRequest.City, 
                        State = objRequest.State, PostalCode = objRequest.ZIPCode, Country = objRequest.Country
                        /*EstimatedMRR__c = objRequest.EstimatedMRR, EstimatedCloseDate__c = objRequest.EstimatedCloseDate*/);
            
    }
}
Wrapper class:
global class Lead_API_Wrapper {
    global class LeadCreationRequestWrappper {
        global String userEmail;
        global String CompanyName;
        global String FirstName;
        global String LastName;
        global String PrimaryEmail;
        global String PhoneNumber;
        global String AddressLine1;
        global String AddressLine2;
        global String City;
        global String State;
        global String ZIPCode;
        global String Country;
        global Decimal EstimatedMRR;
        global Date EstimatedCloseDate;
    }
    
    global class LeadCreationResposeWrappper{
        global Lead lead;
        global Temp_Lead__c TempLead;
        global Boolean IsSuccess;
        global String SuccessMsg;
        global String ErrorMsg;
        
        global LeadCreationResposeWrappper(Lead lead, Temp_Lead__c TempLead, Boolean IsSuccess, String SuccessMsg, String ErrorMsg){
            this.lead = lead;
            this.TempLead = TempLead;
            this.IsSuccess = IsSuccess;
            this.SuccessMsg = SuccessMsg;
            this.ErrorMsg = ErrorMsg;
        }
    }
}

Test class: 
@isTest
public class LeadManagementTestClass {
@testSetup static void testData() {
        //String userEmail = 'testuser1.user@user.com';
        User testUser1 = new user(LastName = 'Test_User1', Email = 'test.user1@user.com', Alias = 'Tcode', Username = 'testuser1.user@user.com', CommunityNickname = 'test12', LocaleSidKey = 'en_US', TimeZoneSidKey = 'GMT', ProfileID = '00e7000000198kn', LanguageLocaleKey = 'en_US', EmailEncodingKey = 'UTF-8');
                insert testUser1;        
    }
@isTest public static void testdoPost(){    
     String user1Email = 'test.user1@user.com';
     Decimal EstimatedMRR = null;
     Date EstimatedCloseDate = null;                 
     List<User> lstUser = [SELECT Id FROM User WHERE UserName = : user1Email OR Email =: user1Email];
     Test.startTest(); 
     RestRequest req = new RestRequest();
     RestResponse res = new RestResponse();
        req.addParameter('userEmail',user1Email);
        req.addParameter('CompanyName','xyz ');
        req.addParameter('FirstName','test ');
        req.addParameter('LastName','record ');
        req.addParameter('PrimaryEmail','test.record@xyz.com');
        req.addParameter('PhoneNumber','9999999999');
        req.addParameter('AddressLine1','lane 1');
        req.addParameter('AddressLine2','lane 2');
        req.addParameter('City','pune');
        req.addParameter('State','Maharastra');
        req.addParameter('ZIPCode','411014');
        req.addParameter('Country','India');
        req.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req.requestURI = 'services/apexrest/LeadManagement/*';  
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
        req.requestBody = Blob.valueOf(JSON.serialize(req.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result = LeadManagement.doPost();
        Lead_API_Wrapper.LeadCreationRequestWrappper objRequest =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);         
        LeadManagement.getTempLeadMapping(objRequest, lstUser[0].id);                                                               
        LeadManagement.getLeadMapping(objRequest, lstUser[0].id);
     Test.stopTest();
    }
}

 
Best Answer chosen by Tulasiram Chippala
Sampath SuranjiSampath Suranji
Hi Ram I have added few comments also for the more readability, Please check.
@isTest public static void testdoPost(){  
        profile objProfile = [select id from profile where name='Standard User'];   
        
        user user2 = new user(LastName = 'Test_User1_do',Email = 'testdo.user@user.com',Alias = 'Tcode do',Username = 'testdo.user@user.com',CommunityNickname = 'testdo',LocaleSidKey = 'en_US',TimeZoneSidKey = 'GMT',ProfileID = objProfile.Id,LanguageLocaleKey = 'en_US',EmailEncodingKey = 'UTF-8');
        insert user2; 
        
        string user2Email = 'testdo.user@user.com'; 
        List<User> user2List = [SELECT Id FROM User WHERE UserName = : user2Email OR Email =: user2Email]; 
        Lead leadRec = new Lead();
        leadRec.FirstName = 'Abc'; 
        leadRec.LastName ='xyz'; 
        leadRec.Phone = '9999999999';
        leadRec.Email = 'abc@xyz.com'; 
        leadRec.OwnerId = user2List[0].id;
        leadRec.Status = 'Qualified';
        leadRec.Company = 'abcd'; 
        leadRec.Street = 'Viman Nagar'; 
        leadRec.City = 'Pune';
        leadRec.State = 'Maharastra'; 
        leadRec.PostalCode = '411014'; 
        leadRec.Country = 'India';
        leadRec.LeadSource = 'Channel Partner';
        insert leadRec;
        
        String user1Email = 'test.user1@user.com';
        Decimal EstimatedMRR = null;
        Date EstimatedCloseDate = null;                 
        List<User> lstUser = [SELECT Id FROM User WHERE UserName = : user1Email OR Email =: user1Email];
        
        Test.startTest(); 
        //pass else if part
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        req.addParameter('userEmail',user1Email);
        req.addParameter('CompanyName','xyz ');
        req.addParameter('FirstName','Abc');
        req.addParameter('LastName','xyz');
        req.addParameter('PrimaryEmail','abc@xyz.com');
        req.addParameter('PhoneNumber','9999999999');
        req.addParameter('AddressLine1','lane 1');
        req.addParameter('AddressLine2','lane 2');
        req.addParameter('City','pune');
        req.addParameter('State','Maharastra');
        req.addParameter('ZIPCode','411014');
        req.addParameter('Country','India');
        req.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req.requestURI = 'services/apexrest/LeadManagement/*';  
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
        req.requestBody = Blob.valueOf(JSON.serialize(req.params));        
        Lead_API_Wrapper.LeadCreationResposeWrappper result = LeadManagement.doPost();  
        
        //pass else part
         Lead_API_Wrapper.LeadCreationRequestWrappper objRequest =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);         
        LeadManagement.getTempLeadMapping(objRequest, lstUser[0].id);                                                               
        LeadManagement.getLeadMapping(objRequest, lstUser[0].id);
        RestRequest req2 = new RestRequest();
        RestResponse res2 = new RestResponse();
        req2.addParameter('userEmail','testdo.user@user.com');//called to else part dupeLead.OwnerId == lstUser[0].Id
        req2.addParameter('CompanyName','abcd');
        req2.addParameter('FirstName','Abc');
        req2.addParameter('LastName','xyz');
        req2.addParameter('PrimaryEmail','abc@xyz.com');
        req2.addParameter('PhoneNumber','9999999999');
        req2.addParameter('AddressLine1','ddd');
        req2.addParameter('AddressLine2','ffff');
        req2.addParameter('City','City');
        req2.addParameter('State','State');
        req2.addParameter('ZIPCode','008800');
        req2.addParameter('Country','India');
        req2.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req2.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req2.requestURI = 'services/apexrest/LeadManagement/*';  
        req2.httpMethod = 'POST';
        RestContext.request = req2;
        RestContext.response = res2;
        req2.requestBody = Blob.valueOf(JSON.serialize(req2.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result2 = LeadManagement.doPost();
        
        
        //pass if part
        delete (leadRec);
        Lead_API_Wrapper.LeadCreationRequestWrappper objRequest3 =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);         
        LeadManagement.getTempLeadMapping(objRequest3, lstUser[0].id);                                                               
        LeadManagement.getLeadMapping(objRequest3, lstUser[0].id);
        RestRequest req3 = new RestRequest();
        RestResponse res3 = new RestResponse();
        req3.addParameter('userEmail',user1Email);
        req3.addParameter('CompanyName','abcd');
        req3.addParameter('FirstName','Abc');
        req3.addParameter('LastName','xyz');
        req3.addParameter('PrimaryEmail','abc@xyz.com');
        req3.addParameter('PhoneNumber','9999999999');
        req3.addParameter('AddressLine1','ddd');
        req3.addParameter('AddressLine2','ffff');
        req3.addParameter('City','City');
        req3.addParameter('State','State');
        req3.addParameter('ZIPCode','008800');
        req3.addParameter('Country','India');
        req3.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req3.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req3.requestURI = 'services/apexrest/LeadManagement/*';  
        req3.httpMethod = 'POST';
        RestContext.request = req3;
        RestContext.response = res3;
        req3.requestBody = Blob.valueOf(JSON.serialize(req3.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result3 = LeadManagement.doPost();
        Test.stopTest();
    }
Best Regards
Sampath
 

All Answers

Sampath SuranjiSampath Suranji
Hi Ram,

Please add below cde part before you call the ''doPost()'' method,
Lead objLead = new Lead(FirstName = 'test ', LastName ='record ', 
                        Phone = '9999999999', Email = 'test.record@xyz.com', OwnerId = lstUser[0].Id,
                        Company = 'xyz' , Street = 'lane 1,lane 2,', City = 'pune', 
                        State = 'Maharastra', PostalCode = '411014', Country = 'India');
    
        insert objLead;

Best regards
Sampath
Tulasiram ChippalaTulasiram Chippala
Hi sampath, i used like below it increased but not full coverage . Last reurn responses are not included. And no temp lead is creating. System.assert is failing please look once.
*/
    @isTest public static void testdoPost(){ 
    user user2 = new user(LastName = 'Test_User1_do',Email = 'testdo.user@user.com',Alias = 'Tcode do',Username = 'testdo.user@user.com',CommunityNickname = 'testdo',LocaleSidKey = 'en_US',TimeZoneSidKey = 'GMT',ProfileID = '00e7000000198kn',LanguageLocaleKey = 'en_US',EmailEncodingKey = 'UTF-8');
                
        insert user2; 
        string user2Email = 'testdo.user@user.com'; 
        List<User> user2List = [SELECT Id FROM User WHERE UserName = : user2Email OR Email =: user2Email]; 
        Lead leadRec = new Lead();
                leadRec.FirstName = 'Abc'; 
                leadRec.LastName ='xyz'; 
                leadRec.Phone = '9999999999';
                leadRec.Email = 'abc@xyz.com'; 
                leadRec.OwnerId = user2List[0].id;
                leadRec.Status = 'Qualified';
                leadRec.Company = 'abcd'; 
                leadRec.Street = 'Viman Nagar'; 
                leadRec.City = 'Pune';
                leadRec.State = 'Maharastra'; 
                leadRec.PostalCode = '411014'; 
                leadRec.Country = 'India';
                leadRec.LeadSource = 'Channel Partner';
                insert leadRec;
        
     String user1Email = 'test.user1@user.com';
     Decimal EstimatedMRR = null;
     Date EstimatedCloseDate = null;                 
     List<User> lstUser = [SELECT Id FROM User WHERE UserName = : user1Email OR Email =: user1Email];
     Lead objLead = new Lead(FirstName = 'test ', LastName ='record ', 
                        Phone = '9999999999', Email = 'test.record@xyz.com', OwnerId = lstUser[0].Id,
                        Company = 'xyz' , Street = 'lane 1,lane 2,', City = 'pune', 
                        State = 'Maharastra', PostalCode = '411014', Country = 'India');
                        insert objLead;
     Test.startTest(); 
     RestRequest req = new RestRequest();
     RestResponse res = new RestResponse();
        req.addParameter('userEmail',user1Email);
        req.addParameter('CompanyName','xyz ');
        req.addParameter('FirstName','test ');
        req.addParameter('LastName','record ');
        req.addParameter('PrimaryEmail','test.record@xyz.com');
        req.addParameter('PhoneNumber','9999999999');
        req.addParameter('AddressLine1','lane 1');
        req.addParameter('AddressLine2','lane 2');
        req.addParameter('City','pune');
        req.addParameter('State','Maharastra');
        req.addParameter('ZIPCode','411014');
        req.addParameter('Country','India');
        req.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req.requestURI = 'services/apexrest/LeadManagement/*';  
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
        req.requestBody = Blob.valueOf(JSON.serialize(req.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result = LeadManagement.doPost();  
            
        Lead_API_Wrapper.LeadCreationRequestWrappper objRequest =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);         
        LeadManagement.getTempLeadMapping(objRequest, lstUser[0].id);                                                               
        LeadManagement.getLeadMapping(objRequest, lstUser[0].id);
     RestRequest req2 = new RestRequest();
     RestResponse res2 = new RestResponse();
        req2.addParameter('userEmail',user1Email);
        req2.addParameter('CompanyName','abcd');
        req2.addParameter('FirstName','Abc');
        req2.addParameter('LastName','record');
        req2.addParameter('PrimaryEmail','abc@xyz.com');
        req2.addParameter('PhoneNumber','9999999999');
        req2.addParameter('AddressLine1','ddd');
        req2.addParameter('AddressLine2','ffff');
        req2.addParameter('City','City');
        req2.addParameter('State','State');
        req2.addParameter('ZIPCode','008800');
        req2.addParameter('Country','India');
        req2.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req2.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req2.requestURI = 'services/apexrest/LeadManagement/*';  
        req2.httpMethod = 'POST';
        RestContext.request = req2;
        RestContext.response = res2;
        req2.requestBody = Blob.valueOf(JSON.serialize(req2.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result2 = LeadManagement.doPost();
        Lead_API_Wrapper.LeadCreationRequestWrappper objRequest2 =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req2.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);
        /* List<Temp_Lead__c> tempObj = [select id from Temp_Lead__c];
        integer size = 1;
        system.assertEquals(size, tempObj.size()); */
     Test.stopTest();
    }

 
Sampath SuranjiSampath Suranji
Hi Ram,
Please check your Duplicate Rule. I tested a duplicate rule with the matching rule of 'Standard Lead Matching Rule'.
If you are using any custom matching rule, please create test data accordinglly.
Here is the test code,
@isTest public static void testdoPost(){  
        profile objProfile = [select id from profile where name='Standard User'];   
        
        user user2 = new user(LastName = 'Test_User1_do',Email = 'testdo.user@user.com',Alias = 'Tcode do',Username = 'testdo.user@user.com',CommunityNickname = 'testdo',LocaleSidKey = 'en_US',TimeZoneSidKey = 'GMT',ProfileID = objProfile.Id,LanguageLocaleKey = 'en_US',EmailEncodingKey = 'UTF-8');
        insert user2; 
        
        string user2Email = 'testdo.user@user.com'; 
        List<User> user2List = [SELECT Id FROM User WHERE UserName = : user2Email OR Email =: user2Email]; 
        Lead leadRec = new Lead();
        leadRec.FirstName = 'Abc'; 
        leadRec.LastName ='xyz'; 
        leadRec.Phone = '9999999999';
        leadRec.Email = 'abc@xyz.com'; 
        leadRec.OwnerId = user2List[0].id;
        leadRec.Status = 'Qualified';
        leadRec.Company = 'abcd'; 
        leadRec.Street = 'Viman Nagar'; 
        leadRec.City = 'Pune';
        leadRec.State = 'Maharastra'; 
        leadRec.PostalCode = '411014'; 
        leadRec.Country = 'India';
        leadRec.LeadSource = 'Channel Partner';
        insert leadRec;
        
        String user1Email = 'test.user1@user.com';
        Decimal EstimatedMRR = null;
        Date EstimatedCloseDate = null;                 
        List<User> lstUser = [SELECT Id FROM User WHERE UserName = : user1Email OR Email =: user1Email];
        
        Test.startTest(); 
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        req.addParameter('userEmail',user1Email);
        req.addParameter('CompanyName','xyz ');
        req.addParameter('FirstName','Abc');
        req.addParameter('LastName','xyz');
        req.addParameter('PrimaryEmail','abc@xyz.com');
        req.addParameter('PhoneNumber','9999999999');
        req.addParameter('AddressLine1','lane 1');
        req.addParameter('AddressLine2','lane 2');
        req.addParameter('City','pune');
        req.addParameter('State','Maharastra');
        req.addParameter('ZIPCode','411014');
        req.addParameter('Country','India');
        req.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req.requestURI = 'services/apexrest/LeadManagement/*';  
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
        req.requestBody = Blob.valueOf(JSON.serialize(req.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result = LeadManagement.doPost();  
        
        delete (leadRec);
        Lead_API_Wrapper.LeadCreationRequestWrappper objRequest =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);         
        LeadManagement.getTempLeadMapping(objRequest, lstUser[0].id);                                                               
        LeadManagement.getLeadMapping(objRequest, lstUser[0].id);
        RestRequest req2 = new RestRequest();
        RestResponse res2 = new RestResponse();
        req2.addParameter('userEmail',user1Email);
        req2.addParameter('CompanyName','abcd');
        req2.addParameter('FirstName','Abc');
        req2.addParameter('LastName','xyz');
        req2.addParameter('PrimaryEmail','abc@xyz.com');
        req2.addParameter('PhoneNumber','9999999999');
        req2.addParameter('AddressLine1','ddd');
        req2.addParameter('AddressLine2','ffff');
        req2.addParameter('City','City');
        req2.addParameter('State','State');
        req2.addParameter('ZIPCode','008800');
        req2.addParameter('Country','India');
        req2.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req2.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req2.requestURI = 'services/apexrest/LeadManagement/*';  
        req2.httpMethod = 'POST';
        RestContext.request = req2;
        RestContext.response = res2;
        req2.requestBody = Blob.valueOf(JSON.serialize(req2.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result2 = LeadManagement.doPost();
        Lead_API_Wrapper.LeadCreationRequestWrappper objRequest2 =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req2.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);
        /* List<Temp_Lead__c> tempObj = [select id from Temp_Lead__c];
integer size = 1;
system.assertEquals(size, tempObj.size()); */
        Test.stopTest();
    }

Best Regards
Sampath
Sampath SuranjiSampath Suranji
Hi Ram I have added few comments also for the more readability, Please check.
@isTest public static void testdoPost(){  
        profile objProfile = [select id from profile where name='Standard User'];   
        
        user user2 = new user(LastName = 'Test_User1_do',Email = 'testdo.user@user.com',Alias = 'Tcode do',Username = 'testdo.user@user.com',CommunityNickname = 'testdo',LocaleSidKey = 'en_US',TimeZoneSidKey = 'GMT',ProfileID = objProfile.Id,LanguageLocaleKey = 'en_US',EmailEncodingKey = 'UTF-8');
        insert user2; 
        
        string user2Email = 'testdo.user@user.com'; 
        List<User> user2List = [SELECT Id FROM User WHERE UserName = : user2Email OR Email =: user2Email]; 
        Lead leadRec = new Lead();
        leadRec.FirstName = 'Abc'; 
        leadRec.LastName ='xyz'; 
        leadRec.Phone = '9999999999';
        leadRec.Email = 'abc@xyz.com'; 
        leadRec.OwnerId = user2List[0].id;
        leadRec.Status = 'Qualified';
        leadRec.Company = 'abcd'; 
        leadRec.Street = 'Viman Nagar'; 
        leadRec.City = 'Pune';
        leadRec.State = 'Maharastra'; 
        leadRec.PostalCode = '411014'; 
        leadRec.Country = 'India';
        leadRec.LeadSource = 'Channel Partner';
        insert leadRec;
        
        String user1Email = 'test.user1@user.com';
        Decimal EstimatedMRR = null;
        Date EstimatedCloseDate = null;                 
        List<User> lstUser = [SELECT Id FROM User WHERE UserName = : user1Email OR Email =: user1Email];
        
        Test.startTest(); 
        //pass else if part
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        req.addParameter('userEmail',user1Email);
        req.addParameter('CompanyName','xyz ');
        req.addParameter('FirstName','Abc');
        req.addParameter('LastName','xyz');
        req.addParameter('PrimaryEmail','abc@xyz.com');
        req.addParameter('PhoneNumber','9999999999');
        req.addParameter('AddressLine1','lane 1');
        req.addParameter('AddressLine2','lane 2');
        req.addParameter('City','pune');
        req.addParameter('State','Maharastra');
        req.addParameter('ZIPCode','411014');
        req.addParameter('Country','India');
        req.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req.requestURI = 'services/apexrest/LeadManagement/*';  
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response = res;
        req.requestBody = Blob.valueOf(JSON.serialize(req.params));        
        Lead_API_Wrapper.LeadCreationResposeWrappper result = LeadManagement.doPost();  
        
        //pass else part
         Lead_API_Wrapper.LeadCreationRequestWrappper objRequest =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);         
        LeadManagement.getTempLeadMapping(objRequest, lstUser[0].id);                                                               
        LeadManagement.getLeadMapping(objRequest, lstUser[0].id);
        RestRequest req2 = new RestRequest();
        RestResponse res2 = new RestResponse();
        req2.addParameter('userEmail','testdo.user@user.com');//called to else part dupeLead.OwnerId == lstUser[0].Id
        req2.addParameter('CompanyName','abcd');
        req2.addParameter('FirstName','Abc');
        req2.addParameter('LastName','xyz');
        req2.addParameter('PrimaryEmail','abc@xyz.com');
        req2.addParameter('PhoneNumber','9999999999');
        req2.addParameter('AddressLine1','ddd');
        req2.addParameter('AddressLine2','ffff');
        req2.addParameter('City','City');
        req2.addParameter('State','State');
        req2.addParameter('ZIPCode','008800');
        req2.addParameter('Country','India');
        req2.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req2.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req2.requestURI = 'services/apexrest/LeadManagement/*';  
        req2.httpMethod = 'POST';
        RestContext.request = req2;
        RestContext.response = res2;
        req2.requestBody = Blob.valueOf(JSON.serialize(req2.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result2 = LeadManagement.doPost();
        
        
        //pass if part
        delete (leadRec);
        Lead_API_Wrapper.LeadCreationRequestWrappper objRequest3 =  (Lead_API_Wrapper.LeadCreationRequestWrappper)JSON.deserialize(req.requestBody.toString(), Lead_API_Wrapper.LeadCreationRequestWrappper.class);         
        LeadManagement.getTempLeadMapping(objRequest3, lstUser[0].id);                                                               
        LeadManagement.getLeadMapping(objRequest3, lstUser[0].id);
        RestRequest req3 = new RestRequest();
        RestResponse res3 = new RestResponse();
        req3.addParameter('userEmail',user1Email);
        req3.addParameter('CompanyName','abcd');
        req3.addParameter('FirstName','Abc');
        req3.addParameter('LastName','xyz');
        req3.addParameter('PrimaryEmail','abc@xyz.com');
        req3.addParameter('PhoneNumber','9999999999');
        req3.addParameter('AddressLine1','ddd');
        req3.addParameter('AddressLine2','ffff');
        req3.addParameter('City','City');
        req3.addParameter('State','State');
        req3.addParameter('ZIPCode','008800');
        req3.addParameter('Country','India');
        req3.addParameter('EstimatedMRR',string.valueOf(EstimatedMRR));
        req3.addParameter('EstimatedCloseDate',string.valueOf(EstimatedCloseDate));
        req3.requestURI = 'services/apexrest/LeadManagement/*';  
        req3.httpMethod = 'POST';
        RestContext.request = req3;
        RestContext.response = res3;
        req3.requestBody = Blob.valueOf(JSON.serialize(req3.params)); 
        Lead_API_Wrapper.LeadCreationResposeWrappper result3 = LeadManagement.doPost();
        Test.stopTest();
    }
Best Regards
Sampath
 
This was selected as the best answer
Tulasiram ChippalaTulasiram Chippala
Thanks sampath its worked. Thank u very much.