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
mavsmavs 

Help with Test Method

Hello

 

By looking at the blogs and discussion boards i got the logic to work.

 

And i've spent quite some time already and cannot getting code coverage more than 68%. To deploy this to production i should have 75% coverage.

 

Can someone please help with the test methods for the below code?..Thanks

public class Account_Controller { Id AcctId = ApexPages.currentPage().getparameters().get('id'); Public Boolean errormsg; private String searchText; public string AName; Public Account acctName; public Boolean geterrormsg(){ return errormsg;} public List<AccountResult> AccountResults{get; set;} public List<AccountResult> getAccResult() { return AccountResults; } public string getSearchText() { return AName; } public void setSearchText (String searchText) { this.searchText = searchText; } public PageReference continue1() { String MstAcctid; String DupeAcctid; if(AccountResults!=null) { for(AccountResult AResult : getAccResult()) { if(AResult.master_selected == true) { MstAcctid=AResult.id; } if(AResult.duplicate_selected == true) { DupeAcctid=AResult.id; } } } System.debug('Mst Acct '+MstAcctid); System.debug('Duplicate Account '+DupeAcctid); if(MstAcctid==DupeAcctid) { errormsg=true; return null; } else { errormsg=false; PageReference Duplicate_step2Page = new PageReference('/Apex/Account_page2?MastId='+MstAcctid+'&DupeId='+DupeAcctid); Duplicate_step2Page.setRedirect(true); return Duplicate_step2Page; } } public PageReference init() { acctName=[Select Name from Account where id=:AcctId]; AName=acctName.Name; return null; } public List<Account> search1() { AccountResults= new List<AccountResult>(); for (Account account : [select Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name from Account where Name like: searchText+'%']) { AccountResults.add(new AccountResult(account)); } return null; } public Class AccountResult { public Account Acc { get; set; } public Boolean selected { get;set;} public string id {get;set;} public string Name {get;set;} public Boolean master_selected { get; set; } public Boolean duplicate_selected { get; set; } public AccountResult(Account a) { Acc=a; id=a.id; Name=a.Name; selected=false; } } public void SelectAccount() { for (AccountResult acc : AccountResults) { if (acc.master_selected) { if(acc.duplicate_selected) { } } } } }

 

public static testMethod void results() { Account testAccount =new Account(Name='Sample2',RecordTypeid='012300000000RFTD',NumberOfEmployees=10,ownerid='00540000000TRFG); insert testAccount ; ApexPages.currentPage().getParameters().put('id', testAccount.id ); Account_Controller test=new Account_Controller(); test.init(); test.setSearchText('Tree'); String searchText = test.getSearchText(); test.getSearchText(); test.search1(); test.continue1(); }

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard
You probably want to add some asserts in too, so that you can check you are getting back the test data that you have defined - that way you'd get some clues to why all the code isn't being exercised as anticipated.

All Answers

thangasan@yahoothangasan@yahoo

Hai

 

   1)  In testmethod sample record give Name=Tree' ( Because you are Searching Tree not Sample2)

   2)  Remove Unused SelectAccount() method.

   3)  Use master_selected  duplicate_selected in AccountResult Result Constructor.

 

Regards

Thanga

Kirtesh_JainKirtesh_Jain
I am agree with Thanga . Please follow steps , then Look at code coverage.
mavsmavs

Thanks to both of you.

 

I tried the suggestions and got the code coverage of 70%. But still i need 5% more  :smileysad:

 

Any more ideas????

bob_buzzardbob_buzzard

I think the problem is the way that you are carrying out the search:

 

for (Account account : [select Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name from Account where Name like: searchText+'%'])

 

as you are binding the search text in as a variable, you can't just append '%' to it.

 

Try the following:

 

 

String wildcardSearchText=searchText + '%'; for (Account account : [select Id, Name, Type, BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name from Account where Name like: wildcardSearchText])

 

bob_buzzardbob_buzzard
You probably want to add some asserts in too, so that you can check you are getting back the test data that you have defined - that way you'd get some clues to why all the code isn't being exercised as anticipated.
This was selected as the best answer