Apex Test Classes Interview Questions and Answers

Salesforce Interview Question on Apex Unit testing 

apex-Testing

1. What is an apex Unit Testing? Why we need to write Test classes?

2. What is @isTest annotation? When you will use it?
When you define method or class using @isTest annotation then it only contains code used for testing your application. Test classes should be public or private only. Test class doesn't count against your organization limit of 3 MB for all Apex code.
@isTest
private class MyTestClass {
   // Methods for testing
   @isTest static void test1() {
      // Implement test code
   }
}

3. What is Test.startTest() and Test.stopTest()? When you will use it?
Test.startTest() and Test.stopTest() allow you to reset the governor limits within the context of your test execution and testing asynchronous classes. When Test.startTest() begins all the governor limits will be reset. and Test.stopTest() to end the execution of asynchronous process and switch back to the main context.

Points to remember when you use Test.startTest() and Test.stopTest() methos.

  • These two methods we can use only once in test method.
  • It's not mandatory to use in every test method, but we can use some situations like testing Asynchronous classes.
4. What is @testVisible annotation? When you will use it?
For example, If you have an Apex class or controller, It has a Private or Protected variable or methods or inner classes. so you can't access them in other classes. But using this @testVisible annotation in a Private or Protected variable or methods or inner classes safely expose them only for test classes.
Ex: In Apex class not in Test class
// Private variable
@TestVisible private static String accountName= 'acme';
// Private method
@TestVisible private static void showRecord(String name) {
    // add something
}
access them in test class accustomed. 

5. What is @testSetup annotation? When you will use it?
For example, we have a test class, It has two test methods and each test method required 5 account records.So without creating 5 records for each test method, we will create a new method with annotation @testSetup then create only 5 account records. These records can access all test methods using SOQL query in that test class.   
Ex: @testSetup method
@testSetup static void setup() {
    // Create common test accounts
    List<Account> accountsLst = new List<Account>();
    for(Integer i=0;i<5;i++) {
testAccts.add(new Account(Name = 'TestAc '+i));
    }
    Insert accountsLst;     
}
Thinks to remember when you use @testSetup method:
  • We can use only one test setup method per class.
  • Test setup method will execute first in the test class.
  • If any DML error occurs in this test setup method, rest of the method not executed.
  • If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.

6. What is a Test.setCreatedDate? When you will use it?
Sometimes we need test data like record created date in past. So we will use Test.setCreatedDate to set the created date meet our requirements. It only used in test classes.
Ex:
Account ac = new Account(name='test ac');
Insert ac;
Test.setCreatedDate(ac.Id, DateTime.newInstance(2015,01,01));

7. What is a seeAllData=true?
In Apex test classes every time we need to create a new set of data for your test class. But some objects we can't create a data for testing. for example Profiles. we can't create profiles in test classes. so we will query the profiles and assign to users.
@isTest (SeeAllData = true)

8. Which Objects can we access without using seeAllData=true?
We use seeAllData = true to get real-time data in test class, but without using this also you can get the data from following objects.
User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent , ApexPage and custom metadata types. 

9. What is a System.runAs? When you will use it?

10. What is @IsTest(isParallel=true) ?

11. What is a test.isrunningtest()? When you will use it?
12. What is a Test.setMock? When you will use it?
13. How to run single test method in a test class?
14. What is a Test Suite?
15. What are the testing best practices? 

Popular posts from this blog

Redirect Visualforce pages in Salesforce Lightning

How to use Database.Query and Database.countQuery