Test Class Interview Question
Test Class Interview Question
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.
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.
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.
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.
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.