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
DeptonDepton 

Help with test method for trigger

I need help with the test method of this trigger, can someone please help me with the code, the trigger works but do not know how to create the test methods to deploy it to production:

Thank you!

 

The trigger is:

 

trigger CopyAttachments on MileStone__c(after insert)

{

Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].Opportunity__c];

Attachment[] insertAttList = new Attachment[]{};

 

         for(Attachment a: attList)

         {

               Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].id);

               insertAttList.add(att);

         }

       if(insertAttList.size() > 0)

       {

            insert insertAttList;

       }

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

<div>

Hi Depton,
Here is your test class, I have only added a test method for single record testing, please create testmethod for bulk testing also, with positive and negative cases. Please ask if problem in that. 
 @isTest
private class TestCopyAttachments 
    { 
        //
        // This testMethod tests CopyAttachments triger
        // 
        public static testmethod void testCopyAttachments()
            {
                       
                //Insert a new instance of Oppurtunity Object with test values 
                Opportunity opr = new Opportunity(Name = 'Test' , StageName = 'TestStag' , CloseDate = Date.today());
                insert opr;
                
                //Insert a list of Oppurtunity Object with test values 
                List<Attachment> listAtt = new List<Attachment>();
                
                for(Integer i = 0; i < 5 ; i++)
                    { 
                        Attachment att = new Attachment(Name = 'TestAttachement', ParentId = opr.id , Body = Blob.valueOf('Test Attachment'));
                        listAtt.add(att);
                    }
                
                insert listAtt;
                
                test.startTest();
                
                MileStone__c mileStone = new MileStone__c(Opportunity__c = opr.id);
                insert mileStone;
                List<Attachment> listAttCopied = [Select id From Attachment where ParentId =: mileStone.id];
                system.assertEquals(listAttCopied.size() , listAtt.size());
                
                test.stopTest();
            }
    }
    

</div>

All Answers

dtravellerdtraveller

Have you read this article on writing test methods?

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

Shashikant SharmaShashikant Sharma

<div>

Hi Depton,
Here is your test class, I have only added a test method for single record testing, please create testmethod for bulk testing also, with positive and negative cases. Please ask if problem in that. 
 @isTest
private class TestCopyAttachments 
    { 
        //
        // This testMethod tests CopyAttachments triger
        // 
        public static testmethod void testCopyAttachments()
            {
                       
                //Insert a new instance of Oppurtunity Object with test values 
                Opportunity opr = new Opportunity(Name = 'Test' , StageName = 'TestStag' , CloseDate = Date.today());
                insert opr;
                
                //Insert a list of Oppurtunity Object with test values 
                List<Attachment> listAtt = new List<Attachment>();
                
                for(Integer i = 0; i < 5 ; i++)
                    { 
                        Attachment att = new Attachment(Name = 'TestAttachement', ParentId = opr.id , Body = Blob.valueOf('Test Attachment'));
                        listAtt.add(att);
                    }
                
                insert listAtt;
                
                test.startTest();
                
                MileStone__c mileStone = new MileStone__c(Opportunity__c = opr.id);
                insert mileStone;
                List<Attachment> listAttCopied = [Select id From Attachment where ParentId =: mileStone.id];
                system.assertEquals(listAttCopied.size() , listAtt.size());
                
                test.stopTest();
            }
    }
    

</div>

This was selected as the best answer
Shashikant SharmaShashikant Sharma

Hi Depton,

 

In above test class please add any other required field that you have on MileStone__c or Opportunity objects

 

         Thanks

Shashikant Sharma