+ Start a Discussion
NikiG22NikiG22 

Test Class on a Controller

Hello - I need help adding a test calss for the following controller?Please help!

 

public class AllAttachmentsController {

    public AllAttachmentsController(ApexPages.StandardSetController controller) {

    }

    public List<Attachment> ATT {get;set;}
    
    public AllAttachmentsController (ApexPages.StandardController stdController) {        
        }
    
     public List<String> getAttLinks() {
        Id caseid = ApexPages.currentPage().getParameters().get('Id');
        
        List<String> attLinks = new List<String>();
        String strURL;
        String strResult ;
                
        ATT=[SELECT Id, Name FROM Attachment 
             WHERE parentId = :ApexPages.currentPage().getParameters().get('Id')]; 
        integer j = ATT.size();
        
        for(integer i=0; i<j; i++) {                      
            
            strURL= 'https://' + ApexPages.currentPage().getHeaders().get('Host') 
                + '/servlet/servlet.FileDownload?file='  + ATT[i].Id;    
            attLinks.add(strURL);    
            }
        return attLinks ;
        }        

 }

 Cheers,

Niki

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below class with test method define inside this:

public class AllAttachmentsController {

 

    public AllAttachmentsController(ApexPages.StandardSetController controller) {

 

    }

 

    public List<Attachment> ATT {get;set;}

   

    public AllAttachmentsController (ApexPages.StandardController stdController) {       

        }

   

     public List<String> getAttLinks() {

        Id caseid = ApexPages.currentPage().getParameters().get('Id');

       

        List<String> attLinks = new List<String>();

        String strURL;

        String strResult ;

               

        ATT=[SELECT Id, Name FROM Attachment

             WHERE parentId = :ApexPages.currentPage().getParameters().get('Id')];

        integer j = ATT.size();

        

        for(integer i=0; i<j; i++) {                     

           

            strURL= 'https://' + ApexPages.currentPage().getHeaders().get('Host')

                + '/servlet/servlet.FileDownload?file='  + ATT[i].Id;   

            attLinks.add(strURL);   

            }

        return attLinks ;

        }       

    static testmethod void testshow()

    {

    attachment ac=new attachment();

    list<account> acc=new list<account>();

    account a=new account(name='test');

    acc.add(a);         

    ApexPages.StandardController sc = new ApexPages.standardController(ac);

    ApexPages.StandardSetController sc1 = new ApexPages.standardSetController(acc);

    AllAttachmentsController obj  = new AllAttachmentsController (sc);

    AllAttachmentsController obj1  = new AllAttachmentsController (sc1);

          

    obj.getAttLinks();

    }

 }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

kiranmutturukiranmutturu

try this

 

@isTest
private class TestAllAttachmentsController{
static testMethod void testAllAttachmentsController1() {

//create the instance record i.e. which object u are using as a standardcontroller i.e

insert objAttachment;

ApexPages.StandardController ApptController = new ApexPages.standardController(objAttachment);
AllAttachmentsController objApptView = new AllAttachmentsController(ApptController);
ApexPages.currentPage().getParameters().put('id', objAttachment.id);

objApptView.getAttLinks();
objApptView.getATT();

}
}

BondicloudBondicloud

Hi Niki,

   u write code in the controllr for the test coverage .  

 

  @isTest
    private static void  testAllAttachmentsController (){

          case cs= new case();

          cs.name ='bondi';

           insert cs;

         ApexPages.currentPage().getParameters().put('Id',cs.id);

        ApexPages.StandardController stdController = new ApexPages.StandardController(cs);    
        AccountSummaryController controller2 = new AccountSummaryController(stdController);
          controller2. getAttLinks();

       

 

 

}

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below class with test method define inside this:

public class AllAttachmentsController {

 

    public AllAttachmentsController(ApexPages.StandardSetController controller) {

 

    }

 

    public List<Attachment> ATT {get;set;}

   

    public AllAttachmentsController (ApexPages.StandardController stdController) {       

        }

   

     public List<String> getAttLinks() {

        Id caseid = ApexPages.currentPage().getParameters().get('Id');

       

        List<String> attLinks = new List<String>();

        String strURL;

        String strResult ;

               

        ATT=[SELECT Id, Name FROM Attachment

             WHERE parentId = :ApexPages.currentPage().getParameters().get('Id')];

        integer j = ATT.size();

        

        for(integer i=0; i<j; i++) {                     

           

            strURL= 'https://' + ApexPages.currentPage().getHeaders().get('Host')

                + '/servlet/servlet.FileDownload?file='  + ATT[i].Id;   

            attLinks.add(strURL);   

            }

        return attLinks ;

        }       

    static testmethod void testshow()

    {

    attachment ac=new attachment();

    list<account> acc=new list<account>();

    account a=new account(name='test');

    acc.add(a);         

    ApexPages.StandardController sc = new ApexPages.standardController(ac);

    ApexPages.StandardSetController sc1 = new ApexPages.standardSetController(acc);

    AllAttachmentsController obj  = new AllAttachmentsController (sc);

    AllAttachmentsController obj1  = new AllAttachmentsController (sc1);

          

    obj.getAttLinks();

    }

 }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
NikiG22NikiG22

Thank you all for helping out so quickly.  They all worked!

 

cheers,

Niki