← Back to list

Writing Pure Apex Unit Testing

In the era of agile software development, with quick releases of small iterations, we should do testing more and more frequently.

Rajkumar Gaikwad · 2022-04-22 13:35 · 72 claps · 1.7 min read
#apex-unit-testing #unit-testing #mocker #mockito
Open on Medium ↗
Wiki topics: 💻 · Programming 📋 · Product Management

Writing Pure Apex Unit Testing

In the era of agile software development, with quick releases of small iterations, we should do testing more and more frequently.

However, Salesforce development world, we focused on writing functional / integration level testing (which combines individual components and tests them as groups) rather than doing pure low level unit testing.

Unit test is the foundation of all other testing, the more pure unit testing we write the more robust software we will produce.

What is unit testing ?

In simple words, testing the smallest piece of your code.

A “good” unit test follows these rules -

  1. Every independent class should have independent unit test class created. — every function/ method we write should have corresponding unit test for that, if you change that function so that you change unit test method of it as well.
  2. Writing unit test doesn’t mean testing everything — we should always test the class/ method under test and mock all other integration / dependencies (Unit tests should be self-sufficient), as those dependencies are already tested independently. The main objective of unit testing is to ensure that each individual part is working well and as it’s supposed to work.

Unlike mockito framework from Java development world, we don’t have that flexible robust mocker framework provided in Salesforce. However, Salesforce has provided Stub API for implementing a mocking framework.

Finnacialforce has provided a bunch of good libraries (Stub APIs) using that we can easily write the true apex unit tests.

You can find the sample unit test class implemented using financialforce stub APIs here.

Benefits — Though there are tons of benefits we will get by writing pure unit tests but this is incredibly useful when you have many dependencies with upstream process /systems as it will save a-lot of your unit test development time and tests runs faster as it meant to be.

References —

[embed]Code 4 Cloud Coding for the Force.com Platformcode4cloud.wordpress.com

[embed]Build a Mocking Framework with the Stub API Apex provides a stub API for implementing a mocking framework. A mocking framework has many benefits. It can streamline…developer.salesforce.com

[embed]Breaking Runtime Dependencies with Dependency Injection Whether you actively plan for it or not, dependency management is part of a developer's everyday work. In this post…developer.salesforce.com

[embed]Unit Testing, Apex Enterprise Patterns and ApexMocks - Part 1 If you attended my Advanced Apex Enterprise Patterns session at Dreamforce 2014 you'll have heard me highlight the…andyinthecloud.com

[embed]


메타데이터
post_id
f49db6b9e2b2
slug
writing-pure-apex-unit-testing-f49db6b9e2b2
url
https://medium.com/@rajkumar.gaikwad3/writing-pure-apex-unit-testing-f49db6b9e2b2
canonical_url
https://medium.com/@rajkumar.gaikwad3/writing-pure-apex-unit-testing-f49db6b9e2b2
author_url
https://medium.com/@rajkumar.gaikwad3
status
ok
fetched_at
2026-07-27 05:39:41