Write Positive Tests
Learning Objectives
After completing this unit, you’ll be able to:
- Explain why positive tests are important.
- Write positive unit tests.
Positive unit tests are tests that return expected results when you present valid data to the tested code. Think of the classic calculator. A positive test for an addition method might execute with data points 2 and 3 while asserting the output is 5. This is a simplistic example, of course, but the idea is the same. When you execute it with valid data, the code you’re testing should respond with predictable results.
Before We Start
Before we start, we’ll retrieve the Apex classes that got added to the org when you installed the package in unit 1.
- Open Org Browser by clicking on the cloud icon of the left panel.
- Click on Apex Classes, and click on the download button to retrieve them.
- If you see a dialog box asking to overwrite your files, select SkipAll.
You should see the classes on your files when Org Browser finishes retrieving them.
The Pattern
Writing positive unit tests is all about seeing a set of expected results when you know your input data is good. The standard model for positive tests uses the following steps.
- Generate or load test data.
- Use a data factory or a CSV file.
- Make assertions to validate your data.
- Use a data factory or a CSV file.
- Call
Test.startTest()
.- This resets governor limits and isolates your test from test setup.
- This resets governor limits and isolates your test from test setup.
- Execute the code you’re testing.
- Call
Test.stopTest()
.- This forces any asynchronous code to complete.
- This forces any asynchronous code to complete.
- Make assertions about the output of your tested code. You might assert a record exists, or that a field is set to an expected value.
To illustrate these steps, we can look at the AccountWrapper
class in VS Code. AccountWrapper
class is a custom logic layer around the Account object. This is a fairly common way to add custom logic to standard objects. In this case, you’re adding some account-level logic to calculate a rounded average amount of opportunities associated with a given account.
Let’s look at the AccountWrapper
class and write some positive tests for it.
- Open VS Code.
- In the Explorer sidebar, click the folder
classes
.
- Select the class AccountWrapper. This is the code you’re testing.
- In the Explorer sidebar, right-click the folder
classes
, then choose SFDX: Create Apex Class. Do not choose the New File command, as that won’t create the necessary metadata .xml file.
- Enter
AccountWrapperTests
as the name and accept the default directory..
- Replace the default contents with the following code.
@IsTest private class AccountWrapperTests { @TestSetup static void loadTestData() { // GIVEN Account acct = new Account(Name = 'ACME'); insert acct; List<Opportunity> opps = TestFactory.generateOppsForAccount(acct.id, 1000.00, 5); insert opps; } @IsTest static void testPositiveRoundedAveragePrice() { // WHEN Account acct = [SELECT Id FROM Account LIMIT 1]; AccountWrapper acctWrapper = new AccountWrapper(acct); // THEN Test.startTest(); Assert.areEqual( 1000.00, acctWrapper.getRoundedAvgPriceOfOpps(), 'Expected to get 1000.00'); Test.stopTest(); } }
- Click File > Save.
- Right-click the file you’re working on, then choose SFDX: Deploy Source To Org.
- Click the Run Test button that appears on the
testPositiveRoundedAveragePrice
method.
Code Highlights
This test class uses a TestFactory
class that generates five opportunities for each of your accounts.
With accounts and opportunities, you can test the logic of your accountWrapper getRoundedAvgPriceOfOpps()
method. This method is designed to return the average price of all opportunities related to this account, rounded to the nearest thousand dollars. Because this is a positive test, the data you loaded and the opportunities you generated are logically consistent with what the method needs. In other words, your opportunities have positive amount values and an easily predictable rounded average. A good maxim for positive testing is: Good code produces expected results when you give it good inputs.
Resources