What are the common facts for apex test Classes?
isTest Annotation is used to define the Apex Test class.
Classes defined with the isTest annotation do not count against your organization limit of 2 MB for all Apex code.
Classes annotated with isTest can be declared as private or public otherwise Top-level type must have public or global visibility.
At least 75% of your Apex code must be covered by unit tests, and all of those tests must complete successfully.
Every trigger must have some test coverage.
All classes and triggers must compile successfully.
What are some Best Practices while writing Test Classes?
Whenever we are creating the test class, we must be following below best practices:
Though we need 75% code coverage to deploy the class from Sandbox to Production, still test Coverage Target Should not be limited to 75%, we should try to make it 100%.
If possible don’t use seeAllData=true, Create your Own Test Data.
Create a Common class for Dummy Data for testing, and use it everywhere.
If you’re Object’s Schema is not changing frequently, you can create a CSV file of records and load in a static resource. This file will act as Test data for your Test Classes.
Use As much as Assertions like System.AssertEquals or System.AssertNotEquals to verify the test result.
Use Test.startTest() to reset Governor limits in Test methods and close it with Test.stopTest().
End your test class with “_Test”. So that in the Apex Class list view, the Main class and Test class will come together, resulting in easy navigation and time saver.
Always test the Batch Capabilities of your code by passing 20 to 100 records.
Use the runAs method to test your application in different user contexts.
If there is a unique field then use System. currentTimeMillis() method with the name
Test Negative scenario in the test class.
What are the methods used for test class?
Test.getStandardPricebookId() - Returns the ID of the standard price book in the organization.
Test.isRunningTest() -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.
Test.startTest()-Marks the point in your test code when your test actually begins. Use this method when you are testing governor limits.
Test.stopTest()-Marks the point in your test code when your test ends. Use this method in conjunction with the startTest method.
Test.loadData(Schema.SObjectType, String)-Inserts test records from the specified static resource .csv file and for the specified sObject type, and returns a list of the inserted sObjects.
Apart from the above, there are below methods used in Apex test Class:
System.Assert
System.AssertEquals
System.AssertNotEquals
System.runAs(User)
What is the difference between System.Assert and System.AssertEquals()?
Basically, both methods are used to prove that code behaves properly.
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.
Syntax:
System.assert(var1 == var2, "The value of var1 is: " +var1 + " and the value of... oh hell I don't care I mean it's just a variable");
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.
Syntax:
System.assertEquals(var1, var2,<Optional Message>);
The advantage of System.AssertEquals over System.Assert is that the AssertEquals method shows the exact error without using the optional parameters. However, in the Assert method, we cannot be able to see the error until unless we will pass some message in the optional parameter.
To understand this, let's take an example, I am creating an Account in below two test classes. In one class, I am using the Assert method and in another class, I am using the assertEquals method.
TestSetup Methods
Multiple test methods in the same class often have similar data requirements. Because of this, the Lightning Platform offers a way for you to annotate methods in your test class as @testSetup methods. The platform calls these methods automatically before every individual test method.
@isTest
private class myDataGenerationTests {
@testSetup
static void myDataGenerationTests() {
List<sObject> accounts = Test.loadData(Account.SObjectType, 'Mock_Data');
}
@isTest
static void testLoadAccountsFromStaticResource(){
Test.startTest();
List<Account> lstAccount =[Select Id from Account];
Test.stopTest();
system.assert(lstAccount.size() == 15, 'expected 15 accounts');
}
}
No comments:
Post a Comment