You need to sign in to do that
Don't have an account?
Tulasiram 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:
Test class:
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(); } }
Sampath
All Answers
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
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,
Best Regards
Sampath
Sampath