Reader small image

You're reading from  Salesforce Lightning Platform Enterprise Architecture - Third Edition

Product typeBook
Published inNov 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789956719
Edition3rd Edition
Languages
Concepts
Right arrow
Author (1)
Andrew Fawcett
Andrew Fawcett
author image
Andrew Fawcett

Andrew Fawcett has over 30 years of experience holding several software development-related roles with a focus around enterprise-level product architecture. He is experienced in managing all aspects of the software development life cycle across various technology platforms, frameworks, industry design patterns, and methodologies. He is currently a VP, Product Management, and a Salesforce Certified Platform Developer II at Salesforce. He is responsible for several key platform features and emergent products for Salesforce. He is an avid blogger, open source contributor and project owner, and an experienced speaker. He loves watching movies, Formula 1 motor racing, and building Lego!
Read more about Andrew Fawcett

Right arrow

Unit Testing

Unit testing is a key technique used by developers to maintain a healthy and robust code base. This approach allows developers to write smaller tests that invoke more varied permutations of a given method or a unit of code. Treating each method as a distinct testable piece of code means that not only is the current usage of that method safe from regression, but that future usage is protected as well. This frees the developer to focus on other permutations, such as error scenarios and parameter values beyond those currently in use.

Unit testing is different from integration testing, where many method invocations are tested as a part of an overall business process. Both have a place on the Lightning Platform. In this chapter, we will explore when to use one over the other.

To understand how to adopt unit testing, we first need to understand dependency injection. This...

Comparing unit testing and integration testing

Much of the difference between unit and integration testing relates to the scope of the code being tested and the goals of the test. Chances are you have been mixing a combination of the two on the Lightning Platform without realizing it. Before we go deeper into these differences, let's consider some characteristics of integration testing:

  • Integration tests test your key application features and related code paths under different scenarios, which can span multiple classes, including frontend code. Thus, the term "integration" refers to all code executing end to end together for a given set of inputs (including database rows) to assert a given output at the end.
  • This type of testing occurs after unit testing, but also eventually forms a key part of what is sometimes referred to as your regression (or system) test suite...

Introduction to unit testing with Apex

At this stage, you're probably wondering just how it is technically possible, using code, to substitute or inject (to use the correct term) different compiled code during test execution. To illustrate the various options for dependency injection, let's start with a simple code example. We will explore how unit testing can be applied to Apex Enterprise Patterns later in this chapter.

The following diagram shows the Unified Modeling Language (UML) for a Car class model, which has been designed with SOC in mind. Responsibilities such as the engine, dashboard, and the digital readout display have been separated. This is a pure Apex code example to illustrate how dependencies between classes can be managed with dependency injection (DI):

The following code is for the Car class. It has a dependency on methods from the Dashboard and Engine...

Writing unit tests with the Apex Stub API

The Apex Stub API applies only within an Apex test context, so it cannot be used to implement DI outside of tests. To do that, you still need to leverage Apex interfaces. Utilizing Apex Stub APIs requires an understanding of the following:

  • Implementing the Stub Provider interface: The System.StubProvider system-provided Apex interface is effectively a callback style interface. It allows your mocking code to be informed when method calls are against classes you are mocking in your test. You can implement this interface multiple times, once per class you're mocking, or a single implementation for building sophisticated generalized mocking frameworks. The Apex Mocks open source framework from FinancialForce.com is one such framework that we will be reviewing later.
  • Dynamic creation of stubs for mocking: The platform automatically creates...

ApexMocks and Apex Enterprise Patterns

As we saw earlier, the supporting library, or Apex Enterprise Patterns, provides methods that provide a dependency injection facility through the factories in the Application class. This facility is also compatible with the use of ApexMocks and the Apex Stub API. The following sections contain examples of the use of ApexMocks to unit test the layers within the application architecture introduced in earlier chapters.

Unit testing a controller method

The following test can be found in the RaceControllerTest class and demonstrates how to mock a service layer class:

@IsTest 
private static void whenAwardPointsCalledIdPassedToService() { 
     
    fflib_ApexMocks mocks = new fflib_ApexMocks...

Unit testing with Lightning Web Components

In this section, we are going to build a unit test for the Race Setup Lightning Web Component. This component has a number of dependencies that need to be mocked in order to build a successful unit test that covers the component's HTML and JavaScript code. Here is a reminder of what the component looks like:

When the user selects drivers and clicks Add Drivers, a confirmation toast message is shown:

The Race Setup component uses a lightning-table child component defined in its HTML and methods from an Apex Controller RaceSetupComponentController class defined in its JavaScript controller. Lightning Web Components unit tests focus solely on the logic directly in the Race Setup component code (raceSetup.html and raceSetup.js) and no other concerns, thus, we will focus on testing the following behaviors:

  • When the component has...

Summary

In this chapter, you learned that integration testing focuses on the scope of UIs or APIs exposed via your services to test the full stack of your code. It requires setting up the database, executing the code to be tested, and querying the database. These tests are critical to ensuring that all the components of your application deliver the expected behavior.

This chapter also introduced unit testing. Here, you learned that in order to make individual code components, classes, and methods as robust and future-proof as possible, developers can test each method in isolation without incurring the overhead of setting up the database or updating it. As such, unit tests run more quickly. Unit tests can increase coverage, as more corner-case testing scenarios can be emulated using the mocking of scenarios that would otherwise be impossible or difficult to set up on the database...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Salesforce Lightning Platform Enterprise Architecture - Third Edition
Published in: Nov 2019Publisher: PacktISBN-13: 9781789956719
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Andrew Fawcett

Andrew Fawcett has over 30 years of experience holding several software development-related roles with a focus around enterprise-level product architecture. He is experienced in managing all aspects of the software development life cycle across various technology platforms, frameworks, industry design patterns, and methodologies. He is currently a VP, Product Management, and a Salesforce Certified Platform Developer II at Salesforce. He is responsible for several key platform features and emergent products for Salesforce. He is an avid blogger, open source contributor and project owner, and an experienced speaker. He loves watching movies, Formula 1 motor racing, and building Lego!
Read more about Andrew Fawcett