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
nathanxlanenathanxlane 

Test coverage and fixing APEX code.

Hi,

 

I really don't understand the deployment process of salesforce. I'm trying to implement/deploy my first CONTROLLER. While it works in the Sandbox and when I run it with it's associated PAGE, it fails the deployment debug test each time.

 

I deleted the MyProfileController class which was first giving me a hard time, now I have test coverage of 73% out of 75%- what can i do in the code to increase this? Can one bypass this debug/testing process?

 

My controller (A very, very simple search - working)

 

public class ContactController {

        public String getSearchText() {
             return null;       
        }

   private List<Contact> result = new List<Contact>();
   private String searchText;

   public ContactController(){
   }

   public List<Contact> getResult() {return result;}

   public void setSearchText(String searchText) 
        { this.searchText = searchText; }

   public void search() { 
  String queryText = searchText + '%';
  result = [select Name, group__c, pre_commit__c, consent__c, Phone from Contact Where Name like :queryText ];}
}

 

 

Cory CowgillCory Cowgill

1. No, you cannot bypass the testing / deployment process. Because Salesforce is a multi-tenant architecture, your code is running on the same CPU as other applications. The testing/deployment process helps prevents users from deploying code which could cause erratic behavior for other users or hog resources.

 

2. To increase your code coverage, you will need to configure your test data to hit the lines of code which are not being covered. When you run your Unit Tests, you should be able to view the code coverage report either in the IDE or browser, and it will tell you what lines of codes have not been tested. Modify your test input to hit those lines of code.

 

For example, to test your simploe controller provided at 100%, use this Apex Class:

 

@isTest
private class ContactControllerTest
{
    static testMethod void testContactSearch()
    {
     //Insert your Test Data
     Contact testContact = new Contact();
     testContact.FirstName = 'UnitTestXYZABC99123ZZBB';
     testContact.LastName = 'TestXYZABC99123ZZBB';
     insert testContact;
     
     //Instantiate Your Controller
     ContactController controller = new ContactController();
     
     //Invoke your methods
     controller.setSearchText(testContact.FirstName);
     controller.search();
     List<Contact> contacts = controller.getResult();
     
     //Assert the Code worked
     system.assertEquals(1,contacts.size());
    }
}