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
MedhanieHabteMedhanieHabte 

Improve Apex Code Coverage

Greetings, I currently only am able to achieve anywhere between 16-34% coverage with my current apex test class and am trying to determine ways to improve my apex class coverage.


Here is an instance of my apex class itself
 

APEX CLASS
public class ContactLookupControl {

  public Contact contact {get;set;} // new contact to create
  public List<Contact> results{get;set;} // search results
  public string searchString{get;set;} // search keyword

  public ContactLookupControl() {
    contact = new Contact();
    // get the current search string
    searchString = System.currentPageReference().getParameters().get('lksrch');
    runSearch();  
  }

  // performs the keyword search
  public PageReference search() {
    runSearch();
    return null;
  }

  // prepare the query and issue the search command
  private void runSearch() {
    // TODO prepare query string for complex serarches & prevent injections
    results = performSearch(searchString);               
  } 

  // run the search and return the records found. 
  private List<Contact> performSearch(string searchString) {

    String soql = 'select id, name from contact';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';
    System.debug(soql);
    return database.query(soql); 

  }

  // save the new contact record
  public PageReference saveContact() {
    insert contact;
    // reset the contact
    contact = new Contact();
    return null;
  }

  // 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');
  }

}


Here's the test class too

TEST CLASS

@isTest 
public class ContactLookupControlTest {
    static testMethod void validateContactLookupControl() {
        Contact c = new Contact();
            c.FirstName = 'Paul';
            c.LastName = 'Reid';
            c.MailingStreet = '205 Hill Street';
            c.MailingCity = 'Los Angeles';
            c.MailingState = 'CA';
            c.MailingPostalCode = '90015';
            c.Email = 'testingout1@aol.com';
            insert c;
    }
}

Are there any additional steps I can pursue to enable it to work accordingly. Let me know and hope it helps.
Best Answer chosen by James Loghry
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. I hope this will help you:-
@isTest 
public class ContactLookupControlTest 
{
    static testMethod void validateContactLookupControl() 
	{
			Contact c = new Contact();
            c.FirstName = 'Paul';
            c.LastName = 'Reid';
            c.MailingStreet = '205 Hill Street';
            c.MailingCity = 'Los Angeles';
            c.MailingState = 'CA';
            c.MailingPostalCode = '90015';
            c.Email = 'testingout1@aol.com';
            insert c;
			
			Test.StartTest(); 
			
				PageReference pageRef = Page.YOURPAGENAME; // Add your VF page Name here
				pageRef.getParameters().put('lksrch', 'Paul');
  
				ContactLookupControl obj = new ContactLookupControl();
				obj.contact.FirstName ='Test';
				obj.contact.LastName ='TestLast';
				obj.saveContact();
				String str = obj.getFormTag();
				
			Test.StopTest();
    }
}

Please check below blog for more information on test classess:-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Pleae mark this as solution if this will help you

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class. I hope this will help you:-
@isTest 
public class ContactLookupControlTest 
{
    static testMethod void validateContactLookupControl() 
	{
			Contact c = new Contact();
            c.FirstName = 'Paul';
            c.LastName = 'Reid';
            c.MailingStreet = '205 Hill Street';
            c.MailingCity = 'Los Angeles';
            c.MailingState = 'CA';
            c.MailingPostalCode = '90015';
            c.Email = 'testingout1@aol.com';
            insert c;
			
			Test.StartTest(); 
			
				PageReference pageRef = Page.YOURPAGENAME; // Add your VF page Name here
				pageRef.getParameters().put('lksrch', 'Paul');
  
				ContactLookupControl obj = new ContactLookupControl();
				obj.contact.FirstName ='Test';
				obj.contact.LastName ='TestLast';
				obj.saveContact();
				String str = obj.getFormTag();
				
			Test.StopTest();
    }
}

Please check below blog for more information on test classess:-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Pleae mark this as solution if this will help you

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
This was selected as the best answer
Medhanie HabteMedhanie Habte
Pefect, I don't know to mark answers as resolved since I'm new to the community, but as a result, I acheived 76% code coverage. Incredible! 
Amit Chaudhary 8Amit Chaudhary 8
You can go to best comment and then move your mouse near to like and dislike button bellow button will be visible to you.
Then you can select best solution:;-
User-added image


 
Medhanie HabteMedhanie Habte
I do not think it is allowing me to do so, as a newbie, nevertheless, I do regard your answer as the best.
James LoghryJames Loghry
Took care of it for you ;-)