+ Start a Discussion
Vishnu7700Vishnu7700 

Test class for controller class

With below test class only 45% coverage is showing.can one provide suggetions to get 80%

Test Class

@isTest
public class AssignmentFourTest{
static testMethod void test(){
AssignmentFour vrb = new AssignmentFour ();
vrb.doSearch();
List<account> gacc = vrb.getAccounts();
account acc = new account(name='test account 32',BillingState = 'UK',Phone = '96589658',Website = 'abc@abc.com');
insert acc;
}
}

 

Controller class

public class AssignmentFour {
   //Declear Variables
   List<Account> accounts{get;set;}
   public String searchString { get; set; }

 public List<Account> getAccounts() {
      return accounts;
   } 
 //Logic to search the account and display in output panel in VF page
 public PageReference doSearch() {
      if(searchString !='' && searchString !=null){
         accounts = new list<account>();
         accounts = [Select Id,Name,BillingState,Phone,Website FROM Account where name =:searchString];
         //System.debug('@@@@@@@@@@searchString'+searchString);
      }
            if(accounts.size()>0 && accounts.size()!= null){
              // System.debug('@@@@@@listAccount.size()'+listAccount.size());
            }
            else{
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));
               }
       return NULL;
 }
}

Best Answer chosen by Admin (Salesforce Developers) 
MellowRenMellowRen

Hi

 

Have your test give your controller something to search and find:

 

@isTest
public class AssignmentFourTest{
    static testMethod void test(){
        account a1 = new account(name='abc test',BillingState = 'UK',Phone = '96589658',Website = 'www.abc.com');
insert a1;

AssignmentFour vrb = new AssignmentFour();

vrb.searchString = 'xyz test';
vrb.doSearch(); //nothing to find

vrb.searchString = 'abc test'; vrb.doSearch(); //should find this
List<account> gacc = vrb.getAccounts();

//what's this for??? account acc = new account(name='test account 32',BillingState = 'UK',Phone = '96589658',Website = 'abc@abc.com'); insert acc; } }

This should get you there.

 

Regards

MellowRen

All Answers

MellowRenMellowRen

Hi

 

Have your test give your controller something to search and find:

 

@isTest
public class AssignmentFourTest{
    static testMethod void test(){
        account a1 = new account(name='abc test',BillingState = 'UK',Phone = '96589658',Website = 'www.abc.com');
insert a1;

AssignmentFour vrb = new AssignmentFour();

vrb.searchString = 'xyz test';
vrb.doSearch(); //nothing to find

vrb.searchString = 'abc test'; vrb.doSearch(); //should find this
List<account> gacc = vrb.getAccounts();

//what's this for??? account acc = new account(name='test account 32',BillingState = 'UK',Phone = '96589658',Website = 'abc@abc.com'); insert acc; } }

This should get you there.

 

Regards

MellowRen

This was selected as the best answer
Vishnu7700Vishnu7700

Thanks alot MellowRen.....For your solution...