+ Start a Discussion
Anju Alexander 5Anju Alexander 5 

Test class for controller.

Hi,

I have written a controller fro my visualforce page. The vf page is created for when an account is selected using the checkbox, an opportunity should be created with the same name as the account. I have written a controller for this purpose. But I get only 69% code coverage for this controller. Can anyone suggest a solution?

My Controller code is:

/*************************************************************************************************
Created By: Anju Alexander
Created Date: 19/8/14
Version: 31.0
Description: Controller class to create opputunity with the same name as the account selected
**************************************************************************************************/


public class opp{
public boolean s;
public List<AccountWrapper> wrapAccountList {get; set;}
/*************************************************************
List that contains all the accounts
**************************************************************/

List<Account> Aaccount=[select name,phone,industry from Account];
public AccountWrapper accountWrapper1=new AccountWrapper();

/*************************************************************
Method to set the instances of the wrapper class'AccountWrapper'
**************************************************************/
public opp()
{
            wrapAccountList = new List<AccountWrapper>();
            for(Account a: Aaccount)
            {
            wrapAccountList.add(new AccountWrapper(a,s));
            }
       
}
/*************************************************************
Method to create opportunity when the accounts are selected
**************************************************************/


public pageReference createOpp()
{

        for(AccountWrapper wrapObj : wrapAccountList) {
            if(wrapObj.selected == true)
            {
                Opportunity opportunity1= new Opportunity();
                String accountName=wrapObj.AccountInstance.name;
                opportunity1.name=accountName;
                opportunity1.closedate=date.today();
                opportunity1.stagename='Prospecting';
                insert opportunity1;
            }
        }
PageReference pg= new PageReference('https://ap1.salesforce.com/home/home.jsp');
pg.setRedirect(true);
return pg;
}
/*************************************************************
Wrapper class to contain Account Instance and selected value
**************************************************************/
public class AccountWrapper
{
        public Boolean selected{get;set;}
        public Account AccountInstance{get;set;}
        public AccountWrapper(Account a,Boolean s)
        {
        AccountInstance=a;
        selected=false;
        }
          public AccountWrapper()
        {
       
        }
}
}
Best Answer chosen by Anju Alexander 5
asish1989asish1989
I dont understand why you have written two methods,it is no required in your case and also no need to insert Opportunity in test class coz you inserting opportunity in the controller,

if you would  use opportunity in the controller without creating fresh opportiniy then you would have to insert in the test class. 

For example in your controller you are using Account that is why you have to insert Account. 
make sense ????

Pagereference will be automaticaly covered, If you are passing parameter of getting some parameter in your controller then you have to use following code..

Apexpages.currentpage().getParametters().put('paramenter name', 'value');

please don't Forget to Mark this as your best answer if it works fine for you

All Answers

Michael VerhovskiMichael Verhovski
It would help if you post your test class. 


/*************************************************************************************************
Created By: Anju Alexander
Created Date: 19/8/14
Version: 31.0
Description: Controller class to create opputunity with the same name as the account selected
**************************************************************************************************/


public class opp{
public boolean s;
public List<AccountWrapper> wrapAccountList {get; set;}
/*************************************************************
List that contains all the accounts
**************************************************************/

List<Account> Aaccount=[select name,phone,industry from Account];
public AccountWrapper accountWrapper1=new AccountWrapper();

/*************************************************************
Method to set the instances of the wrapper class'AccountWrapper'
**************************************************************/
public opp()
{
            wrapAccountList = new List<AccountWrapper>();
            for(Account a: Aaccount)
            {
            wrapAccountList.add(new AccountWrapper(a,s));
            }
       
}
/*************************************************************
Method to create opportunity when the accounts are selected
**************************************************************/


public pageReference createOpp()
{

        for(AccountWrapper wrapObj : wrapAccountList) {
            if(wrapObj.selected == true)
            {
                Opportunity opportunity1= new Opportunity();
                String accountName=wrapObj.AccountInstance.name;
                opportunity1.name=accountName;
                opportunity1.closedate=date.today();
                opportunity1.stagename='Prospecting';
                insert opportunity1;
            }
        }
PageReference pg= new PageReference('https://ap1.salesforce.com/home/home.jsp');
pg.setRedirect(true);
return pg;
}
/*************************************************************
Wrapper class to contain Account Instance and selected value
**************************************************************/
public class AccountWrapper
{
        public Boolean selected{get;set;}
        public Account AccountInstance{get;set;}
        public AccountWrapper(Account a,Boolean s)
        {
        AccountInstance=a;
        selected=false;
        }
          public AccountWrapper()
        {
       
        }
}
}


asish1989asish1989
Hi 

Below is the test class

@isTest
private class TestClassforforum {

    static testMethod void myUnitTest() {
   
        Account account = new Account(name='testAccount');
        insert account;
        opp clsObj = new opp();
        opp.AccountWrapper accWrap = new opp.AccountWrapper(account, true);
        accWrap.selected = true;
        clsObj.wrapAccountList.add(accWrap);
       
        clsObj.createOpp();
        
    }
}

Mark it as best solution if it helps you for other's benift 
Grazitti TeamGrazitti Team
Hi Anju Alexander 5, 

Issue is inside the Parametrized constructor of the AccountWrapper class. 
public AccountWrapper(Account a,Boolean s)
        {

        AccountInstance=a;

        selected=false;

        }

As you are setting the value of seleted = false ,  and in  createOpp Method , yo are comparing if(wrapObj.selected == true) { }
if(wrapObj.selected == true)
            {
                Opportunity opportunity1= new Opportunity();
                String accountName=wrapObj.AccountInstance.name;
                opportunity1.name=accountName;
                opportunity1.closedate=date.today();
                opportunity1.stagename='Prospecting';
                insert opportunity1;
            }
so test method  is not covering this part of code.   

you need to define the Constructor as follows;
public AccountWrapper(Account a,Boolean s)
        {

        AccountInstance=a;

        selected=true;

        }
let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you

Regards,
Grazitti Team
Web: www.grazitti.com

Anju Alexander 5Anju Alexander 5
Thank you asish1989 very much
Anju Alexander 5Anju Alexander 5
Hi,
If I am writing a test class for a visualforce page whose controller returns a page refernce. How should I write and what all things should be kept in mind while writing the test class for pagereference?
Anju Alexander 5Anju Alexander 5
Hi ashish1989,
This is my test class. Can you tell what was the problem?

My test class:
@isTest
public class pdfTest{
static testMethod void test()
{
opp opp1=new opp();
boolean sel=true;

Account a=new Account(name='Account1');
System.assertEquals(sel,true);

opp.AccountWrapper aw=new opp.AccountWrapper(a,sel);

if(sel==true)
{
    Opportunity op=new Opportunity();
    op.name='pdfTest';
    op.closedate=date.today();
    op.stagename='Prospecting';
    insert op;
}

PageReference pg= Page.Practice;
Test.setCurrentPage(pg);
String nextPage = opp1.createOpp().getUrl();
System.assertEquals('https://ap1.salesforce.com/home/home.jsp', nextPage);
}

static testMethod void test1()
{
opp opp1=new opp();
boolean sel=false;

Account a=new Account(name='Account2');
System.assertEquals(sel,false);

opp.AccountWrapper aw=new opp.AccountWrapper(a,sel);

if(sel==false)
{
    Opportunity op=new Opportunity();
    op.name='account2';
    op.closedate=date.today();
    op.stagename='Prospecting';
    insert op;
}


}

}
asish1989asish1989
I dont understand why you have written two methods,it is no required in your case and also no need to insert Opportunity in test class coz you inserting opportunity in the controller,

if you would  use opportunity in the controller without creating fresh opportiniy then you would have to insert in the test class. 

For example in your controller you are using Account that is why you have to insert Account. 
make sense ????

Pagereference will be automaticaly covered, If you are passing parameter of getting some parameter in your controller then you have to use following code..

Apexpages.currentpage().getParametters().put('paramenter name', 'value');

please don't Forget to Mark this as your best answer if it works fine for you
This was selected as the best answer
Anju Alexander 5Anju Alexander 5
Hi ashish1989,

Thanks for explaining the test class.

Have a nice day.

Thanks and Regards,
Anju Alexander.