The following are the system static methods for Test.
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| isRunningTest | Boolean | Returns true if the currently executing code was called by code contained in a method defined as testMethod, false otherwise. Use this method if you need to run different code depending on whether it was being called from a test. | |
| setCurrentPage | PageReference page | Void | A Visualforce test method that sets the current PageReference for the controller. |
| setCurrentPageReference | PageReference page | Void | A Visualforce test method that sets the current PageReference for the controller. |
| setFixedSearchResults | ID[] opt_set_search_results | Void | Defines a list of fixed search results to be returned by all
subsequent SOSL statements in a test method. If opt_set_search_results is not specified, all subsequent SOSL queries return no results. The list of record IDs specified by opt_set_search_results replaces the results that would normally be returned by the SOSL queries if they were not subject to any WHERE or LIMIT clauses. If these clauses exist in the SOSL queries, they are applied to the list of fixed search results. For more information, see Adding SOSL Queries to Unit Tests. |
| setReadOnlyApplicationMode | Boolean application_mode | Void | Sets the application mode for an organization to read-only
in an Apex test to simulate read-only mode during Salesforce upgrades and downtimes. The application mode is reset to the
default mode at the end of each Apex test run. setReadOnlyApplicationMode is available as part of 5 Minute Upgrade. See also the getApplicationReadWriteMode System method. |
| startTest | Void | Marks the point in your test code when your test actually begins. Use this method when you are testing governor limits. You can also use this method with stopTest to ensure that all asynchronous calls that come after the startTest method are run before doing any assertions or testing. Each testMethod is allowed to call this method only once. All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits. | |
| stopTest | Void | Marks the point in your test code when your test ends. Use this method in conjunction with the startTest method. Each testMethod is allowed to call this method only once. Any code that executes after the stopTest method is assigned the original limits that were in effect before startTest was called. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously. |
The following example sets the application mode to read only and attempts to insert a new account record, which results in the exception. It then resets the application mode and performs a successful insert.
@isTest private class ApplicationReadOnlyModeTestClass { public static testmethod void test() { // Create a test account that is used for querying later. Account testAccount = new Account(Name = 'TestAccount'); insert testAccount; // Set the application read only mode. Test.setReadOnlyApplicationMode(true); // Verify that the application is in read-only mode. System.assertEquals( ApplicationReadWriteMode.READ_ONLY, System.getApplicationReadWriteMode()); // Create a new account object. Account testAccount2 = new Account(Name = 'TestAccount2'); try { // Get the test account created earlier. Should be successful. Account testAccountFromDb = [SELECT Id, Name FROM Account WHERE Name = 'TestAccount']; System.assertEquals(testAccount.Id, testAccountFromDb.Id); // Inserts should result in the InvalidReadOnlyUserDmlException // being thrown. insert testAccount2; System.assertEquals(false, true); } catch (System.InvalidReadOnlyUserDmlException e) { // Expected } // Insertion should work after read only application mode gets disabled. Test.setReadOnlyApplicationMode(false); insert testAccount2; Account testAccount2FromDb = [SELECT Id, Name FROM Account WHERE Name = 'TestAccount2']; System.assertEquals(testAccount2.Id, testAccount2FromDb.Id); } }