Author: haroonkhan

  • Writing automated tests for SQL queries

    We can use JUnit to write SQL tests. All we need to do is (1) establish a connection with the database, (2) make sure the database is in the right initial state, (3) execute the SQL query, and (4) check the output. Consider the following scenario: A simple JDBC implementation of such a class is…

  • What to test in a SQL query

    SQL is a robust language and contains many different functions we can use. Let’s simplify and look at queries as a composition of predicates. Here are some examples: In these examples, value < 50, i.customer_id = c.id, c.country = ‘NL’, and value > 50 and value < 200 are the predicates that compose the different queries. As a tester, a possible criterion is to exercise the predicates and check whether the SQL…

  • Database and SQL testing

    In many of the examples a Data Access Object (DAO) class is responsible for retrieving or persisting information in the database. Whenever these classes appear, we quickly stub or mock them out of our way. However, at some point, you need to test these classes. These DAOs often perform complex SQL queries, and they encapsulate a…

  • Testing larger components that go beyond our code base

    In the previous example, the large test gives us confidence about the overall behavior of the component, but we could still test each unit individually. In some cases, however, we cannot write tests for units in isolation. Or rather, we can write tests, but doing so would not make sense. Let’s look at examples of…

  • Testing larger components

    As always, let’s use a concrete example. Suppose we have the following requirement: Given a shopping cart with items, quantities, and respective unit prices, the final price of the cart is calculated as follows: NOTE The business rule related to delivery costs is not realistic. As a developer, when you notice such inconsistencies, you should talk…

  • When to use larger tests

    I see two situations where you should use a larger test: The following sections show examples of both cases and will help you generalize them.

  • Introduction

    Most of the code we tested could be tested via unit tests. When that was not possible because, say, the class depended on something else, we used stubs and mocks to replace the dependency, and we still wrote a unit test. As I said when we discussed the testing pyramid. I favor unit tests as…

  • TDD and proper testing

    Some studies show that TDD practitioners write more test cases than non-TDD practitioners. However, I do not believe that the test suites generated by TDD sessions are as good as the strong, systematic test suites we engineered in after applying different testing practices. The reasoning is simple: when doing TDD, we are not focused on…

  • Other schools of TDD

    TDD does not tell you how to start or what tests to write. This flexibility gave rise to various different schools of TDD. If you are familiar with TDD, you may have heard of the London school of TDD, mockist vs. classicist TDD, and outside-in TDD. This section summarizes their differences and points you to…

  • What does the research say about TDD?

    TDD is such a significant part of software development that it is no wonder researchers try to assess its effectiveness using scientific methods. Because so many people treat it as a silver bullet, I strongly believe that you should know what practitioners think, what I think, and what research currently knows about the subject. Research…