You need to sign in to do that
Don't have an account?
venkyyy
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.
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.
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
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.
Let me know if you need more details.
Thanks,
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.