Unit Tests
Explore how to create unit tests for Scala HTTP APIs using functional programming. Learn to test database helpers and JSON codecs, including handling valid and invalid inputs and ensuring reversible encoding. This lesson helps build reliable and maintainable tests for pure and impure service components.
Base class for unit tests
To avoid repeating the construction of our unit test classes, we will implement a base class for tests, which is quite simple.
We will be leaning towards the more verbose test styles, but we can essentially use any of the other test styles that ScalaTest offers.
Testing Product fromDatabase
The code above is a straightforward test of our helper function fromDatabase, which works in the following way:
- The
forAllwill generate a lot ofProductentities using the generator (Line 3). - From each entity, a list of “rows” is constructed as they would appear in the database (Line 4).
- These constructed rows are given to the
fromDatabasefunction (Line 5). - The returned
Optionmust then contain the