Skip to main content
@isTest
public class TestDeleteAttachment {
    
    @testSetup static void prepData()
    {
        Profile p1 = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];       
        User testUser = new User(FirstName = 'Test', LastName = 'User', ProfileId = p1.Id,Email = '[email protected]', userName='[email protected]',alias='utest',
                                timezonesidkey='America/Chicago',localesidkey='en_US',emailencodingkey='UTF-8',languagelocalekey='en_US');
        insert testUser;
        
        //TO-DO: Create another user with a different profile in your org
        
        //Create a dummy record
        Account a1 = New Account (Name='ACME');
        insert a1;
        //Create Attachment
        Attachment attach = New Attachment();
        attach.Name = 'Test Attachment';       
        Blob bodyBlob=Blob.valueOf('Dummy Body');
        attach.body=bodyBlob;
        attach.parentId=a1.id;
        insert attach;
        
    }
    static testMethod void testDeleteAttach1()
    {
        
        //First fetch the user you created in test setup
        User u = [select Id from User where Username = '[email protected]'];
        Attachment attach = [Select Id from Attachment where Name = 'Test Attachment' LIMIT 1];
        
        System.runas(u)
        {
            //Delete the attachment to test the trigger
            Test.startTest();
            delete attach;
            Test.stopTest(); 
    }
    }
           }
    
}
static testMethod void testDeleteAttach2()
    {
        
        //To-DO: Use the 2nd user to test whether your trigger prevents the deletion as expected
        //Also add methods to test the method 'isAllowed'
    
           }
    
}

}

@isTest
public class TestDeleteAttachment {

@testSetup static void prepData()
{
    Profile p1 = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];       
    User testUser = new User(FirstName = 'Test', LastName = 'User', ProfileId = p1.Id,Email = '[email protected]', userName='[email protected]',alias='utest',
                            timezonesidkey='America/Chicago',localesidkey='en_US',emailencodingkey='UTF-8',languagelocalekey='en_US');
    insert testUser;
    
    //TO-DO: Create another user with a different profile in your org
    
    //Create a dummy record
    Account a1 = New Account (Name='ACME');
    insert a1;
    //Create Attachment
    Attachment attach = New Attachment();
    attach.Name = 'Test Attachment';       
    Blob bodyBlob=Blob.valueOf('Dummy Body');
    attach.body=bodyBlob;
    attach.parentId=a1.id;
    insert attach;
    
}
static testMethod void testDeleteAttach1()
{
    
    //First fetch the user you created in test setup
    User u = [select Id from User where Username = '[email protected]'];
    Attachment attach = [Select Id from Attachment where Name = 'Test Attachment' LIMIT 1];
    
    System.runas(u)
    {
        //Delete the attachment to test the trigger
        Test.startTest();
        delete attach;
        Test.stopTest(); 
    }
    
           
    
}
static testMethod void testDeleteAttach2()
{
    
    //To-DO: Use the 2nd user to test whether your trigger prevents the deletion as expected
    //Also add methods to test the method 'isAllowed'
    
           
    
}

}

@isTest
public class TestDeleteAttachment {
    
    @testSetup static void prepData()
    {
        Profile p1 = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];       
        User testUser = new User(FirstName = 'Test', LastName = 'User', ProfileId = p1.Id,Email = '[email protected]', userName='[email protected]',alias='utest',
                                timezonesidkey='America/Chicago',localesidkey='en_US',emailencodingkey='UTF-8',languagelocalekey='en_US');
        insert testUser;
        
        //TO-DO: Create another user with a different profile in your org
        
        //Create a dummy record
        Account a1 = New Account (Name='ACME');
        insert a1;
        //Create Attachment
        Attachment attach = New Attachment();
        attach.Name = 'Test Attachment';       
        Blob bodyBlob=Blob.valueOf('Dummy Body');
        attach.body=bodyBlob;
        attach.parentId=a1.id;
        insert attach;
        
    }
    static testMethod void testDeleteAttach1()
    {
        
        //First fetch the user you created in test setup
        User u = [select Id from User where Username = '[email protected]'];
        Attachment attach = [Select Id from Attachment where Name = 'Test Attachment' LIMIT 1];
        
        System.runas(u)
        {
            //Delete the attachment to test the trigger
            Test.startTest();
            delete attach;
            Test.stopTest(); 
        }
    }
    static testMethod void testDeleteAttach2()
    {
        
        //To-DO: Use the 2nd user to test whether your trigger prevents the deletion as expected
        //Also add methods to test the method 'isAllowed'
    }
    
}
Source Link
Richard N
  • 3.8k
  • 6
  • 38
  • 54

As Adrian said, you would need to put some effort towards your question and actually tell us what you have done and what you need help with.

But seeing that you are new and the fact that your handle could suggest your enthusiasm towards learning APEX :), I have gone ahead and written a little test code for you.

This should help you with regards to the unit test and you should complete the To-Do sections. In a nutshell, your APEX unit test code must:

    1. Provide code coverage (Salesforce requires 75% but you should strive for 100%).
    2. Test whether your code works as intended (Positive/Negative conditions, testing profile access etc).
@isTest
public class TestDeleteAttachment {

@testSetup static void prepData()
{
    Profile p1 = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];       
    User testUser = new User(FirstName = 'Test', LastName = 'User', ProfileId = p1.Id,Email = '[email protected]', userName='[email protected]',alias='utest',
                            timezonesidkey='America/Chicago',localesidkey='en_US',emailencodingkey='UTF-8',languagelocalekey='en_US');
    insert testUser;
    
    //TO-DO: Create another user with a different profile in your org
    
    //Create a dummy record
    Account a1 = New Account (Name='ACME');
    insert a1;
    //Create Attachment
    Attachment attach = New Attachment();
    attach.Name = 'Test Attachment';       
    Blob bodyBlob=Blob.valueOf('Dummy Body');
    attach.body=bodyBlob;
    attach.parentId=a1.id;
    insert attach;
    
}
static testMethod void testDeleteAttach1()
{
    
    //First fetch the user you created in test setup
    User u = [select Id from User where Username = '[email protected]'];
    Attachment attach = [Select Id from Attachment where Name = 'Test Attachment' LIMIT 1];
    
    System.runas(u)
    {
        //Delete the attachment to test the trigger
        Test.startTest();
        delete attach;
        Test.stopTest(); 
    }
    
           
    
}
static testMethod void testDeleteAttach2()
{
    
    //To-DO: Use the 2nd user to test whether your trigger prevents the deletion as expected
    //Also add methods to test the method 'isAllowed'
    
           
    
}

}