-7

I am designing an Apex Class and VF page, but am fairly new to the testing component. I am looking to write some test code, but currently am only achieving 0% coverage. I would like it to be at 75% or more code coverage, which is the deployment threshold.

Here is an example of a visualforce page.

<apex:page controller="ContactLookupControl"
title="Search"
showHeader="false"
sideBar="false"
tabStyle="Contact"
id="pg">
<apex:form >
<apex:outputPanel id="page" layout="block"                                                       
style="margin:5px;padding:10px;padding-top:2px;">
<apex:tabPanel switchType="client" selectedTab="name1" id="tabbedPanel">
<!-- SEARCH TAB -->
  <apex:tab label="Search" name="tab1" id="tabOne"> 
  <apex:actionRegion > 
  <apex:outputPanel id="top" layout="block" style="margin:5px;padding:10px;padding-top:2px;">
    <apex:outputLabel value="Search" style="font-weight:Bold;padding-right:10px;" for="txtSearch"/>
    <apex:inputText id="txtSearch" value="{!searchString}" />
      <span style="padding-left:5px"><apex:commandButton id="btnGo" value="Go" action="{!Search}" rerender="searchResults"></apex:commandButton></span>
  </apex:outputPanel>

   <apex:outputPanel id="pnlSearchResults" style="margin:10px;height:350px;overflow-Y:auto;" layout="block">
    <apex:pageBlock id="searchResults">
      <apex:pageBlockTable value="{!results}" var="a" id="tblResults">
        <apex:column >
          <apex:facet name="header">
            <apex:outputPanel >Name</apex:outputPanel>
          </apex:facet>
           <apex:outputLink value="javascript:top.window.opener.lookupPick2('{!FormTag}','{!TextBox}_lkid','{!TextBox}','{!a.Id}','{!a.Name}', false)" rendered="{!NOT(ISNULL(a.Id))}">{!a.Name}</apex:outputLink>    
        </apex:column>
      </apex:pageBlockTable>
    </apex:pageBlock>
   </apex:outputPanel>
  </apex:actionRegion>
</apex:tab>
<!-- NEW CONTACT TAB -->
<apex:tab label="New Contact" name="tab2" id="tabTwo">
    <apex:pageBlock id="newContact" title="New Contact" >
      <apex:pageBlockButtons >
        <apex:commandButton action="{!saveContact}" value="Save"/>
      </apex:pageBlockButtons>
      <apex:pageMessages />
      <apex:pageBlockSection columns="2">
          <apex:inputField value="{!Contact.FirstName}"/>
          <apex:inputField value="{!Contact.LastName}"/>
          <apex:inputField value="{!Contact.Company_Name__c}"/>
          <apex:inputField value="{!Contact.MailingStreet}"/>
          <apex:inputField value="{!Contact.MailingCity}"/>
          <apex:inputField value="{!Contact.MailingState}"/>
       <apex:inputField value="{!Contact.MailingPostalCode}"/>
       <apex:inputField value="{!Contact.MailingCountry}"/>
       <apex:inputField value="{!Contact.Phone}"/>
       <apex:inputField value="{!Contact.Email}"/>
       <apex:inputField value="{!Contact.Company_Website_URL__c}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>   
</apex:tab>

Additionally here is the controller

public class ContactLookupControl {

  public Contact contact {get;set;} // new contact to create
  public List 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 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');
  }

}

And lastly the test, which much to my silliness, I copied the text again.

@istest
public class ContactLookupControlTest {

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

  public ContactLookupControlTest() {
    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 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');
  }

}

Are there any steps I should take to enable code coverage. We are creating a contact lookup and create, mimicking the dreaded quickcreate button for a particular visualforce page/app we are designing any steps would be helpful.

Many thanks. Sorry for any newbieness.

closed as off-topic by Eric, Sergej Utko, Chris Duncombe, Mohith Shrivastava, crmprogdev Jun 20 '15 at 21:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Eric, Sergej Utko, Chris Duncombe, Mohith Shrivastava, crmprogdev
If this question can be reworded to fit the rules in the help center, please edit the question.

4

Your Testclass is not a test class at all. It is same as your controller. Purpose of testclass is to test the methods in apex to check if they work as expected and desired and they are able to handle any exceptions as well. I am showing a small snippet of a test class/methos,

@isTest

private class HelloWorldTestClass {

    static testMethod void validateHelloWorld() {

       Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100);

       System.debug('Price before inserting new book: ' + b.Price__c);



       // Insert book

       insert b;



       // Retrieve the new book

       b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];

       System.debug('Price after trigger fired: ' + b.Price__c);



       // Test that the trigger correctly updated the price

       System.assertEquals(90, b.Price__c);

    }

}

You can see https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm for more insight into a test class. You must also provide test data for your test class. Please update your test class and run it again. Moreover, using Developer console, you can see which part of your apex code is not covered using your test class and you can work on that as well.

Please revert back in case of any queries/issues you face.

4

The first problem I see is that the methods in your test class aren't test methods. You need to include methods that start off something like this:

@isTest
public static void test1(){
    ContactLookupControl clc = new ContactLookupControl();
    // do testing here
}
  • bad syntax there – Ronnie Jul 10 '18 at 23:14
  • 1
    @Ronnie I see it, I had an extra 'r' in there. – martin Jul 11 '18 at 1:08
  • 1
    i updated for you – Ronnie Jul 11 '18 at 3:10
  • 2
    @Ronnie Putting the @isTest annotation in the method signature is actually valid syntax. Recently though I have been favoring the style of keeping the annotation in the line above. – martin Jul 11 '18 at 3:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.