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
venkyyyvenkyyy 

facing problem while writing test class..

Hi, 
Can anybody give me test class for the following class, so that i can learn by each and every line( i am new to test classes).
---------------
public with sharing class acSearch {

    public String acclist { get; set; }
    public String AcPhone { get; set; }
    public String AcName { get; set; }
    public list<account> lst{get;set;}


    public pageReferece Search() {
    list<account> lstt = [select id,name,type,industry,phone from account];
    lst = new list<account>();
    for(account a: lstt){
        
        if(AcName == a.name & AcPhone == a.phone){
        account ac = new account();
        ac.Name = a.name;
        ac.Phone = a.phone;
        lst.add(ac);
        }
    }
        return null;
    }
    
}
--------------------

Thanks in advance.
Abhishek BansalAbhishek Bansal
Hi ,

Please find the code for test class :

private class acSearchTest {
    static testMethod void myUnitTest() {
        Account testAcc = new Account(Name = 'Test Account', Phone = '1234');
        insert testAcc;
        
        Test.startTest();
        acSearch testRef = new acSearch();
        testRef.AcName = testAcc.Name;
        testRef.AcPhone = testAcc.Phone;
        
        testRef.search();
        Test.stopTest();
    }
}


Regards,
Abhishek
salesforce mesalesforce me
Hi Venkey check it once....With 100% code coverage
@isTest(seeAllData=true)
private class acSearch_test{ 

public testmethod static void addTest(){


Account a=new Account();
a.name='ram';
a.phone='8790604868';
insert a;

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

acSearchww e = new acSearchww();

e.AcName ='ram';
e.AcPhone='8790604868';
e.acclist='no use';
e.Search();

system.assertEquals(null,e.Search());

}




}



 
Himanshu ParasharHimanshu Parashar
Hi Venky,

Test class is a way to unit test your apex code prior to deploy on PROD so we write different test methods to test apex code execution.

Prior to write test method we first create prerequisite data which will be used to run test method so we first look whole code for prerequisite data. In above case pre requisite data is from Account object so first of all we need Account object data. 

Step 1 : Create Account object data so that SOQL from apex class can find that data at line 7.
Step 2: As you have logic around name and phone field at line 10 we need to fill those fields as well while creating data.
Step 3: We will now call controller Search method so that test method can execute the code.
 Step4 : Assert the value to validate the execution.

 
@isTest(SeeAllData=false)  //This define where we can see the values from database or not.
private class acSearchTest{

   static testmethod void myTestMethod1() 
  {
     //Step 1
     List<Account> acclist = new List<Account>();
     for(integer index=1; index<=5 ; index++)
     {
         //Step 2
         Account acc = new Account(name='Account' + index,Phone='8989898989');
         acclist.add(acc);
     }
     //insert account list
     insert acclist;
     //create class object. Step 3
      acSearch objacSearch = new acSearch();
      objacSearch.AcName='Account1';
      objacSearch.AcPhone='8989898989';
      objacSearch.Search();

      //Step 4 : assert the value.
      system.assertequals(1,objacSearch.lst.size());
  }


}

Let me know if you need more details.

Thanks,
Himanshu
 
venkyyyvenkyyy
Hi Himanshu,
Thanks for the neat explenation on test class, upto now i learned it perfect by following your explenation, i like the code style too But i am unable to cover the code all the ways like Postitive, Negative, Bulk data and for user asserts etc(not to above test class, for other classes). can you please explain me with a scenario example(simple scenario with if and else if, and for perticuler user,bulk) so that it'll cover all the cases in test, by that i can learn full and full test classes and it'll help for all the others like me(Hope i get reply). 


Thanks in advance,
Venkyy.