Author: Haroon Khalil
-
The disadvantages of mocking
I have been talking a lot about the advantages of mocks. However, as I hinted before, a common (and heated) discussion among practitioners is whether to use mocks. Let’s look at possible disadvantages. Some developers strongly believe that using mocks may lead to test suites that test the mock, not the code. That can happen. When…
-
Mocks in the real world
Now that you know how to write mocks and stubs and how you can write powerful tests with them, it is time to discuss best practices. As you can imagine, some developers are big fans of mocking. Others believe mocks should not be used. It is a fact that mocks make your tests less real.…
-
Simulating exceptions
The developer realizes that SAP’s send method may throw a SapException if a problem occurs. This leads to a new requirement: The system should return the list of invoices that failed to be sent to SAP. A failure should not make the program stop. Instead, the program should try to send all the invoices, even though some of them…
-
Capturing arguments
Imagine a tiny change in the requirements of sending the invoice to the SAP feature: Instead of receiving the Invoice entity directly, SAP now requires the data to be sent in a different format. SAP requires the customer’s name, the value of the invoice, and a generated ID. The ID should have the following format: <date><customer code>.…
-
Mocks and expectations
Next, let’s discuss mocks. Suppose our current system has a new requirement: All low-valued invoices should be sent to our SAP system (a software that manages business operations). SAP offers a sendInvoice web service that receives invoices. You know you probably want to test the new class without depending on a real full-blown SAP web service. So,…
-
Stubbing dependencies
Let’s learn how to use Mockito and set up stubs with a practical example. Suppose we have the following requirement: The program must return all the issued invoices with values smaller than 100. The collection of invoices can be found in our database. The class IssuedInvoices already contains a method that retrieves all the invoices. The code…
-
An introduction to mocking frameworks
Mocking frameworks are available for virtually all programming languages. While they may differ in their APIs, the underlying idea is the same. Here, I will use Mockito one of the most popular stubbing and mocking libraries for Java. Mockito offers a simple API, enabling developers to set up stubs and define expectations in mock objects…
-
Spies
As the name suggests, spies spy on a dependency. They wrap themselves around the real object and observe its behavior. Strictly speaking, we are not simulating the object but rather recording all the interactions with the underlying object we are spying on. Spies are used in very specific contexts, such as when it is much…
-
Mocks
Mock objects act like stubs in the sense that you can configure how they reply if a method is called: for example, to return a list of invoices when getAllInvoices is called. However, mocks go beyond that. They save all the interactions and allow you to make assertions afterward. For example, maybe we only want the getAllInvoices method to…
-
Stubs
Stubs provide hard-coded answers to the calls performed during the test. Unlike fake objects, stubs do not have a working implementation. If the code calls a stubbed method getAllInvoices, the stub will return a hard-coded list of invoices. Stubs are the most popular type of simulation. In most cases, all you need from a dependency is for…