Author: Haroon Khalil

  • 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…

  • Property-based testing in the real world

    Let me give you some tips on writing property-based tests.

  • 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…

  • Testing the Basket Class

    Let’s explore one last example that revisits the Basket class. The class offers two methods: an add() method that receives a product and adds it a quantity of times to the basket, and a remove() method that removes a product completely from the cart. Let’s start with the add method. Listing 5.12 Implementation of Baskets add method ❶ Checks all the pre-conditions ❷ Stores the old value so we can check the post-condition later ❸ If the product…