Testing the network and database layers
In this section, we are going to learn how to write tests for our network and database layers step by step.
Testing the network layer
To test our network layer, we will write unit tests. However, since we are using Retrofit to make our network requests, we will use MockWebServer
to mock our network requests. The MockWebServer
is a library that allows us to mock our network requests. Let us start by setting up the test dependencies in our version catalog:
- Open the
libs.version.toml
file and add the following versions in the versions section:mockWebServer = "5.0.0-alpha.2" coroutinesTest = "1.7.3" truth = "1.1.5"
We are setting up the versions for
mockWebServer
,coroutinesTest
, andtruth
. - Next, in the libraries section, add the following:
test-mock-webserver = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "mockWebServer" } test-coroutines = { module = "org.jetbrains...