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
sabeerasabeera 

Please help me to write test classes for the following trigger

Hi,

I tried some code, but it is not working.

/Trigger to Count the Number of Attachments on an Object

trigger countattachment on Attachment (after insert, after update, after delete, after undelete) {
  Map<Id,List<Attachment>> parent = new Map<Id,List<Attachment>>();
  set<id> attids = new set<id>();
     
   if(Trigger.new<>null){
       for(Attachment c:Trigger.new){
           MDF__c l;
           if(c.ParentId != null)
               attids.add(c.parentid);
       }
           
   }else if(Trigger.old != null){
       for(Attachment c:Trigger.old){
           if(c.ParentId<>null)      
               attids.add(Trigger.oldMap.get(c.id).parentid);
       }
   }
   if(attids.size()>0){
       try{
           List<Attachment> a = new List<Attachment>();
           Map<id,MDF__c> testmap = new Map<id,MDF__c>([select id,CountAttachment__c from MDF__c where id IN: attids]);
           a = [select id,parentid from Attachment where parentid IN:attids];
           
           for(Attachment at: a){
               List<Attachment> llist = new List<Attachment>();
               if(parent.get(at.parentid) == null){
                   llist = new List<Attachment>();
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }else if(parent.get(at.parentid) != null){
                   llist = new List<Attachment>();
                   llist = parent.get(at.parentid);
                   llist.add(at);
                   parent.put(at.parentid,llist);
               }
           }
           
           for(Id i: attids){
               if(testmap.get(i) != null && parent.get(i) != null){
                  testmap.get(i).CountAttachment__c = parent.get(i).size(); 
               
               }else if(testmap.get(i) != null && parent.get(i) == null){
                  testmap.get(i).CountAttachment__c = 0; 
               }
           }
       
           update testmap.values();
           System.Debug(testmap.values());
       }catch(Exception e){}
    }

}
Best Answer chosen by sabeera
Harish RamachandruniHarish Ramachandruni
Hi,


Replace this code and check .Any issue ask me directly .




 
@isTest
public class countattachmenttest{
    Private Static testmethod void countattachmenttest(){ 
        MDF__C m = new MDF__C();
        m.Name='Test';
        m.Amount__c=500;
        insert m;


        Attachment attach=new Attachment(); 
        attach.Name='Unit Test Attachment'; 
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
        attach.body=bodyBlob; 
        attach.parentId=m.id;
        insert attach; 



Attachment attach1 =new Attachment(); 
        attach1.Name = 'Unit Test Attachment'; 
        Blob bodyBlob1 = Blob.valueOf('Unit Test Attachment Body'); 
        attach1.body = bodyBlob; 
        attach1.parentId=m.id;
        insert attach1; 



   Update  attach; 

upadte attach1;


Delete attach;  

Delete  attach1; // if you are not able to get 75 add undelete 



      


      //  List<Attachment> attachments=[select id, name from Attachment where parent.id=:m.id]; 
        System.assertEquals(1, attachments.size()); 
       }  
}

Regards,
Harish.R.

All Answers

Harish RamachandruniHarish Ramachandruni
Hi ,


I am writen in org it's getting 100% check it any issue let me know  . 




User-added image


 





01@isTest 
02Public  class harishraoTeat{
03  
04  
05   Public  static testMethod void harish1() {
06    
07    
08   string emailid = 'harish.rao.salesforce@gmail.com';
09    
10  string  posswd = '9676930011';
11  
12  
13apexpages.currentpage().getparameters().put('Email' , emailid );
14 
15apexpages.currentpage().getparameters().put('pwd' , posswd );
16 
17 
18apexpages.currentpage().getparameters().put('Email' , emailid );
19 
20 
21 
22  
23 CustomLogin login = new CustomLogin();
24  
25  
26 login.getEmail();
27  
28  login.getpwd();
29 
30   login.login();
31 
32  
33  
34  
35    
36       
37    }
38}


If it is usefull make it best answer .
 
sabeerasabeera
Thank u Harish.


@isTest
public class CustomLoginTest
{
    public static testMethod void loginPass()
    {
        CustomLogin c = new CustomLogin();
        String Email='test@yopmail.com';
        String pwd='12345';
        c.getEmail();
        c.getpwd();
        c.login();
            
    }
}

It is also shows 100% is it fine? or can i use ur code?
sabeerasabeera
Please help me  to write test class for above trigger

The below code is cover only 64%  only.

@isTest
public class countattachmenttest{
    Private Static testmethod void countattachmenttest(){ 
        MDF__C m = new MDF__C();
        m.Name='Test';
        m.Amount__c=500;
        insert m;
        Attachment attach=new Attachment(); 
        attach.Name='Unit Test Attachment'; 
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
        attach.body=bodyBlob; 
        attach.parentId=m.id;
        insert attach;
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:m.id]; 
        System.assertEquals(1, attachments.size()); 
       }  
}
Harish RamachandruniHarish Ramachandruni
Hi,


Replace this code and check .Any issue ask me directly .




 
@isTest
public class countattachmenttest{
    Private Static testmethod void countattachmenttest(){ 
        MDF__C m = new MDF__C();
        m.Name='Test';
        m.Amount__c=500;
        insert m;


        Attachment attach=new Attachment(); 
        attach.Name='Unit Test Attachment'; 
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); 
        attach.body=bodyBlob; 
        attach.parentId=m.id;
        insert attach; 



Attachment attach1 =new Attachment(); 
        attach1.Name = 'Unit Test Attachment'; 
        Blob bodyBlob1 = Blob.valueOf('Unit Test Attachment Body'); 
        attach1.body = bodyBlob; 
        attach1.parentId=m.id;
        insert attach1; 



   Update  attach; 

upadte attach1;


Delete attach;  

Delete  attach1; // if you are not able to get 75 add undelete 



      


      //  List<Attachment> attachments=[select id, name from Attachment where parent.id=:m.id]; 
        System.assertEquals(1, attachments.size()); 
       }  
}

Regards,
Harish.R.
This was selected as the best answer
Harish RamachandruniHarish Ramachandruni
Hi,

Let Me know it's working or not .


Regards,
Harish.R.
sabeerasabeera
Thanks a lot Harish!
Its working good and Code coverage 96%.

Your code is not covered  all lines except below line
catch(Exception e){}
sabeerasabeera
Sorry! Your code is covered all lines except below line
catch(Exception e){}
Harish RamachandruniHarish Ramachandruni
Okk 

Write any exeption in test class like .if any error get exeption also cover .

Any query ask dirctly Harish.rao.salesforce@gmail.com

Regards,
Harish.R.