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
Pallavi singhPallavi singh 

Test Class for the apex class

Hello,
Need help to write a test class for the apex class.

Thank you in advance

public without sharing class Fileuploadcttrl {
    @AuraEnabled(cacheable=true)
    public static List<ContentVersion> fetchPassportFiles(String recordId){
        List<ContentVersion> documents =new List<ContentVersion>();
        DateTime nowDT=System.now();
        String formatted=nowDT.format('dd_MM_yyyy');
      //  set<id> contentdocIds=new set<id>();
        for(ContentVersion cv : [SELECT Id,ContentDocumentId, Title, CreatedDate, FileType, ContentSize From ContentVersion WHERE FirstPublishLocationId =: recordId]){
                   
            if( cv.Title.contains('Diplomatic Passport') && cv.Title!='')
            {
                cv.Title = cv.title + ' ' + nowDT;
                documents.add(cv);
            }
         //   contentdocIds.add(cv.ContentDocumentId);
        }
     //   updateDocumentTitle(contentdocIds);
        return documents;
    }
//@future
    public static void updateDocumentTitle(set<id> contentdocIds){
        DateTime nowDT=System.now();
        String formatted=nowDT.format('dd_MM_yyyy');
         list<ContentDocument> contDocList=new list<ContentDocument>();
        for (ContentDocument doc:[select id, Title from ContentDocument where id=:contentdocIds]){
                if(doc.Title!=''&& doc.Title!='null'){
                    doc.Title = doc.title + ' ' + nowDT;
                    contDocList.add(doc);    
                }
        }
        update contDocList;
    }

   @AuraEnabled(cacheable=true)
    public static List<ContentVersion> fetchStatusFiles(String recordId){
        List<ContentVersion> documents =new List<ContentVersion>();
        DateTime nowDT=System.now();
        String formatted=nowDT.format('dd_MM_yyyy');
        for(ContentVersion cvs : [SELECT Id, Title, CreatedDate, FileType, ContentSize From ContentVersion WHERE FirstPublishLocationId =: recordId]){
                   
            if( cvs.Title.contains('Status Certificate') && cvs.Title!='')
            {
               cvs.Title = cvs.title + ' ' + nowDT;
                documents.add(cvs);
            }
        }
        return documents;
    }
                     
}
Best Answer chosen by Pallavi singh
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pallavi,

Can you try the below test class,it gives you 100% coverage .
 
@isTest
private class FileuploadcttrlTest {
    @isTest
    static void testFetchPassportFiles() {
        Account testAccount = new Account(Name='Test Account');
        insert testAccount;

        ContentVersion testContentVersion = new ContentVersion();
        testContentVersion.ContentLocation = 'S';
        testContentVersion.PathOnClient = 'testFile.pdf';
        testContentVersion.Title = 'Diplomatic Passport';
        testContentVersion.FirstPublishLocationId = testAccount.Id;
        testContentVersion.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body') ;
        insert testContentVersion;

        List<ContentVersion> result = Fileuploadcttrl.fetchPassportFiles(testAccount.Id);

        System.assertEquals(1, result.size());
        System.assertEquals(testContentVersion.Id, result[0].Id);
    }

    @isTest
    static void testFetchStatusFiles() {
        Account testAccount = new Account(Name='Test Account');
        insert testAccount;

        ContentVersion testContentVersion = new ContentVersion();
        testContentVersion.ContentLocation = 'S';
        testContentVersion.PathOnClient = 'testFile.pdf';
        testContentVersion.Title = 'Status Certificate';
        testContentVersion.FirstPublishLocationId = testAccount.Id;
        testContentVersion.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
        insert testContentVersion;

        List<ContentVersion> result = Fileuploadcttrl.fetchStatusFiles(testAccount.Id);

        System.assertEquals(1, result.size());
        System.assertEquals(testContentVersion.Id, result[0].Id);
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I dont see you are calling updateDocumentTitle future method anywhere from this class. It is called explictly from lightning page?

Thanks,
 
Pallavi singhPallavi singh
Hi ,

Yes this is the correct apex class.

public without sharing class Fileuploadcttrl {
    @AuraEnabled(cacheable=true)
    public static List<ContentVersion> fetchPassportFiles(String recordId){
        List<ContentVersion> documents =new List<ContentVersion>();
        DateTime nowDT=System.now();
        String formatted=nowDT.format('dd_MM_yyyy');
      //  set<id> contentdocIds=new set<id>();
        for(ContentVersion cv : [SELECT Id,ContentDocumentId, Title, CreatedDate, FileType, ContentSize From ContentVersion WHERE FirstPublishLocationId =: recordId]){
                   
            if( cv.Title.contains('Diplomatic Passport') && cv.Title!='')
            {
                cv.Title = cv.title + ' ' + nowDT;
                documents.add(cv);
            }
         //   contentdocIds.add(cv.ContentDocumentId);
        }
     //   updateDocumentTitle(contentdocIds);
        return documents;
    }

   @AuraEnabled(cacheable=true)
    public static List<ContentVersion> fetchStatusFiles(String recordId){
        List<ContentVersion> documents =new List<ContentVersion>();
        DateTime nowDT=System.now();
        String formatted=nowDT.format('dd_MM_yyyy');
        for(ContentVersion cvs : [SELECT Id, Title, CreatedDate, FileType, ContentSize From ContentVersion WHERE FirstPublishLocationId =: recordId]){
                   
            if( cvs.Title.contains('Status Certificate') && cvs.Title!='')
            {
               cvs.Title = cvs.title + ' ' + nowDT;
                documents.add(cvs);
            }
        }
        return documents;
    }
                     
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Pallavi,

Can you try the below test class,it gives you 100% coverage .
 
@isTest
private class FileuploadcttrlTest {
    @isTest
    static void testFetchPassportFiles() {
        Account testAccount = new Account(Name='Test Account');
        insert testAccount;

        ContentVersion testContentVersion = new ContentVersion();
        testContentVersion.ContentLocation = 'S';
        testContentVersion.PathOnClient = 'testFile.pdf';
        testContentVersion.Title = 'Diplomatic Passport';
        testContentVersion.FirstPublishLocationId = testAccount.Id;
        testContentVersion.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body') ;
        insert testContentVersion;

        List<ContentVersion> result = Fileuploadcttrl.fetchPassportFiles(testAccount.Id);

        System.assertEquals(1, result.size());
        System.assertEquals(testContentVersion.Id, result[0].Id);
    }

    @isTest
    static void testFetchStatusFiles() {
        Account testAccount = new Account(Name='Test Account');
        insert testAccount;

        ContentVersion testContentVersion = new ContentVersion();
        testContentVersion.ContentLocation = 'S';
        testContentVersion.PathOnClient = 'testFile.pdf';
        testContentVersion.Title = 'Status Certificate';
        testContentVersion.FirstPublishLocationId = testAccount.Id;
        testContentVersion.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
        insert testContentVersion;

        List<ContentVersion> result = Fileuploadcttrl.fetchStatusFiles(testAccount.Id);

        System.assertEquals(1, result.size());
        System.assertEquals(testContentVersion.Id, result[0].Id);
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
Brett MorrisBrett Morris
Wonderful web site. Plenty of useful information here. I’m sending it to a few buddies ans also sharing in delicious. And of course, thanks for your effort!best coffee maker under $100 (https://coffeebeanshub.com/best-coffee-maker-under-100/)
 
Matt Smith 151Matt Smith 151
We need SF Developer for our Luxury India Tours (https://www.indusbound.com) website