Testing Asynchronous Code
Learn how to test asynchronous code in Scala with ScalaTest.
Synchronous vs. asynchronous code
So far, we’ve only been testing synchronous code; every method and test we’ve written has been blocking and not asynchronous. This isn’t very realistic in a modern application since many operations have to be non-blocking. Think of downloading a file from somewhere, querying a database, or waiting for a response from another application on the internet. If those operations were blocking, our applications would be incredibly slow, and we’d waste tons of time waiting for results.
The basic concurrency component in Scala is the Future
class. An instance of Future
is nothing more than a placeholder for a value that will be ready sometime in the future. Using Future
leads to faster code that can be run in a non-blocking way. In this lesson and the next, we’ll get a glimpse of how to test asynchronous code in Scala and ScalaTest. Covering the Future
execution model in ScalaTest in detail is outside the scope of this course because it requires a deeper understanding of the concurrency model in Scala. In the next sections, we’ll see different possibilities for Future
in our tests without focusing too much on the underlying mechanics.
Get hands-on with 1400+ tech skills courses.