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
Bob Lee 14Bob Lee 14 

Needed help to write a test class?

public class CustomEmailComponentHandler {
    @AuraEnabled
    public static User getUserInfo() {
        try{
            User UserObj = [SELECT Name,Email 
                            FROM User 
                            WHERE Id = :UserInfo.getUserId()];
            return UserObj;
            
        }catch(Exception e){
            System.debug('Exception ::' + e.getMessage() + '==============Line Number ::' + e.getLineNumber());
        }
        return null;
    }
    
    @AuraEnabled
    public static  List<SearchResultWrapper> getSearchResult(String searchKeyword){
        try{
            String searchkey = '%'+ searchKeyword + '%';
            System.debug('searchkey ::'+searchkey);
            List<Lead> leadList = new List<Lead>();
            List<Contact> contactList = new List<Contact>();
            List<SearchResultWrapper> searchResultWrapperList = new List<SearchResultWrapper>();
            leadList = [SELECT Name,Email FROM Lead WHERE Email != Null AND Name != Null AND (Name Like :searchkey OR Email Like :searchkey) LIMIT 50000];
            System.debug('leadList ::'+leadList);
            contactList =[SELECT Name,Email FROM Contact WHERE Email != Null AND Name != Null AND (Name Like :searchkey OR Email Like :searchkey )LIMIT 4];
            System.debug('contactList ::'+contactList);
            if(leadList.size()>0 || contactList.size()>0){
                for(Lead Obj : leadList){
                    SearchResultWrapper searchResultWrapperObj = new SearchResultWrapper();
                    searchResultWrapperObj.Name = Obj.Name;
                    searchResultWrapperObj.Email = Obj.Email;
                    searchResultWrapperObj.ObjectName = 'Lead';
                    searchResultWrapperList.add(searchResultWrapperObj);
                }
                for(Contact Obj : contactList){
                    SearchResultWrapper searchResultWrapperObj = new SearchResultWrapper();
                    searchResultWrapperObj.Name = Obj.Name;
                    searchResultWrapperObj.Email = Obj.Email;
                    searchResultWrapperObj.ObjectName = 'Contact';
                    searchResultWrapperList.add(searchResultWrapperObj);
                }
                System.debug('searchResultWrapperList'+searchResultWrapperList);
            }
            return searchResultWrapperList;
            
            
        }catch(Exception e){
            System.debug('Exception ::' + e.getMessage() + '-----Line Number::' + e.getLineNumber());
        }
        return null;
    }
    
    @AuraEnabled
    public static String senMail_apex(String sendFrom, List<String> To, List<String> Cc, List<String> Bcc, String Subject, String Body, List<String> Attachments){
        try{
            List<ContentVersion> contentVersionList = new List<ContentVersion>();
            List<Messaging.EmailFileAttachment> attachmentList = new List<Messaging.EmailFileAttachment>();
            System.debug('sendFrom :' + sendFrom);
            Set<Id> documentId = new Set<Id>();
            if(Attachments.size()>0){
                for(String obj :Attachments){
                    documentId.add(obj);
                }  
            }
            if(documentId.size()>0){
                contentVersionList = [SELECT VersionData,Title,FileExtension FROM ContentVersion WHERE ContentDocumentId In :documentId AND IsLatest = true] ; 
            }
            if(contentVersionList.size()>0){
                for(ContentVersion fileobj : contentVersionList){
                    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                    attach.filename = fileobj.Title + '.' + fileobj.FileExtension;
                    attach.body = fileobj.VersionData;
                    attachmentList.add(attach);
                }
            }
            
            
            
            Messaging.SingleEmailMessage sendMail = new Messaging.SingleEmailMessage();
            
            sendMail.setSenderDisplayName(sendFrom);
            sendMail.setToAddresses(To); 
            sendMail.setBccAddresses(Bcc); 
            sendMail.setCcAddresses(Cc); 
            sendMail.setSubject(Subject); 
            sendMail.setHtmlBody(Body);
            sendMail.setFileAttachments(attachmentList);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {sendMail}) ; 
            
            return null;
        }catch(Exception e){
            System.debug('Exception ::' + e.getMessage() + '-----Line Number::' + e.getLineNumber());
        }
        return null;
        
    }
    
    @AuraEnabled
    public static  List<ContentDocument> getFiles(){
        try{
            List<ContentDocument>  contentDocumentList = [SELECT id,ContentSize,ContentModifiedDate,FileExtension,Title,FileType 
                                                          FROM ContentDocument
                                                          LIMIT 50000];
            return contentDocumentList;
            
        }catch(Exception e){
            System.debug('Exception ::' + e.getMessage() + '-----Line Number::' + e.getLineNumber());
        }
        return null;
        
    }
    
    @AuraEnabled
    public static  List<ContentDocument> searchFiles( String searchKeyword){
        try{
            String searchkey = '%'+ searchKeyword + '%';
            List<ContentDocument>  contentDocumentList = [SELECT id,ContentSize,ContentModifiedDate,FileExtension,Title,FileType 
                                                          FROM ContentDocument WHERE
                                                          Title LIKE :searchkey
                                                          LIMIT 50000];
            return contentDocumentList;
            
        }catch(Exception e){
            System.debug('Exception ::' + e.getMessage() + '-----Line Number::' + e.getLineNumber());
        }
        return null;
        
    }
    
    
    public class SearchResultWrapper {
        @AuraEnabled
        public String Name {get;set;}
        @AuraEnabled
        public String Email {get;set;}
        @AuraEnabled
        public String ObjectName {get;set;}
    }
}
Best Answer chosen by Bob Lee 14
Ajay K DubediAjay K Dubedi
Hi Bob,

I have gone through your apex class and I have written the test class for you.  

@isTest
private class TestCustomEmailComponentHandler {
@isTest static void method_test(){
Contact c = new contact(lastname='lee',email='rcm.test@abc.com');
insert c;

Lead leadObj = new Lead();
leadObj.LastName = 'test';
leadObj.Company = 'Test';
leadObj.Status = 'Closed-Converted';
leadObj.Email = 'rcm.test@abc.com';
insert leadObj;

String[] toAddresses = new String[] {UserInfo.getUserEmail()};
String sendFrom = 'Test';
String Body = 'abc';
String subject = 'Test';

ContentVersion contentVersion_1 = new ContentVersion(
Title = 'Penguins',
PathOnClient = 'Penguins',
VersionData = Blob.valueOf('Test Content') );
insert contentVersion_1;
ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];


System.Test.startTest();
User userObj = CustomEmailComponentHandler.getUserInfo();
CustomEmailComponentHandler.getFiles();
List<ContentDocument> cd = CustomEmailComponentHandler.searchFiles('background');
system.assertEquals(true, cd != Null);
CustomEmailComponentHandler.senMail_apex(sendFrom, toAddresses, toAddresses, toAddresses, subject, Body, new String[] {contentVersion_2.ContentDocumentId});
system.assertEquals(true, userObj != Null);
system.assertEquals(true, CustomEmailComponentHandler.getSearchResult('s') != Null);
System.Test.stopTest();
}

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi

All Answers

Dushyant SonwarDushyant Sonwar
Hi Bob,

You can look the below url for creating test data for contentDocument.
https://vijayasankarn.wordpress.com/2018/02/19/salesforce-apex-writing-unit-test-for-contentdocument/
Final Output would be something like this below:
@isTest
public class TestCustomEmailComponentHandler{
    static testMethod void unitTest(){
        CustomEmailComponentHandler.getUserInfo();
        ContentVersion contentVersionInsert = new ContentVersion(
            Title = 'Test',
            PathOnClient = 'Test.jpg',
            VersionData = Blob.valueOf('Test Content Data'),
            IsMajorVersion = true
        );
        insert contentVersionInsert;
        
        Lead leadObj = new Lead();
        leadObj.lastName = 'TestLname';
        leadObj.Company = 'Test Company';
        leadObj.email = 'test@test.com';
        insert leadObj;
        
        Contact conObj = new Contact();
        conObj.lastName = 'TestLname';
        conObj.email = 'test@test.com';
        insert conObj;
        
        CustomEmailComponentHandler.getSearchResult('TestLname');
        CustomEmailComponentHandler.getFiles();
        CustomEmailComponentHandler.searchFiles('Test');
        
        String contDocId = [Select contentDocumentId from ContentVersion where Id = :contentVersionInsert.Id].contentDocumentId;
        
        CustomEmailComponentHandler.senMail_apex('Test' , new list<String>{'test@test.com'} 
                                    , new list<String>{'testcc@test.com'}  , new list<String>{'testcc@test.com'}
                                    , 'Test Subject' , 'Test Body' , new list<String>{contDocId});
    
    }
}

 
Ajay K DubediAjay K Dubedi
Hi Bob,

I have gone through your apex class and I have written the test class for you.  

@isTest
private class TestCustomEmailComponentHandler {
@isTest static void method_test(){
Contact c = new contact(lastname='lee',email='rcm.test@abc.com');
insert c;

Lead leadObj = new Lead();
leadObj.LastName = 'test';
leadObj.Company = 'Test';
leadObj.Status = 'Closed-Converted';
leadObj.Email = 'rcm.test@abc.com';
insert leadObj;

String[] toAddresses = new String[] {UserInfo.getUserEmail()};
String sendFrom = 'Test';
String Body = 'abc';
String subject = 'Test';

ContentVersion contentVersion_1 = new ContentVersion(
Title = 'Penguins',
PathOnClient = 'Penguins',
VersionData = Blob.valueOf('Test Content') );
insert contentVersion_1;
ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];


System.Test.startTest();
User userObj = CustomEmailComponentHandler.getUserInfo();
CustomEmailComponentHandler.getFiles();
List<ContentDocument> cd = CustomEmailComponentHandler.searchFiles('background');
system.assertEquals(true, cd != Null);
CustomEmailComponentHandler.senMail_apex(sendFrom, toAddresses, toAddresses, toAddresses, subject, Body, new String[] {contentVersion_2.ContentDocumentId});
system.assertEquals(true, userObj != Null);
system.assertEquals(true, CustomEmailComponentHandler.getSearchResult('s') != Null);
System.Test.stopTest();
}

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
This was selected as the best answer