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
Robert AderoRobert Adero 

This is an Apex Test Class, Delete and Modify methods not working. Only Insert new method when running the test.

Below are part of the Test Class Code which fails;
Method Name: deleteAttachments
Pass/Fail: Fail
Error Message: System.NullPointerException: Attempt to de-reference a null object
Stack Trace: Class.AttachmentUtilsTest.deleteAttachments: line 137, column 1

Class: AttachmentUtilsTest
Method: NamemodifyAttachments
Pass/Fail: Fail
Error Message: System.NullPointerException: Attempt to de-reference a null object
Stack Trace: Class.AttachmentUtilsTest.modifyAttachments: line 115, column 1

Please help, thanks.
/**
 * Test class for attachmentUtils and filesUtils classes
*/
@isTest
public class AttachmentUtilsTest{

    static integer NUMBER_OF_FILES = 5;
    
    //this is a test setup method - will be automatically executed before each test method
    @testSetup static void createAgriFiReportandAttachments() {
        
        
        
        //Set up AgriFi Application (record)
        AgriFI_Application__c App =new AgriFI_Application__c();
        //App.Name = 'new AgriFI Reference()';
        //App.LeadBusinessName = 'Weymouth';
        //App.LeadContactSurname = 'Ken';
        insert App;
        
        
        AgriFI_Report__c ast = new AgriFI_Report__c();
        ast.AgriFI_Application__c = App.id;
        
        insert ast;
        
        //create and insert the attachments
        list<attachment> atts = new list<attachment>();
        
        for(integer i=0;i<NUMBER_OF_FILES;i++){
            attachment att = new attachment();
            att.parentId = ast.id;
            att.name = 'a';
            att.body = Blob.valueOf('Attachment Body');
            atts.add(att);
        }
        
        //this should cause the trigger to fire and concatenate attachment names in the Attachment_Name__c field
        //names should be 300 characters long (200 x with spaces in between, plus trailing space)
        insert atts;
        
        //create and insert files
        list<ContentVersion> contentVersions = new list<ContentVersion>();
        
        for(integer i=0;i<NUMBER_OF_FILES;i++){
            String filesString = 'Binary string of the files';

            ContentVersion conVer = new ContentVersion();
            conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
            conVer.PathOnClient = 'file.txt'; // The files name, extension is very important here which will help the file in preview.
            
            conVer.Title = 'c'; // Display name of the files
            conVer.VersionData = EncodingUtil.base64Decode(filesString); // converting your binary string to Blog
            contentVersions.add(conVer);
        
        }
               
        insert contentVersions;
        
        list<ContentDocumentLink> contentDocLinks = new list<ContentDocumentLink>();
        
        for(ContentVersion conVer: [SELECT contentDocumentId from ContentVersion WHERE Title = 'c']){
            //Create ContentDocumentLink
            ContentDocumentLink cDe = new ContentDocumentLink();
            cDe.ContentDocumentId = conVer.contentDocumentId;
            cDe.LinkedEntityId = ast.Id; // you can use objectId,GroupId etc
            cDe.ShareType = 'I'; // Inferred permission, checkout description of ContentDocumentLink object for more details
            cDe.Visibility = 'AllUsers';
            contentDocLinks.add(cDe);
        }
               
        insert contentDocLinks;
        
    }
    
    //test to see if trigger is correctly modifying field on assignment when 200 attachments are added
    public static testmethod void addAttachments(){
    
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM AgriFI_Report__c][0].Attachment_Names__c;
                               
        //Each file name is 1 character long
        //And the 'attachment names' field contains each filename with a space in between
        //So length of Attachment Names should = number of files * 2 characters * 2 (attachments and content) - 1 space
        if(names!=null) system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1,names.length());
        
    }
    
    
    //test to see if trigger is correctly modifying field on assignment when one attachment name is changed
    public static testmethod void modifyAttachments(){
    
        //select an attachment and modify its name
        attachment att = [    SELECT id, name
                              FROM attachment
                              ][0];
        att.name = 'aaaa';
        
        contentVersion cv = [SELECT id, Title from ContentVersion][0];
        cv.Title = 'cccc';
        
        test.startTest();
        update att;
        update cv;
        test.stopTest();
        
        //test that names field has increased by 4
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM AgriFI_Report__c][0].Attachment_Names__c;
                        
        
        //Increases by 6 characters total
        system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1 + 6,names.length());
        
    }
    
    //test to see if trigger is correctly modifying field on assignment when one attachment name is deleted
    public static testmethod void deleteAttachments(){
    
        //select an attachment and delete it
        attachment att = [    SELECT id
                              FROM attachment
                              ][0];
        
        
        test.startTest();
        delete att;
        test.stopTest();
        
        //test that names field is now decreased by 2
        //assert that the attachment name field is correct
        string names = [SELECT Attachment_Names__c 
                        FROM AgriFI_Report__c][0].Attachment_Names__c;
                               
        system.assertEquals(NUMBER_OF_FILES * 2 * 2 - 1 - 2,names.length());
    }
    
}



 
TechingCrewMattTechingCrewMatt
It looks like the Attachment_Names__c field is null in both methods. You can add the following assertion to lines 113 and 136 to verify that.
System.assertNotEquals(null, names, 'The "names" variable should not be null');
Once you verify that, you may need to re-visit the functional code to determine why it's happening. If those assertions pass, then my theory is incorrect.

Thanks,
Matt