Wednesday, December 14, 2022

Interview Questions and Answers on Test Classes

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.
See the below examples –

  • 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

  1. system.assertEquals(val1,val2): If both val1 and val2 are same then test class run successfully otherwise test class will fail.
  2. system.assertNotEquals(val1,val2): If both val1 and val2 are not same then test class run successfully otherwise test class will fail.
  3. 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. 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?

  1. 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.
  2. While testing apex triggers and batch classes, we should do bulk testing at least with 200 records.
  3. We should test for all the positive and negative scenarios.

Testing Apexsalesforce.com strongly recommends that you use a test-driven development process, that is, test development that occurs at the same time as code development.
Why Test Apex?* Testing is key to the success of your application, particularly if your application is to be deployed to customers.* If you validate that your application works as expected, that there are no unexpected behaviors, your customers are going to trust you more.* There are two ways of testing an application.  1. One is through the Salesforce user interface, important, but merely testing through the user interface will not catch all of the use cases for your application.  2. The other way is to test for bulk functionality: up to 200 records can be passed through your code if it’s invoked using the Force.com Web services API or by a Visualforce standard set controller.* You will have additional releases of it, where you change and extend functionality.* If you have written comprehensive tests, you can ensure that a regression is not introduced with any new functionality.
Before you can deploy your code or package it for the Force.com AppExchange, the following must be true:• 75% of your Apex code must be covered by unit tests, and all of those tests must complete successfully.Note the following:- When deploying to a production organization, every unit test in your organization namespace is executed.- Calls to System.debug are not counted as part of Apex code coverage in unit tests.- While only 75% of your Apex code must be covered by tests, your focus shouldn’t be on the percentage of code that is covered.- Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single record.- This should lead to 75% or more of your code being covered by unit tests.• Every trigger has some test coverage.• All classes and triggers compile successfully.Note:Salesforce runs all tests in all organizations with Apex scripts to verify that no behavior has been altered as a result of any service upgrades. 

What to Test in ApexSalesforce.com recommends that you write tests for the following:Single actionTest to verify that a single record produces the correct, expected result.Bulk actionsEvery Apex script, whether a trigger, a class or an extension, may be invoked for 1 to 200 records. You must test not only the single record case, but the bulk cases as well.Positive behaviorTest to verify that the expected behavior occurs through every expected permutation, that is, that the user filled out everything correctly and did not go past the limits.Negative behaviorThere are likely limits to your applications, such as not being able to add a future date, not being able to specify a negative amount, and so on.You must test for the negative case and verify that the error messages are correctly produced as wellas for the positive, within the limits cases.Restricted userTest whether a user with restricted access to the sObjects used in your code sees the expected behavior.That is, whether they can run the code or receive error messages.

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...