0% found this document useful (0 votes)
95 views3 pages

Test Class Interview Question

Test code cannot be written inside an Apex trigger. It can only be written in a class annotated with @isTest. Test methods allow writing test code to validate logic in classes and triggers. Test.startTest() and Test.stopTest() reset governor limits between tests, while seeAllData=true allows accessing existing data but reduces isolation of tests.

Uploaded by

Mradul Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views3 pages

Test Class Interview Question

Test code cannot be written inside an Apex trigger. It can only be written in a class annotated with @isTest. Test methods allow writing test code to validate logic in classes and triggers. Test.startTest() and Test.stopTest() reset governor limits between tests, while seeAllData=true allows accessing existing data but reduces isolation of tests.

Uploaded by

Mradul Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Syntax of test Method?


@isTest
private class MyTestClass {

static testMethod void myTest1() {


}

static testMethod void myTest2() {


}

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.

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 test Method 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().

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.

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(User
Instance).

 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().

 Mixed DML operation error occurs when you try to persist in the same transaction, changes to a Setup
Object and a non-Setup Object.
 For example, if you try to update an Opportunity record and a user record at the same time.
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.

Test.isRunningTest():
 The Test.isRunningTest() method is used to identify, if the piece of code being executed is invoked from a
Test class execution
 or from other artefacts such as a Trigger, Batch Job etc.
 Returns true if the code being executed is invoked from a test class otherwise returns a false.

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.

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.

 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.

 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.

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.

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.
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.

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.

 If the test class is having access to organization data using @isTest (SeeAllData=true) annotation, will the test
class be able to support @testSetup method?
 @testSetup method are not supported in this case.

How many @testSetup method are supported inside a test class?


 One test setup method per test class.

You might also like