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
KRISHNAMURTHY KOLUMAM ANANTHARAMAKKRISHNAMURTHY KOLUMAM ANANTHARAMAK 

How do I write a test class for the below code ?

I am trying to send an email with attachment whenever a pdf is uploaded in Notes and attachment object. How do I write a test class ? Kindly help.
 
trigger EmailTrigger on ContentVersion (after insert){
    
    if(Trigger.isAfter){
        
        List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>(); 
        if(Trigger.isInsert){
            
            
            for(ContentVersion cv : trigger.new){ 
                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); 
                attachment.setBody(Blob.valueof(cv.ContentBodyId));
                attachment.setFileName(cv.Title + '.' + cv.FileType); 
                
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); 
                message.toAddresses = new String[] { 'andre.nobre@cleantechsolar.com' }; 
                message.ccAddresses = new String[] {'krishnamurthy.ka@cleantechsolar.com'};
                message.subject = 'Test Mail'; 
                message.plainTextBody = 'Attached file name: ' + cv.Title; 
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment}); 
                
                allMessages.add(message); 
            } 
        }
        Messaging.sendEmail(allMessages); 
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please try test class like below
@isTest 
public class EmailTriggerTest 
{
    static testMethod void testMethod1() 
	{

		ContentVersion testContentInsert =newContentVersion(); 
		testContentInsert.ContentURL='<a target="_blank" href="http://www.google.com/';" rel="nofollow">http://www.google.com/';</a> 
		testContentInsert.Title ='Google.com'; 
		insert testContentInsert; 
		
		ContentVersion testContent = [SELECT ContentDocumentId FROM ContentVersion where Id = :testContentInsert.Id]; 
		
 
    }
}

Let us know if this will help you
 
KRISHNAMURTHY KOLUMAM ANANTHARAMAKKRISHNAMURTHY KOLUMAM ANANTHARAMAK
Hi Amit,

There is an issue in 8 th line. Please check the quotation and can you send me ?

It says unsupported identifier.

see the below pic:
User-added image
Amit Chaudhary 8Amit Chaudhary 8
Please try like below
@isTest 
public class EmailTriggerTest 
{
    static testMethod void testMethod1() 
	{

		ContentVersion testContentInsert =newContentVersion(); 
		testContentInsert.ContentURL='<a target="_blank" href="http://www.google.com/" rel="nofollow">http://www.google.com/</a>'; 
		testContentInsert.Title ='Google.com'; 
		insert testContentInsert; 
		
		ContentVersion testContent = [SELECT ContentDocumentId FROM ContentVersion where Id = :testContentInsert.Id]; 
		
 
    }
}

 
KRISHNAMURTHY KOLUMAM ANANTHARAMAKKRISHNAMURTHY KOLUMAM ANANTHARAMAK
Hi Amit,

I am getting the below error :

User-added image
KRISHNAMURTHY KOLUMAM ANANTHARAMAKKRISHNAMURTHY KOLUMAM ANANTHARAMAK
Hi Amit,

There is something wrong. Could you please  correct and revert and also it covers 0% of the code ?
 
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below

Trigger should be like below
trigger EmailTrigger on ContentVersion (after insert){
    
    if(Trigger.isAfter){
        
        List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>(); 
        if(Trigger.isInsert){
            
            
            for(ContentVersion cv : trigger.new){ 
                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); 
                
                if( Test.isRunningTest() ){
                    attachment.setBody(Blob.valueof('TestBody'));
                }
                else
                {
                    attachment.setBody(Blob.valueof(cv.ContentBodyId));
                }
                attachment.setFileName(cv.Title + '.' + cv.FileType); 
                
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); 
                message.toAddresses = new String[] { 'andre.nobre@cleantechsolar.com' }; 
                message.ccAddresses = new String[] {'krishnamurthy.ka@cleantechsolar.com'};
                message.subject = 'Test Mail'; 
                message.plainTextBody = 'Attached file name: ' + cv.Title; 
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment}); 
                
                allMessages.add(message); 
            } 
        }
        Messaging.sendEmail(allMessages); 
    }
}

Test Class should be like below
@isTest 
public class EmailTriggerTest 
{
    static testMethod void testMethod1() 
    {

        ContentVersion testContentInsert =new ContentVersion(); 
        testContentInsert.ContentURL='<a target="_blank" href="http://www.google.com/" rel="nofollow">http://www.google.com/</a>'; 
        testContentInsert.Title ='Google.com'; 
        insert testContentInsert; 
        
        ContentVersion testContent = [SELECT ContentDocumentId FROM ContentVersion where Id = :testContentInsert.Id]; 
        
 
    }
}

I tested the same in my developer org and got 94% (16/17) code coverage.
Let me know if this will help you