Tutorial by Examples

static TestMethod void DmlTest() { List<Contact> allContacts = [SELECT Id FROM Contact]; System.assert(allContacts.isEmpty()); Contact testContact = new Contact(FirstName = 'John', LastName = 'Doe'); insert testContact; allContacts = [SELECT Id FROM Contact]; Sy...
This test class will test the IsBlank(...) method of SomeClass. Below is the example SomeClass. This class has only the one, basic static method, but you will be unable to deploy it to a production instance for use until you have reached the code coverage threshold. public class SomeClass { ...
You can use a method annotated with @testSetup to write code that will have been executed before each test run: public class AccountService { public static Account fetchAccount() { return [ SELECT Id, Name FROM Account LIMIT 1 ]; } } @isTest public class AccountServiceTest { priv...
While you can use the @testSetup annotation to designate a method to be run before tests are executed, this method will usually only be run once. If you need code to be run before each test, you can use a static block: @isTest public class MyTest { static { // code here will be run before ...
System.assert can be used to check that a boolean expression evaluates to true: System.assert(Service.isActive()); System.assert(!Service.getItems().isEmpty(), 'items should not be empty'); System.assertEquals and System.assertNotEquals can be used to check equality of two values. The expected ...

Page 1 of 1