Writing the tests
Let's write a simple and flawed application that can help us, as an example of how to write Functional Tests for concurrent applications. In practice, writing these tests will require more work, but the principles should still apply.
Creating a flawed UserManager
Using interfaces to define the dependencies of our code will allow us to provide mocks during the tests. Let's start by defining a basic DataSource
interface that contains the methods that will be used concurrently:
interface DataSource { fun getNameAsync(id: Int): Deferred<String> fun getAgeAsync(id: Int): Deferred<Int> fun getProfessionAsync(id: Int): Deferred<String> }
Now, let's define a data class that will be the return type of the functionality we want to test. In this case, we just need a simple class:
data class User( val name: String, val age: Int, val profession: String )
And finally, let's add UserManager
, which has the functionality that we want to test. This class contains a method that...