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
Gdev20000Gdev20000 

Basic Test class Help

I am not familiar with the test classes.Now i'm starting to learn how to test.

Please can you help me, i get 0% code coverage.

my class:

 

public with sharing class CustomInfluencerLookupController {
 
  public Contact contact {get;set;} 
  public List<Contact> results{get;set;} // search results
  public string searchString{get;set;} // search keyword
     public String accplanid {get;set;}
     public String accid {get;set;}
  public CustomInfluencerLookupController() {
    contact = new Contact();
    // get the current search string
    searchString = System.currentPageReference().getParameters().get('lksrch');
    accplanid = System.currentPageReference().getParameters().get('accplanid');
    
    accid = [SELECT Account__c FROM Account_Plan__c WHERE id = :accplanid].Account__c;
    runSearch();  
  }
 
  // performs the keyword search
  public PageReference search() {
    runSearch();
    return null;
  }
 
  // prepare the query and issue the search command
  public void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);               
  } 
 
  // run the search and return the records found. 
  public List<Contact> performSearch(string searchString) {
 
    String soql = 'SELECT Account.id, name from Contact where Account.id = \'' + accid+ '\'';
    
  //  String soql = 'SELECT Account.id,name from Contact';
    if(searchString != '' && searchString != null)
      soql = soql +  ' AND name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 30';
    System.debug(soql);
    return database.query(soql); 
 
  }
 

  // used by the visualforce page to send the link to the right dom element
  public string getFormTag() {
    return System.currentPageReference().getParameters().get('frm');
  }
 
  // used by the visualforce page to send the link to the right dom element for the text box
  public string getTextBox() {
    return System.currentPageReference().getParameters().get('txt');
  }
 
}

 Test class:

@isTest
private class cilcTest {
    static testMethod void validate() {
    
    // Create a new account object.
    Account testAccount = new Account(Name = 'TestAccount');
    testAccount.Red_Account_End_Date__c=date.today();
    testAccount.Red_Account_Reason__c='reason';
    testAccount.Red_Account__c= true;
    insert testAccount;
  
    
    //CREATE TEST ACCOUNT PLAN
    Account_Plan__c accountplan=new Account_Plan__c ();
    accountplan.account__c=testaccount.id;
    insert accountplan;
    
      //CREATE TEST CONTACT
    Contact contact = new contact(lastname='lname');
    contact.Job_title__c='salesrep';
    insert contact; 
    Contact contact2 = new contact(lastname='lname');
    insert contact2;  
    
  

    Test.startTest();
    
    string searchString='search';
        PageReference pageRef = Page.CustomInfluencerLookup;
        Test.setCurrentPage(pageRef);

        ApexPages.currentPage().getParameters().put('accplanid', accountplan.Id);
        ApexPages.currentPage().getParameters().put('lksrch', searchString);
        CustomInfluencerLookupController controller = new CustomInfluencerLookupController();       
           controller.runSearch();
            controller.getFormTag();
             controller.getTextBox();
        
    Test.stopTest();       
         
        
   }
   }

 

 

 Thank you in advantage for any explanation.

BR

 

Rahul_sgRahul_sg
You need to initilize CustomInfluencerLookupController instead of
MyCustomLookupController in your test method then you can use
controller.runSearch();
controller.runSearch();
controller.getFormTag() ; controller.getTextBox();
in your test method to cover the class
Gdev20000Gdev20000

Thank you very much! I get 100% of code coverage.

I have just one more question:

methods in the class must to be set up all to public?

so you can write in the test class controller.namemethod.

 

 

Before I had a private method

 

 private void runSearch() {

 

when I was using

 

controller.runSearch();

 

I get an error about visibility.

Thanks again.