0

I have written one trigger for a PDF reader and Case creation based on that PDF. Now I am trying to coverage by the Test Class but it is showing only 0% Coverage. Can anyone please help me with the Coverage, Let me know Where I am getting Wrong?
Apex Trigger:-

trigger InitiateProcess on Project__c (before update) {
try{
Project__c Process = Trigger.New[0];
List<ContentDocument> listToDelete = new List<ContentDocument>();
if(Process.Status__c == 'Submitted'){
    if(HelperClass.firstRun)
    {
        if( Trigger.oldMap.get( Process.Id ).Status__c != Trigger.newMap.get( Process.Id ).Status__c ){
            List <ContentVersion> contentVersions = [select Id, LastModifiedById, PathonClient, VERSIONDATA , Title, ContentDocumentId from ContentVersion where FIRSTPUBLISHLOCATIONID =: Process.Id];
            if(contentVersions != null){
                for(ContentVersion cv : contentVersions){
                    String fileName;
                    fileName = cv.PathOnClient;
                    if(fileName.contains('_')){
                        Integer lastIndex = fileName.lastIndexOf('_');

                        String LastName  = fileName.substring(0, lastIndex);
                        String unit = fileName.substring(lastIndex + 1,fileName.lastIndexOf('.'));
                        if(householdLastName != null && unit != null){
                            Case obj = new Case(); 
                            obj.Date_Certification__c = System.today();
                            obj.ProductCase__c= Process.ProductCase__c;
                            obj.Product_Staff_Name__c = 
    Process.Product_Staff_Name__c;
                            obj.Request_Name__c = Process.Id;
                            obj.OwnerId = Process.OwnerId;
                            obj.Status = 'Awaiting';
                            if(Process.Request_Type__c == 'Asset'){
                                obj.RecordTypeId = '0124F221100iDQAW';    
                                obj.Type__c = 'Asset';
                            } else if(Process.Request_Type__c == 'Desk'){
                                obj.RecordTypeId = '0124F000000MCi8QAG';
                                obj.Type__c = 'Desk';
                            }
                            insert obj;
                            ContentVersion cvInsert = new ContentVersion();
                            cvInsert.PathOnClient = cv.PathOnClient;
                            cvInsert.Title = cv.Title;
                            cvInsert.FirstPublishLocationId = obj.Id;
                            cvInsert.VersionData = cv.VersionData;
                            insert cvInsert;
                            listToDelete.add(new ContentDocument(
                                Id = cv.ContentDocumentId 
                            ));    
                        }else{
                            String nameWithoutExt = fileName;
                            cv.Title =  'ERROR_' +  nameWithoutExt ;
                            update cv; 
                        }
                    }else{
                        String nameWithoutExt = fileName;
                        cv.Title =  'ERROR_' +  nameWithoutExt ;
                         update cv;                   
                    }               
                }           
            }
        }
        delete listToDelete; 
        HelperClass.firstRun = false;
    }   
}
}catch(Exception e){    
} 

}

Apex Test Class:-

@IsTest

public class ProjectRequestTestClass{

@IsTest(SeeAllData=false) 
public  static void test1(){
    try{

           Project__c ap = new Project__c();
        ap.Status__c = 'Submitted';
        ap.Request_Type__c = 'Desk ';
        ap.ProductCase__c = 'a0D0b2211moYEAY';
        insert ap;

        Product_records__c ob = new Product_records__c();
        ob.Name = 'Test';
        insert ob;

       Case obj = new Case(); 
        obj.Status = 'Awaiting';
        obj.Effective_Date_Certification__c = System.today();
        obj.ProductCase__c= ap.ProductCase__c; 
        obj.Type__c = 'Desk';
        insert obj;
        Test.startTest();
        obj.Status = 'Awaiting';
        Test.stopTest();
        update obj;  
        ContentVersion contentVersion = new ContentVersion(
            Title = 'Penguins',
            PathOnClient = 'Penguins.jpg',
            VersionData = Blob.valueOf('Test Content'),
            IsMajorVersion = true
        );
        insert contentVersion;    
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

        //create ContentDocumentLink  record 
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = obj.id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.shareType = 'V';
        insert cdl;

        System.assertEquals('Desk', ap.Request_Type__c);

        }
    catch(Exception e){

    }
}

}

Thanks in advance

1

1 Answer 1

1

The problem is that your trigger has only (before update) and in your test you only do insert for the Project and no updates.

Some suggestions for code improvement: a) Move code from trigger to separate handler class. b) You only handling Trigger.new[0] and not all of them (up to 200). Use for loop to iterate over all Trigger.new. c) If you need to handle Updates test for changes in the field i.e. (project.Status == 'value' && Trigger.oldmap.get(project.Id).Status != 'value')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .