+ Start a Discussion
V AnandV Anand 

How to write test case for extention controller?

HI.......

Anyone help me to write test case for below Extension controller ,I am facing problem to define contructor in testmethod.

 

public class quickEmailController{

ApexPages.StandardSetController setCon;


public quickEmailController(ApexPages.StandardSetController controller)
{
setCon = controller;
}

public pageReference doSomething()
{
for ( contact acc : (contact[])setCon.getSelected() )
{
}
return null;
}
}

 

thanks in advance.......

 

 

Best Answer chosen by Admin (Salesforce Developers) 
pujapuja

Contact con = New Contact(

              lastName  = 'test'
);
insert con;

ApexPages.StandardController sc = new ApexPages.StandardController(con);
quickEmailController acct = new quickEmailController(sc);

 

always pass the related object reference in ApexPages.StandardController.

 

Thnaks

All Answers

pujapuja

Contact con = New Contact(

              lastName  = 'test'
);
insert con;

ApexPages.StandardController sc = new ApexPages.StandardController(con);
quickEmailController acct = new quickEmailController(sc);

 

always pass the related object reference in ApexPages.StandardController.

 

Thnaks

This was selected as the best answer
V AnandV Anand

Thanks puja.......

 

Its working great....

asish1989asish1989

Hi 

  I have written a test method . please refer it . code coverage is 100%

 

   public class quickEmailController{

    ApexPages.StandardSetController setCon;


    public quickEmailController(ApexPages.StandardSetController controller)
    {
        setCon = controller;
    }
    
    public pageReference doSomething()
    {
        for ( contact acc : (contact[])setCon.getSelected() )
        {
        }
        return null;
    }
    private static testMethod void testUnas1(){
        Contact con = new Contact(LastName = 'test');
         insert con;
        List<Contact> conlist = new List<Contact>();
        conlist.add(con) ;
        ApexPages.StandardSetController controller = new ApexPages.StandardSetController(conlist);
        quickEmailController test = new quickEmailController(controller);
        test.doSomething();
    }
}

 

Did this post solve your issue If so please mark it solved so that others can get benifits

 

Thanks

asish

V AnandV Anand

Thanks asish...

 

I got good  code coverage ...problem solved.