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? 

COMMENTS

Name

Apex,6,Batch Apex,2,Chrome Add-on,1,Chrome browser,1,Coding Best Practices,2,CRM Basics,1,cURL,1,Database Query,1,Dataset,1,Dynamic Apex,3,Dynamic SOQL,1,Einstein Analytics,9,Future Apex,1,Git,1,Google Chrome,1,JS for LWC,6,Lightning Components,2,lightning Page,1,Lightning Web Components,13,Linux for Windows users,1,LWC,2,Queueable Apex,1,Redirect page,1,REGEX,1,REST API,1,Salesforce CRM,1,Salesforce Errors,1,Salesforce Interview Questions,13,Salesforce Lightning,1,Salesforce New Features,1,Salesforce Tasks,2,Schedule Apex,1,Test Class,1,Triggers,1,Visualforce Pages,3,Visualforce Pagination,2,VS Code,2,Wave Analytics,7,Winter Release Notes,1,workbench,1,
ltr
item
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog: Apex Test Classes Interview Questions and Answers
Apex Test Classes Interview Questions and Answers
Apex Test Classes Interview Questions and Answers,Salesforce Interview Question on Apex Unit testing ,test classes apex class salesforce,apex test classes best practices,Salesforce Interview Question Test Class,Test Class question on apex
https://1.bp.blogspot.com/-zFgs2KBcK_8/Wiuc1JlPkGI/AAAAAAAAAHY/HDp8zITOqVMpaXmTreWlIxVN5ea4Ux2uQCLcBGAs/s640/apexTesting.png
https://1.bp.blogspot.com/-zFgs2KBcK_8/Wiuc1JlPkGI/AAAAAAAAAHY/HDp8zITOqVMpaXmTreWlIxVN5ea4Ux2uQCLcBGAs/s72-c/apexTesting.png
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog
https://swdcworld.blogspot.com/2019/01/apex-test-classes-interview-questions.html
https://swdcworld.blogspot.com/
https://swdcworld.blogspot.com/
https://swdcworld.blogspot.com/2019/01/apex-test-classes-interview-questions.html
true
5370056087523941001
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy