+ Start a Discussion
Suresh KalluriSuresh Kalluri 

Why do we need test classes?

Hi All,

I have this doubt since some time. "Why do we need test classes?"

Thanks
Pablo_RoldanPablo_Roldan
Hi,

Most of all is to prevent errors in code. However I really recommend you to read this:
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Hopefully that answers your question.
Pablo.
SFDC_DevloperSFDC_Devloper
Hi ,

Unit Testing in Salesforce
Testing is a key and critical component to successful long term software development process. Salesforce.com strongly recommends using a test-driven development process which occurs at the same time as code development. Salesforce has very strong set of documentation. When I was learning salesforce unit testing, I realize that it is difficult to understand where to start read. Therefore, I summarized the unit testing for salesforce beginners to understand the basic aspects of unit testing.

There are few things to consider before you deploy or upload the code or package;
  1. 75% of your Apex code must be covered by unit tests
  2. All the tests must complete successfully
  3. Every trigger has some test coverage (1%)
  4. All classes and triggers must compile successfully
When you are writing a test class, you have to write test for Single Action, Bulk Action, Positive Behavior, Negative Behavior, and Restricted User.
Single Action :Test to verify that a single record produces the correct, expected result.
Bulk Action :  Test not only the single record case, but the bulk cases as well
Positive Behavior :  Verify that the expected behavior occurs through every expected permutation
Negative Behavior :  Verify that the error messages are correctly produced
Restricted User :Test whether a user with restricted access to the sObjects used in     your code sees the expected behavior
Test Class can be defined @isTest annotation. Before the Winter 12’ release we had only private test classes, but on Winter 12’ release salesforce has given the chance to write public test classes as well. Salesforce has released the public test classes for expose common methods for data creation. It can be used to setting up data that the tests need to run against. Public test methods can be called from a running test but not from a non-test request.

When you create a test method,
  1. Use static
  2. Use testMethod keyword
  3. Use void return type
  4. No any arguments
  5. No data changes performed in a test method
  6. Don’t send emails
  7. Cannot be used to test Web service callout because web services are asynchronous and tests are synchronous.
  •         The API for asynchronous test runs is a Beta release (Winter ‘12)
  •          For More : Force.com Apex code Developer’s Guide Page 153
The key methods to use in your unit tests are the system.assert() methods. There are three types of system.assert() methods.
System.assert(condition)
System.assertEquals(x,y)
System.assertNotEquals(x,y)

For the security review, every test method must have at least one system.assert() method. We need use assert methods not only for the pass the security review but also as a best practice. It will be help us to keep track the failures of Apex classes or triggers.

Structure of the Test Class
@isTest 
 private classTest_class 

    public static testMethodvoid test_name() 
    { 
       //code_block; 
    } 
}

Structure of public Test class for test data creation 
@isTest 
public class TestUtil 

    public static void createTestAccounts()  
    { 
      // Create some test accounts 
    } 
 
    public static void createTestContacts() 
    { 
      // Create some test contacts 
    } 
}

Before you write test classes, understand the best practices. There is a good post in following link to understand the best Practice for Writing Test Classes 

http://www.gokubi.com/archives/testing-best-practices

If this solves your problem, kindly mark it as the best answer.

Thanks,
Rockzz