What do others say about mocking?

cAs I said, some developers favor mocking, and others do not. Software Engineering at Google, edited by Winters, Manshreck, and Wright (2020), has an entire dedicated to test doubles. Here’s what I understood from it, along with my own point of view:

Date and time wrappers

Software systems often use date and time information. For example, you might need the current date to add a special discount to the customer’s shopping cart, or you might need the current time to start a batch processing job. To fully exercise some pieces of code, our tests need to provide different dates and times… Continue reading Date and time wrappers

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… Continue reading The disadvantages of mocking

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… Continue reading Simulating exceptions

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>.… Continue reading Capturing arguments

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,… Continue reading Mocks and expectations

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… Continue reading Stubbing dependencies