Back

Test Classes Q&A

1. What is the purpose of writing the test class?

After developing an apex class or apex trigger we should write the unit tests and ensure that we are able to execute at least 75% of the lines of code.

If you are moving the code from sandbox to sandbox regarding code coverage you won’t face any issue.

If you are moving the code from sandbox to production, you need to include all the test classes at the time of deployment and salesforce will run all the test classes which you included for the deployment as well as test classes which are already present in production, if the code coverage is less than 75% deployment will fail.

2. Is it possible to write test code inside of an apex class or apex trigger?

We cannot write test code (test methods) inside of the apex trigger.

From API Version 28.0, we cannot write the test methods inside of an apex class which is not decorated with @isTest.

We can write test methods only in a class which is decorated with @isTest.

Note: We have a governor limit for the overall Apex Code size of the organization which is of 3 MB. If we decorate a class with @isTest annotation Apex Code Size governor limit will be bypassed.

3.Syntax of testMethod?
@isTest
private class MyTestClass {
static testMethod void myTest1() {
}
static testMethod void myTest2() {
}
}

Note: Test Class can be either public or private.

4. What is the purpose of seeAllData?

By default test class cannot recognize the existing data in the database. If you mention @isTest(seeAllData = true) then test class can recognize the existing data in the database.

From a List Custom Settings we cannot fetch the existing data without seeAllData = true in test class.

Suppose you have a custom object called ‘CustomObject__c’ and it contains many records, we cannot fetch the existing data without seeAllData = true in test class.

Note: It is not recommended to use seeAllData = true for a test class. Based on the existing data in database code coverage will impact.

5. What is the purpose of Test.startTest() and Test.stopTest()?

Test.startTest() and Test.stopTest() maintains fresh set of governor limits. Assume that you are consuming 99 SOQL queries outside of Test.startTest() and Test.stopTest() then if you include any SOQL inside of Test.startTest() and Test.stopTest() count will start from 1.

Per testMethod we can use Test.startTest() and Test.stopTest() only for one time.

To execute asynchronous methods synchronously we can call those methods from inside of Test.startTest() and Test.stopTest().

6. What is the purpose of system.runAs()?

By default test class runs in System Mode. If you want to execute a piece of code in a certain user context then we can use system.runAs (UserInstance). For more details refer 2nd question in visualforce category.

To avoid MIXED-DML-OPERATION error we can include DML statements inside of system.runAs(), still the error persists keep DML statements inside of Test.startTest() and Test.stopTest().

7. What are the assert statements? What is the purpose?

To compare Actual value and Expected value we use assert statements.

Types of assert statements

**system.assertEquals(val1,val2):** If both val1 and val2 are same then test class run successfully otherwise test class will fail.

**system.assertNotEquals(val1,val2):** If both val1 and val2 are not same then test class run successfully otherwise test class will fail.

**system.assertEquals(val1> val2):** If the condition satisfied then test class run successfully otherwise test class will fail.

8. What is the purpose of Test.isRunningTest()?

Sometimes we cannot satisfy certain if conditions for the apex classes, in those situations on those if conditions we can add Test.isRunningTest separated with or condition.

For example: if (condition || Test.isRunningTest())

9. What is the purpose of @TestVisible?

Sometimes in test classes we need to access a variable from Apex Class, if it is private we cannot access for that we will replace private with public.

For this reason we are compromising the security. To avoid this before the private variables in apex class we can include @TestVisible so that even though variable is private we can access from the test class.

10. What are the test class best approaches?

We should not depend on the existing data in the database. We should create the test data for all the possible scenarios.

Note: Profiles and recordTypes cannot be created programmatically, we can query from the database. For the remaining objects including users we should create the test data. While testing apex triggers and batch classes, we should do bulk testing at least with 200 records.

We should test for all the positive and negative scenarios.

Back
Site developed by Nikhil Karkra © 2021 | Icons made by Freepik