Category: Writing Larger Tests
-
System tests
At some point, your classes, business rules, persistence layers, and so on are combined to form, for example, a web application. Let’s think about how a web application traditionally works. Users visit a web page (that is, their browser makes a request to the server, and the server processes the request and returns a response…
-
Setting up infrastructure for SQL tests
In our example, it was simple to open a connection, reset the database state, and so on, but that may become more complicated (or lengthy) when your database schema is complicated. Invest in test infrastructure to facilitate your SQL testing and make sure that when a developer wants to write an integration test, they do…
-
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…