Skip to main content

Apex Testing

The following are the benefits of Apex unit tests:

  • Ensuring that your Apex classes and triggers work as expected.
  • Having a suite of regression tests that can be rerun every time classes and triggers are updated to ensure that no future updates you make to your app break existing functionality.
  • Meeting the code coverage requirements for deploying Apex to production or distributing Apex to customers via packages.
  • High quality apps delivered to the production org, which makes production users more productive.
  • High quality apps delivered to package subscribers, which increase your customers trust.
Before each major service upgrade, Salesforce runs all tests in all organizations with Apex code to make sure that no behavior in custom code has been altered as a result of any service upgrades. A side benefit of having Apex unit tests in your org is that Salesforce will run your tests for you and check that they work in each new release.
Before you can deploy your code or package it for the Force.com​ AppExchange, at least 75% of Apex code must be covered by tests and all those tests must pass. In addition, each trigger must have some coverage. Even though code coverage is a requirement for deployment, you shouldn’t write tests only to meet this requirement. Make sure to test the common use cases in your app, including positive and negative test cases, and bulk and single-record processing.

Test methods take no arguments and have the following syntax:
1@isTest static void testName() {
2    // code_block
3}
Alternatively, a test method can have this syntax:
1static testMethod void testName() {
2    // code_block
3}
** Using the isTest annotation instead of the testMethod keyword is more flexible as you can specify parameters in the annotation.

The visibility of a test method doesn’t matter, so declaring a test method as public or private doesn’t make a difference as the testing framework is always able to access test methods. For this reason, the access modifiers are omitted in the syntax.
Test methods must be defined in test classes, which are classes annotated with isTest. This is a definition of a test class with one test method.
1@isTest
2private class MyTestClass {
3    @isTest static void myTest() {
4        // code_block
              System.assertEquals(-23.33,-23.33);
              System.assertEquals(100,-23.33,'Boiling point temperature is not expected.');
5    }
6}
Test classes can be either private or public. If you’re using a test class for unit testing only, declare it as private. Public test classes are typically used for test data factory classes

The verifications are done by calling the System.assertEquals()method, which takes two parameters: the first is the expected value, and the second is the actual value. There is another version of this method that takes a third parameter—a string that describes the comparison being done. This optional string is logged if the assertion fails.

Salesforce records that are created in test methods aren’t committed to the database; they’re rolled back when the test finishes execution. This rollback behavior is handy for testing because you don’t have to clean up your test data after the test executes.
By default, Apex tests don’t have access to pre-existing data in the org, except for setup and metadata objects that can be accessed such as the User or Profile objects. You should set up test data for your tests. Creating test data makes your tests more robust and prevents failures that are caused by missing or changed data in the org. You can create test data directly in your test method, or by using a utility test class as you’ll find out later.
Even though it is not a best practice to do so, there might be times when a test method needs access to pre-existing data. To access org data, annotate the test method with @isTest(SeeAllData=true). The test method examples in this unit don’t access org data and therefore don’t use the SeeAllData parameter.
  • Up to 3 MB of Apex code can be saved in each org. Test classes annotated with isTest don’t count toward this limit.
  • Even though test data rolls back, no separate database is used for testing. As a result, for some sObjects that have fields with unique constraints, inserting duplicate sObject records results in an error.
  • Test methods don’t send emails.
  • Test methods can’t make callouts to external services. You can use mock callouts in tests.
  • SOSL searches performed in a test return empty results. To ensure predictable results, you can use Test.setFixedSearchResults() to define the records to be returned by the search.

The test method contains the Test.startTest() and Test.stopTest() method pair, which delimits a block of code that gets a fresh set of governor limits. In this test, test-data setup uses two DML statements before the test is performed. To test thatApex code runs within governor limits, isolate data setup’s limit usage from your test’s. To isolate the data setup process’s limit usage, enclose the test call within the Test.startTest() and Test.stopTest() block. Also use this test block when testing asynchronous Apex.











Comments

Popular posts from this blog

Visual Workflow

The benefits of Visual Workflow There are  certain benefits of using Visual Workflow. They are as follows: It allows you to create an automated business process using click not code. Visual Workflow does not require coding, and even if you do not know Apex code you can still develop business processes. Using screens, fields, and choices, you can implement complex business processes to make sure that your users are entering data in the right format. Through Visual Workflow, you can manipulate data for certain objects that are not available for the Workflow rule. For example, when a "contact role" is created or updated as primary for an opportunity then create a new task. It allows you to auto submit records for approval. You can post messages on Chatter. For example, if opportunity status gets Closed Won, post a message on Chatter group. It allows you to embed the Flow into the Visualforce page and using the Force.com Site you can expose it for ...

VisualForce Best Practices

Accessing component IDs When we refer Visualforce  components in JavaScript, the ID attribute plays a major role. Every Visualforce component has an ID attribute. The ID attribute must be specified to a particular component in order to refer to it in JavaScript and it is used to bind the two components together. When the page is rendered, this ID attribute is a part of DOM ID of the particular component. The ID attribute must be unique as well. The following best practices are applied for accessing component IDs: Use the  $Component  global variable  to simplify access. For an example, when we have an input field with  id="inputOne"  within a page block with  id="blockOne" , we can access the input field with the $Component.blockOne.inputOne  expression. No need to  specify an ID for a component you want to access if it is an ancestor or sibling to the $Component  variable in the Visualforce component's hierarchy. ...

Visualforce fundamentals

Visualforce  is a web development framework that enables developers to build sophisticated, custom user interfaces for mobile and desktop apps that can be hosted on the  Force.com  platform. You can use  Visualforce  to build apps with user interfaces that look like the standard interface provided by  Force.com , as well as your own completely custom interface. Visualforce  enables developers to extend  Salesforce ’s built-in features, replace them with new functionality, and build completely new apps. Use powerful built-in standard controller features, or write your own custom business logic in  Apex . You can build functionality for your own organization, or create apps for sale in the  AppExchange . Visualforce  app development is familiar to anyone who has built web apps. Developers create  Visualforce  pages by composing components, HTML, and optional styling elements.  Visualforce  can integrate with ...