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
NJDeveloper019NJDeveloper019 

At least 75% test coverage is required

I have been building an advanced Search visualforce application for weeks now using Force.com IDE in eclipse.  The originally did it on the page but our SF account was upgraded to production and I was no longer able to access the APEX code on the page.  So I have been deploying to server everytime I made a change and wanted to do testing on our SF instance.  Just today I tried to deploy after some changes and got the error message,

 

" Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required".

 

This has never appeared before and I am stumped as to why it just appeared suddenly and it not allowing me to deploy my code anymore.  Did something change recently?  I have never written any tests for this and I am not sure even how to write tests in Apex.

aalbertaalbert

Were you developing your Apex code in a trial org that was recently activated? When a trial org gets activated, the Apex code in that environment gets disabled and you must "deploy" the code to re-activate it or add new apex code to that environment. You won't be able to develop code in a production instance. Instead, you need to develop your Apex code in a Sandbox and "deploy" the code to Production. And this requires writing test methods that cover at least 75% of the apex code. Once you have the proper test methods in your Sandbox environment, you can use the Force.com IDE to deploy the code to your Production instance. 

 

More information about test methods here.

 

 

NJDeveloper019NJDeveloper019
That is what happened.  When our trial account was upgraded I was forced to use the Force.com IDE.  But I've been using it and using the "deploy to server" option to deploy new code to our now upgraded production SF environment.  And it just suddenly will not allow me to deploy anymore because of the test coverage.  I am wondering why this started happening all of a sudden.
QuickDEVQuickDEV

It is a must to prove atleast 75% of your code is working as expected to be able to deploy to production.

 

 

NJDeveloper019NJDeveloper019
Is it possible to write a test for each method that just returns true so I am covered?
gv007gv007
Post the code it will help us to track it,
BA_Kid_LondonBA_Kid_London

am not a Salesforce Apex developer but I have managed to implement the apex pages and classes for Advanced opportunity search on our SandBox environment and it is working fine but when I try to upload to the live environment using Change Set of Salesforce, then it is not going through and producing following message.

 

“Average test coverage across all Apex Classes and Triggers is 65%, at least 75% test coverage is required.”

 

I have done some research on Salesforce community forum about this but could not fix this. Can you please help us to upload this to the live environment with maximum test coverage? I do not know how to write the tests for the apex classes. Could you please help me write this for the following classes.

 

I got the coding from Jeff Douglas website blog and then customised according to my environment.

http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/

 

here is my apex class

 

public with sharing class CustomSearchController {
 
  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of contacts to display
  public List<Opportunity> opportunities {get;set;}
 
  // the current sort direction. defaults to asc
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }
 
  // the current field to sort by. defaults to last name
  public String sortField {
    get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
    set;
  }
 
  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 100'; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public CustomSearchController() {
    soql = 'select id, name, cohort__c, Component_Module__c, account.name, Start_of_Programme__c, End_of_Programme__c from opportunity where account.name != null';
    runQuery();
  }
 
  // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
  }
 
  // runs the actual query
  public void runQuery() {
 
    try {
      opportunities = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 100');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
 
    String opportunityName = Apexpages.currentPage().getParameters().get('opportunityName');
    String cohort = Apexpages.currentPage().getParameters().get('cohort');
    String accountName = Apexpages.currentPage().getParameters().get('accountName');
 
    soql = 'select id, name, cohort__c, Component_Module__c, account.name, Start_of_Programme__c, End_of_Programme__c from opportunity where account.name != null';

    if (!opportunityName.equals(''))
      soql += ' and name LIKE \'%'+String.escapeSingleQuotes(opportunityName)+'%\'';

    if (!cohort.equals(''))
      soql += ' and cohort__c LIKE \''+String.escapeSingleQuotes(cohort)+'\'';

    if (!accountName.equals(''))
      soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\'';  
 
    // run the query again
    runQuery();
 
    return null;
  }
 
}

 

and my apex page

 

<apex:page controller="CustomSearchController" sidebar="false">
 
  <apex:form >
  <apex:pageMessages id="errors" />
 
  <apex:pageBlock title="Search Opportunities" mode="edit">
 
  <table width="100%" border="0">
  <tr>  
    <td width="200" valign="top">
 
      <apex:pageBlock title="Parameters" mode="edit" id="criteria">
 
      <script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("opportunityName").value,
          document.getElementById("cohort").value,
          document.getElementById("accountName").value,
          ''
          );
      }
      </script> 
 
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="opportunityName" value="" />
          <apex:param name="cohort" value="" />
          <apex:param name="accountName" value="" />
      </apex:actionFunction>
 
      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">Opportunity Name<br/>
        <input type="text" id="opportunityName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Cohort<br/>
        <input type="text" id="cohort" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Client<br/>
        <input type="text" id="accountName" onkeyup="doSearch();"/>
        </td>
      </tr>
      </table>
 
      </apex:pageBlock>
 
    </td>
    <td valign="top">
 
    <apex:pageBlock mode="edit" id="results">
 
        <apex:pageBlockTable value="{!opportunities}" var="opportunity">
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Opportunity Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="opportunityName" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputLink value="/{!opportunity.Id}" id="opportunity_id">
                    <apex:outputField value="{!opportunity.name}"/>
                </apex:outputLink>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Cohort" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="cohort" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!opportunity.Cohort__c}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Module/Component" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Component_Module" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!opportunity.Component_Module__c}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Client" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="account.name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!opportunity.account.name}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Programme Start Date" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="programme_start_date" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!opportunity.Start_of_Programme__c}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Programme End Date" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="programme_end_date" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!opportunity.End_of_Programme__c}"/>
            </apex:column>
 
 
        </apex:pageBlockTable>
 
    </apex:pageBlock>
 
    </td>
  </tr>
  </table>
 
 
  </apex:pageBlock>
 
  </apex:form>
 
</apex:page>