You need to sign in to do that
Don't have an account?
siva krishna 61
I have written test class for below class but code percentage i am getting 29 only would you please give solution for it
Test Class
-----------------
@isTest
public class CustSearchTest {
testmethod static void testme()
{
List<Contact> conlist=new List<Contact>();
Contact testcon = new Contact();
testcon.lastname='testcon1' ;
testcon.FirstName= 'Anil';
testcon.Email = 'ram@gmail.com';
testcon.MobilePhone= '9898561236';
conlist.add(testcon);
Contact testcon1 = new Contact();
testcon1.lastname='testcon12' ;
testcon1.FirstName= 'Anil2';
testcon1.Email = 'ram2@gmail.com';
testcon1.MobilePhone= '9898561136';
conlist.add(testcon1);
insert conlist;
test.startTest();
ApexPages.StandardController sc = new ApexPages.StandardController(testcon);
CustSearch cs= new CustSearch(sc);
test.stopTest();
}
}
apex class:
-----------------
public class CustSearch {
public contact con{set;get;}
public list<contact> cont {set;get;}
public CustSearch(Apexpages.StandardController controller){
cont = new list<contact>();
// list<string> str = new list<string>{'FirstName','LastName','Email','AccountId','Account.name', 'Customer_Account_Number__c'};
list<string> str = new list<string>{'FirstName','LastName','Email'};
if (!Test.isRunningTest()){
controller.addFields(str);
con = (contact)controller.getRecord();
}
}
Class
---------------------------
public void search(){
transient String lastname= '%'+con.LastName+'%';
transient string firstname = '%'+con.FirstName+'%';
transient string email1 = '%'+con.Email+'%';
transient id accName = con.Accountid;
//transient String acctno = '%'+con.Customer_Account_Number__c+'%';
transient String mStreet = '%'+con.MailingStreet+'%';
System.debug(accName);
System.debug(con.AccountId);
cont = [select id,firstname,lastname,email,phone,contact.account.name, MailingStreet, MailingCity, MailingPostalCode, MailingState from contact where firstname like : firstname OR lastname like : lastname OR email like : email1 OR MailingStreet like : mStreet ];
if(cont.size()==0)
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, ' No Matching Contact Records Found '));
System.debug(cont.size());
}
}
how to improve code coverage for below class if any give solution for it appriciate
I have written test class for below class but code percentage i am getting 29 only would you please give solution for it
Test Class
-----------------
@isTest
public class CustSearchTest {
testmethod static void testme()
{
List<Contact> conlist=new List<Contact>();
Contact testcon = new Contact();
testcon.lastname='testcon1' ;
testcon.FirstName= 'Anil';
testcon.Email = 'ram@gmail.com';
testcon.MobilePhone= '9898561236';
conlist.add(testcon);
Contact testcon1 = new Contact();
testcon1.lastname='testcon12' ;
testcon1.FirstName= 'Anil2';
testcon1.Email = 'ram2@gmail.com';
testcon1.MobilePhone= '9898561136';
conlist.add(testcon1);
insert conlist;
test.startTest();
ApexPages.StandardController sc = new ApexPages.StandardController(testcon);
CustSearch cs= new CustSearch(sc);
test.stopTest();
}
}
apex class:
-----------------
public class CustSearch {
public contact con{set;get;}
public list<contact> cont {set;get;}
public CustSearch(Apexpages.StandardController controller){
cont = new list<contact>();
// list<string> str = new list<string>{'FirstName','LastName','Email','AccountId','Account.name', 'Customer_Account_Number__c'};
list<string> str = new list<string>{'FirstName','LastName','Email'};
if (!Test.isRunningTest()){
controller.addFields(str);
con = (contact)controller.getRecord();
}
}
Class
---------------------------
public void search(){
transient String lastname= '%'+con.LastName+'%';
transient string firstname = '%'+con.FirstName+'%';
transient string email1 = '%'+con.Email+'%';
transient id accName = con.Accountid;
//transient String acctno = '%'+con.Customer_Account_Number__c+'%';
transient String mStreet = '%'+con.MailingStreet+'%';
System.debug(accName);
System.debug(con.AccountId);
cont = [select id,firstname,lastname,email,phone,contact.account.name, MailingStreet, MailingCity, MailingPostalCode, MailingState from contact where firstname like : firstname OR lastname like : lastname OR email like : email1 OR MailingStreet like : mStreet ];
if(cont.size()==0)
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, ' No Matching Contact Records Found '));
System.debug(cont.size());
}
}
apex class:
-----------------
public class CustSearch {
public contact con{set;get;}
public list<contact> cont {set;get;}
public CustSearch(Apexpages.StandardController controller){
cont = new list<contact>();
// list<string> str = new list<string>{'FirstName','LastName','Email','AccountId','Account.name', 'Customer_Account_Number__c'};
list<string> str = new list<string>{'FirstName','LastName','Email'};
if (!Test.isRunningTest()){
controller.addFields(str);
con = (contact)controller.getRecord();
}
}
public void search(){
transient String lastname= '%'+con.LastName+'%';
transient string firstname = '%'+con.FirstName+'%';
transient string email1 = '%'+con.Email+'%';
transient id accName = con.Accountid;
//transient String acctno = '%'+con.Customer_Account_Number__c+'%';
transient String mStreet = '%'+con.MailingStreet+'%';
System.debug(accName);
System.debug(con.AccountId);
cont = [select id,firstname,lastname,email,phone,contact.account.name, MailingStreet, MailingCity, MailingPostalCode, MailingState from contact where firstname like : firstname OR lastname like : lastname OR email like : email1 OR MailingStreet like : mStreet ];
if(cont.size()==0)
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, ' No Matching Contact Records Found '));
System.debug(cont.size());
}
}