Category: Effective Software Testing
-
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…
-
Fake objects
Fake objects have real working implementations of the class they simulate. However, they usually do the same task in a much simpler way. Imagine a fake database class that uses an array list instead of a real database. This fake object is simpler to control than the real database. A common example in real life…
-
Dummy objects
Dummy objects are passed to the class under test but never used. This is common in business applications where you need to fill a long list of parameters, but the test exercises only a few of them. Think of a unit test for a Customer class. Maybe this class depends on several other classes like Address, Email, and so on.…
-
Dummies, fakes, stubs, spies, and mocks
Before we dive into how to simulate objects, let’s first discuss the different types of simulations we can create. Meszaros (2007), defines five different types: dummy objects, fake objects, stubs, spies, and mocks. Each makes sense in a specific situation.
-
Introduction
Until now, we have been testing classes and methods that were isolated from each other. We passed the inputs to a single method call and asserted its output. Or, when a class was involved, we set up the state of the class, called the method under test, and asserted that the class was in the…
-
Creativity is key
Writing property-based tests requires a lot of creativity from the developer. Finding ways to express the property, generating random data, and being able to assert the expected behavior without knowing the concrete input is not easy. Property-based testing requires more practice than traditional example-based testing: get your hands dirty as soon as possible. I hope…
-
Common issues in property-based tests
I see three common issues in the property-based tests my students write when they learn this technique. The first is requiring jqwik to generate data that is very expensive or even impossible. If you ask jqwik to, say, generate an array of 100 elements in which the numbers have to be unique and multiples of…
-
Example-based testing vs. property-based testing
Property-based testing seems much fancier than example-based testing. It also explores the input domain much better. Should we only use property-based testing from now on? In practice, I mix example-based testing and property-based testing. In the testing workflow I propose, I use example-based testing when doing specification-based and structural testing. Example-based tests are naturally simpler…
-
Creating complex domain objects
Building more complex objects may come in handy when testing business systems. This can be done using jqwik’s Combinators feature, which we’ll use in the following listing. Imagine that we have the following Book class, and we need to generate different books for a property-based test. Listing 5.21 A simple Book class One way to do this would be to have a…