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
JamesSSJamesSS 

Covering Test Class

I am writing testclass for the belowing code.In that code 'List<List<SObject>> searchList = [Find : account.Name RETURNING Lead (Id, Name, Company, Owner.Name, Title, Phone, email, Status WHERE Status = 'Open' AND Company like :Names) ]; isn''t covering.How to cover this line?Anyone help to cover this?

 

public class TestController {

public Account Testaccount {get;set;}

public TestController() {

String accountId = ApexPages.CurrentPage().getParameters().get('Id');

if (accountId != NULL) {

account = [ SELECT Id, Name FROM Account WHERE Id =: accountId ];

String Names = account.Name+'%';
List<List<SObject>> searchList = [Find : account.Name RETURNING Lead (Id, Name, Company, Owner.Name, Title, Phone, email, Status WHERE Status = 'Open' AND Company like :Names) ];

List<Lead> leadLists = searchList[0];

}
}

}


@isTest(SeeAllData=true)
public class TController{
static testMethod void TController () {

Account acc = new Account(Name = 'Test11');
insert acc;

Lead testLead1 = new Lead (Status = 'Open',LastName = 'Test1',Email = 'gggg@gmail.com',Company = 'Test11-1',Phone = '1234545');
insert testLead1 ;
Lead testLead2 = new Lead(LastName = 'Test111-2', Company = 'Test11-2', Status = 'Open',Phone = '123345456');
insert testLead2;
System.assertEquals(testLead2.LastName , 'Test111-2');
ApexPages.CurrentPage().getParameters().put('Id',acc.Id);
TestController tlc = new TestController();

}
}

Best Answer chosen by Admin (Salesforce Developers) 
admintrmpadmintrmp
Ensure that you clear your code coverage before re-running your tests.

Also, in order to return results from your search query, you will need to set the ID of the result you want:

Test.setFixedSearchResults(new Id[]{ ID_HERE });

This is the only way to get results from a search.

All Answers

asish1989asish1989
Hi
Your code is working fine, You need to check whether you have lead records or not
JamesSSJamesSS

No.Still isn't covering.I have inserted two leads in Test class.It returns 0 rows in Debug.

 

05:23:55.825 (825033000)|SOSL_EXECUTE_BEGIN|[16]|FIND :tmpVar1 RETURNING lead(Id,Name,Company,Owner.Name,Title,Phone,email,Status WHERE (Status = 'Open' and Company like :tmpVar2))
05:23:55.826 (826699000)|SOSL_EXECUTE_END|[16]|Rows:0

asish1989asish1989
You have to insert records in the database,that is in the Lead object , insert some sample records , which should match your filtration criteria
JamesSSJamesSS

How It is possible? I am writing a test class.Can you correct my test class or how to write test class for the belowing query?

 

 

List<List<SObject>> searchList = [Find : account.Name RETURNING Lead (Id, Name, Company, Owner.Name, Title, Phone, email, Status WHERE Status = 'Open' AND Company like :Names) ];

admintrmpadmintrmp
Ensure that you clear your code coverage before re-running your tests.

Also, in order to return results from your search query, you will need to set the ID of the result you want:

Test.setFixedSearchResults(new Id[]{ ID_HERE });

This is the only way to get results from a search.
This was selected as the best answer