Activity Forums Salesforce® Discussions How to write a test class for Attachment, Messaging, Content Version Objects in Salesforce?

  • Piyush

    Member
    November 11, 2019 at 5:46 am

    Hi Prachi,

    To write a test class for Attachment, Messaging, Content Version Objects in salesforce you can take help from the following example:-

    @isTest 
    public class AttClassTest 
    {
        static testMethod void testEx1() 
        {
            Account testAccount = new Account();
            testAccount.Name='Test Account' ;
            insert testAccount;
            
            Contact cont = new Contact ();
            cont.FirstName = 'FirstName';
            cont.LastName = 'LastName';
            cont.Email='[email protected]';
            cont.phone='12345678';
            insert cont;
            
            Account acct = new Account(Name='TEST_ACCT');
            insert acct;
            
            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 = acct.id;
            cdl.ContentDocumentId = documents[0].Id;
            cdl.shareType = 'V';
            insert cdl;
            
            System.Test.StartTest(); 
          <b>  PageReference pageRef = Page.aaaaaaaaaa; // Add your VF page Name here</b>
            pageRef.getParameters().put('Id', String.valueOf(acct.Id));
            System.Test.setCurrentPage(pageRef);
            AttClass cls = new AttClass();
            cls.Body =Blob.valueOf('Test Content') ; 
            cls.Name='Test' ;
            cls.save();
            AttClass.createContentVersion('Test',Blob.valueOf('Test Content'));
            AttClass.createContentLink(contentVersion.Id , acct.Id);
            cls.contentId =null ;
            cls.save();
            System.Test.StopTest();
        }
        
        
    }

    for more take help from https://developer.salesforce.com/forums/?id=9060G0000005ooMQAQ

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos