Notes
All the test methods are static.
startTest():
Marks the point in your test code when your test actually begins. Use this method when you are testing governor limits.
stopTest():
Marks the point in your test code when your test ends. Use this method in conjunction with the startTest method.
@isTest
private class MyTestClass {
@isTest
static void myTest() {
// Create test data
Account acc = new Account(Name='Test Account');
insert acc;
// Mark the start of the test block
Test.startTest();
// Call asynchronous code that needs to be tested
List<Contact> contacts = new List<Contact>();
contacts.add(new Contact(FirstName='John', LastName='Doe', AccountId=acc.Id));
Database.SaveResult[] results = Database.insert(contacts, false);
// Verify that the asynchronous code has completed
System.assertEquals('Success', results[0].getStatus());
// Mark the end of the test block
Test.stopTest();
}
}
loadData(sObjectToken, resourceName):
Inserts test records from the specified static resource .csv file and for the specified sObject type, and returns a list of the inserted sObjects.
@isTest
private class MyTestClass {
@isTest
static void myTest() {
// Load test data from a CSV file
List<Account> accounts = (List<Account>) Test.loadData(Account.sObjectType, 'MyTestAccounts');
// Insert the test data
insert accounts;
// Verify that the test data was inserted correctly
List<Account> queriedAccounts = [SELECT Name FROM Account WHERE Name LIKE 'Test%'];
System.assertEquals(accounts.size(), queriedAccounts.size());
}
}
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.
public class MyUtilityClass {
public static void doSomething() {
if (Test.isRunningTest()) {
// Code to execute when running in a test context
System.debug('Running in test context');
} else {
// Code to execute when running outside of a test context
System.debug('Running outside of test context');
}
}
}
Video
Video does not exists.