TEST
CLASSES
1. What is test class in Salesforce?
Ans: When we are moving code from sandbox to production org we
need to write a test class. Whatever apex class(code) we have written we need
to ensure that each and every condition is correct. The code must have 75%
coverage to deploy it to production, else we will not be able to deploy our
code.
To create test class, we create test data in test classes, to
check our functionality. We avoid using data from salesforce orgs.
Ans : Code Coverage is the percentage
number of lines covered by the test class by a total number of lines need to be
covered.
Minimum code coverage for the
trigger is at least 1% and for class, the overall code coverage of production
should be above 75% before a new component can be deployed to the production.
Ans: We
cannot write test code (test methods) inside of the apex trigger.
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.
private class MyTestClass {
static testMethod void myTest1() {
}
static testMethod void myTest2() {
}
}
Ans: we
all know that for most unit tests we need some test data, so to create test
records once and then access them in every method, you have to use @TestSetup
annotation in a test class. Data generated in @TestSetup will be persisted for
use in every test method within the test class. Those data are created using
@TestSetup annotation will roll back after the execution of the whole test
class is complete.
Key points of @TestSetup annotation
Some key points of a @testsetup method are:
- Test setup method is
time-saving.
- Reduce test execution time
while working with many records.
- Multiple @testsetup methods
are allowed in a test class.
- When an error is generated in the setup method then the entire test class fails.
- If the test class is marked
with @isTest(SeeAllData=true), setup method can’t be used.
- Setup method won’t store a
value in static variables.
- It won’t take arguments and
don’t return value.
Syntax:
@testSetup
static void testData() {
}
Example:
6.
What is a seeAllData=true?
Ans: Test classes run
in a different context, i.e. a test class have no idea about the data stored in
Salesforce, by setting seeAllData=true => @isTest(seeAllData = true).
if you mention @isTest(seeAllData = true) then test
class can recognize the existing data in the database.
We should avoid using seeAllData = True.
7.What is
@testVisible Annotation? When to use it?
Ans: Sometimes in test classes, we
need to access a variable from apex classes, if it is private, we cannot Access
for that we will replace Private with Public. For this reason, we will be
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 in test classes.
Apex class:
Test class:
Ans: To compare Actual value and Expected
value we use assert statements.
A.
system.assertEquals(val1,val2): If both
val1 and val2 are same then test class run successfully otherwise test class
will fail.
B.
system.assertNotEquals(val1,val2): If
both val1 and val2 are not same then test class run successfully otherwise test
class will fail.
C. system.assertEquals(val1> val2): If the condition satisfied then test class run successfully otherwise test class will fail.
9.What is difference between System.Assert
and System.AssertEquals ?
Ans: System.Assert accepts two parameters, one (mandatory) which is the condition to test for and the other a message (optional) to display should that condition be false.
System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false.
Both are used in Test Class for “Positive Case Testing” and “Negative Case Testing” with single and multiple records.
10.What is the purpose of system.runAs()?
Ans: In general, apex code runs in System Mode, in system mode record sharing and permissions of the current user are not taken into account. The System.runAs() method enables a developer to write test methods that can change the user context to a new user created in the test or an existing user, this enforces records sharing for the specific user.
It
also provides a way to handle mixed DML in test classes.
Points
to consider while using system.Runas() :
·
You can only use the System.runAs method
in test methods.
·
When the runAs method is complete, the
original system context is started again.
·
System.runAs only enforces record
sharing, it does not enforce field-level permissions or user permissions.
·
You can bypass mixed DML operations in
your test class by having the DML operations inside the System.runAs block of
code.
11. What
is the purpose of Test.startTest() and Test.stopTest()?
Ans : To execute asynchronous methods
synchronously we call these methods from inside 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.
12. What
do you mean by “Testing already started system exception”. When do we get this
error?
Ans: This type of error occurs, when you write more
than one Test.startTest & Test.stopTest() in a test class method. Per
testMethod we can use Test.startTest() and Test.stopTest() only for one time.
13. What
is the purpose of Test.isRunningTest()?
Ans :
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 currently executing
code was called by code contained in a test method, false otherwise. Use this
method if you need to run different code depending on whether it was being
called from a test.
14. What
is @isTest(OnInstall=true) ?
Ans : Use the @isTest(OnInstall=true)
annotation to specify which Apex tests are executed during package
installation. This annotation is used for tests in managed or unmanaged
packages. Only test methods with this annotation, or methods that are part of a
test class that has this annotation, are executed during package installation.
Tests annotated to run during package installation must pass in order for the
package installation to succeed. It is no longer possible to bypass a failing
test during package installation. A test method or a class that doesn't have
this annotation, or that is annotated with @isTest(OnInstall=false) or @isTest,
is not executed during installation.
15.What
is @isTest(isParallel=true)?
Ans: Use the @isTest(isParallel=true)
annotation to indicate test classes that can run in parallel. Default limits on
the number of concurrent tests do not apply to these test classes. This
annotation makes the execution of test classes more efficient, because more tests
can be run in parallel.
Considerations for the @isTest(isParallel=true) Annotation:
A.
This annotation overrides settings that
disable parallel testing.
B.
@isTest(SeeAllData=true) and
@isTest(isParallel=true) annotations cannot be used together on the same Apex
method.
16.What
is Test.setMock? When you will use it?
Ans : By default, test methods don’t
support web service callouts, and tests that perform web service callouts fail.
To prevent tests from failing and to increase code
coverage, Apex provides the built-in WebServiceMock interface and the
Test.setMock method. Use WebServiceMock and Test.setMock to receive fake
responses in a test method. So, Mainly we use this when we need to write test
class for API callout or any other integration.
17. How
to Write a test class for REST and SOAP?
Ans : The first step is to create a mock
class for API callout. The first step is to create a class that implements the
HTTPCalloutMock interface. The Mock class is nothing but a class that will
generate a fake response for our API callout in the Test class. As, we don’t Need
to hit the actual API, while testing we should have a fake response that will
be returned when a callout is Performed.
The Mock class is also a test class with @isTest
annotation that implements HTTPcalloutMock interface. This interface consists
of single method respond() which accepts an instance of HTTPrequest class as a
Parameter and return an instance of HTTPresponse which we will construct inside
the Methods itself.
18. What
is test Suite?
Ans : A test suite is a
collection of Apex test classes that you run together.
Note : For Best Practices of test class , https://learnsfdcwithmg.blogspot.com/2022/05/best-practices-of-apex-trigger-and-apex.html
No comments:
Post a Comment