1

I am getting 88% code coverage when I run the test class but when I am deploying it in production it is giving 63% code coverage

Below is the apex class code

public class UploadeFilesForAuditRequestController {
    @AuraEnabled
    public static String createPdf(Id parentId)
    {
        try
        {
        System.debug('Parent id' +parentId);


     List <Audit_Project__c> auditFields = [select Total_number_of_invalid_efiles_uploaded__c, Request_Type__c, Total_number_of_valid_efiles__c, Total_number_of_efiles_uploaded__c ,   PropertyCase__c, Property_Staff_Name__c, Id from  Audit_Project__c where Id =: parentId   ];
     List<ContentDocument> listToDelete = new List<ContentDocument>();

     if(HelperClass.firstRun)
     {
          List <ContentVersion> contentVersions = [select Id, LastModifiedById, PathonClient, VERSIONDATA , Title, ContentDocumentId from ContentVersion where FIRSTPUBLISHLOCATIONID =:parentId ];

         if(contentVersions != null)
         {

             for(Audit_Project__c auditProcess : auditFields )
             {

             for(ContentVersion cv : contentVersions)
             {
                 if(!cv.Title.contains('Error'))
                 {

                 String fileName;

                 fileName = cv.PathOnClient;
                 String[] fileTitle = fileName.split('');System.debug('Content version ------' + cv.PathOnClient);  



                 if(fileName.contains('_')){
                     Integer lastIndex = fileName.lastIndexOf('_');

               String householdLastName  = fileName.substring(0, lastIndex);
                     String unit = fileName.substring(lastIndex + 1,fileName.lastIndexOf('.'));

                     if(householdLastName != null && unit != null){
                         System.debug('householdlastname = '+householdLastName);
                         System.debug('Unit ='+unit);
                         Case obj = new Case(); 
                         obj.Effective_Date_Certification__c = System.today();
                         obj.Origin = 'Email';
                         obj.PropertyCase__c= auditProcess.PropertyCase__c;
                         obj.Income_Set_Aside__c = 0;

                         obj.Certified_Income__c = 0;
                         obj.Household__c = householdLastName ;
                         obj.Property_Staff_Name__c = auditProcess.Property_Staff_Name__c;
                         obj.Unit__c = unit; 
                         obj.Audit_Request_Name__c = auditProcess.Id;

                         obj.Status = 'Awaiting Assignment';
                         if(auditProcess.Request_Type__c == 'Asset Management Audit'){
                             obj.RecordTypeId = '0124F000000MCiDQAW';    
                             obj.Application_Type__c = 'Asset Management Audit';
                         } else if(auditProcess.Request_Type__c == 'Desk Audit eFile'){
                             obj.RecordTypeId = '0124F000000MCi8QAG';
                             obj.Application_Type__c = 'Desk Audit eFile';
                         }
                         System.debug('file contain household');
                         insert obj;
                        System.debug('object inserted');

                         ContentVersion cvInsert = new ContentVersion();

                         cvInsert.PathOnClient = cv.PathOnClient;

                         cvInsert.Title = cv.Title;
                         cvInsert.FirstPublishLocationId = obj.Id;
                         cvInsert.VersionData = cv.VersionData;
                                                  System.debug('Going to insert in content version');

                         insert cvInsert;
                         System.debug('pdf created');
                         auditProcess.Total_number_of_valid_efiles__c =    auditProcess.Total_number_of_valid_efiles__c + 1; 
                         listToDelete.add(new ContentDocument(
                             Id = cv.ContentDocumentId 
                         ));

                     }
                     else{


                         String nameWithoutExt = fileName;
                         cv.Title =  'ERROR_' +  nameWithoutExt ;

                         update cv;
                         System.debug('Error file added');
                         auditProcess.Total_number_of_invalid_efiles_uploaded__c = auditProcess.Total_number_of_invalid_efiles_uploaded__c +1;


                     }
                 }else{


                     String nameWithoutExt = fileName;
                     cv.Title =  'ERROR_' +  nameWithoutExt ;

                     update cv;
                                              System.debug('Error file added');

                     auditProcess.Total_number_of_invalid_efiles_uploaded__c = auditProcess.Total_number_of_invalid_efiles_uploaded__c +1;


             }
                     }
             }
                   auditProcess.Total_number_of_efiles_uploaded__c = auditProcess.Total_number_of_valid_efiles__c + auditProcess.Total_number_of_invalid_efiles_uploaded__c;
 }
 delete listToDelete; 
         HelperClass.firstRun = false;              
         }
         }
               System.debug('Now going to return true');
        }
        catch(Exception e)
        {
            System.debug('Exception '+e);
        }
     return 'true';
    }
}

Below is the test class code

@IsTest(SeeAllData=true)
public class AuditRequestTestClass {
    @IsTest(SeeAllData=true) 
    public  static void test1(){
        try{


            System.debug('Audit');
            Account ac = new Account();
            ac.Name = 'SMG';
            System.debug('Inside Account');            
            insert ac;

            Contact co = new Contact();
            co.FirstName = 'debby';
            co.LastName = 'jordan';
            co.AccountId = ac.Id;

            insert co;


            Property_records__c ob = new Property_records__c();
            ob.State__c = 'Alabama(AL)';
            ob.Name = 'Oasis on Grand';
            ob.City__c = 'Phoenix';
            ob.Zip__c = '85007';
            ob.Account_Name__c = ac.Id;
            ob.Electric__c = 'Cooking';
            ob.AGE__c = '';
            ob.Unit__c = 89;
            System.debug('Inside Property');
            insert ob;

            Audit_Project__c ap1 = new Audit_Project__c();
            ap1.Status__c = 'Data Request';
            ap1.Request_Type__c = 'Desk Audit eFile';
            ap1.PropertyCase__c = ob.Id ;
            ap1.Total_number_of_attachement__c = 2;

            ap1.Total_number_of_valid_efiles__c = 5;
            ap1.Total_number_of_invalid_efiles_uploaded__c = 2;
            ap1.Total_number_of_efiles_uploaded__c = 6;

            insert ap1;

            ap1.Status__c = 'Data Submited';


            ContentVersion contentVersion = new ContentVersion(
                Title = 'Test_2341_60.pdf',
                PathOnClient = 'Test_2341_60.pdf',
                FIRSTPUBLISHLOCATIONID = ap1.Id,
                VersionData = Blob.valueOf('Test Content')

            );

            insert contentVersion;    

            ContentVersion cv = new ContentVersion(
                Title = 'Test2341.pdf',
                PathOnClient = 'Test2341.pdf',
                FIRSTPUBLISHLOCATIONID = ap1.Id,
                VersionData = Blob.valueOf('Test Content')

            );  

            insert cv;

            Case obj = new Case(); 

            obj.Effective_Date_Certification__c = System.today();
            obj.Origin = 'Email';
            obj.PropertyCase__c= ap1.PropertyCase__c;
            obj.Income_Set_Aside__c = 23;
            obj.House_hold_name__c = '1';
            obj.Bedroom_Size__c = '1 BR';
            obj.Certified_Income__c = 2345;
            obj.Property_Staff_Name__c = ap1.Property_Staff_Name__c;
            obj.Unit__c = '67'; 
            obj.Correction_Counters__c = 0;
            obj.Correction_file_number__c = 0;
            obj.Created_By_Email__c = 'harshit@blucursor.com';
            obj.Household__c = 'test 1234';
            obj.Audit_Request_Name__c = ap1.Id;

            if(ap1.Request_Type__c == 'Asset Management Audit')
            {
                obj.RecordTypeId = '0124F000000MCiDQAW';    
                obj.Application_Type__c = 'Asset Management Audit';
            }
            else if(ap1.Request_Type__c == 'Desk Audit eFile')
            {
                obj.RecordTypeId = '0124F000000MCi8QAG';
                obj.Application_Type__c = 'Desk Audit eFile';
            }



            String temp3 =  UploadeFilesForAuditRequestController.createPdf(ap1.Id);

            Test.startTest();

            obj.Status = 'Awaiting Assignment';
            Test.stopTest();
            System.assertEquals('Desk Audit eFile', ap1.Request_Type__c);


        }
        catch(Exception e){

        }
    }
}

Here I passed desk Audit eFile recordTypeId and Asset Management recordTypeId of Sandbox.

Screenshots Code coverage I am getting when I am running the test class enter image description here

Code coverage I am getting when I am deploying it in production

enter image description here

  • Try to clear test data and rerun your test code. It will give you the exact code coverage count. – Arabinda Dec 23 '19 at 6:28
  • Did you check debug logs while deploying? Is there any error? – rahul gawale Dec 23 '19 at 6:35
  • 3
    Your test uses seeAllData=true, which is usually a red flag. Are you sure the data on the production environment isn't messing up your tests? – rael_kid Dec 23 '19 at 6:50
  • 2
    The other thing that could cause this is the scope of tests being executed. It might be that your direct tests only cover 63% of the code, but usage of this class by other code exercised in other tests might pull it up to 88%. If you are not running all tests on deployment this might explain it. I suspect it will be the use of visible data that is the culprit though. – Phil W Dec 23 '19 at 8:16
3

Issue in your test class

Looking at your code, you are looping through a list of Audit_Project__c so you need at least two records to be able to cover below code

if (fileName.contains('_')) {
    //...
} else {
    //..
}

In your test class add two records for Audit_Project__c

Why fails in production?

Remember there can be different configurations in production from your sandbox, which might not present in your sandbox.

Sometimes, people write validation rules, workflows, process builders and other automation directly in production (I know it's not recommended, and we should always go through sandboxes or whatever deployment process is set in your organization.), or other code changes might be deployed from another sandbox that is not there in the sandbox that you have tested your code. Another reason could be some critical updates activated in production.

  • First, remove the SeeAllData=true from your test class and methods.
  • Second, remove the try/catch block from your test method.

Here are some guidelines to troubleshoot this.

  • Check the debug logs which are generated while deployment that contains debug logs for the test execution. Most of the time you catch the issue here.
  • While checking debug logs run the specific tests which are failing.
  • Try running existing related tests from production, to see if the problem is in existing changes. If yes, you need to fix that as well.
  • Check the labels/custom setting/custom metadata values if any.
  • Make sure you don't have empty catch blocks in your code and test classes. At least a debug logs should be there. If you have an empty catch block, exceptions go unnoticed.
  • Check if all branching statements are being covered. Let's say you have an if block which is a bigger portion in your code, which is not being covered by your test class in production. This don't give you an error but results in low code coverage. So the debug logs are your only friends in these situations.
|improve this answer|||||
  • I have removed the seeAllData= true and try-catch block. I have also checked the log my apex class is running completely and no i am getting 89 % code coverage but when i am pushing it into the production it is giving 60% code coverage. – Vijay Yadav Dec 23 '19 at 9:32
  • did you check the test debug logs in production? – rahul gawale Dec 23 '19 at 9:48
  • I had solved all the problem i am not getting coverage for these second else else{ String nameWithoutExt = fileName; cv.Title = 'ERROR_' + nameWithoutExt ; update cv; System.debug('Error file added'); auditProcess.Total_number_of_invalid_efiles_uploaded__c = auditProcess.Total_number_of_invalid_efiles_uploaded__c +1; } – Vijay Yadav Dec 23 '19 at 12:10
  • please check the comment by Phil W., also have you checked to debug logs? – rahul gawale Dec 23 '19 at 12:13
  • 1
    I have created another testmethod and inside that i had created another Audit_Project__c records. Then i am getting 93% code coverage – Vijay Yadav Dec 23 '19 at 13:56
0

You have 88% code coverage for the UploadeFilesForAuditRequestController class, but the error is saying that you only have 63% code combined code coverage for all of your org's Apex code.

Whenever you deploy a component, Salesforce will run all of the unit tests in your org, even if you are deploying a single class (or even if you are not deploying Apex code at all), and will calculate the combined code coverage for all Apex code, which must exceed 75%.

To fix this, you will need to improve the tests for the other Apex code in triggers and classes which must have poor code coverage, in order to bring up the overall coverage to above 75%.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.