Retrofit Test Rule
JUnit test rule for testing retrofit services
Configuration
testImplementation("io.github.thomsontomy:testrule:0.4.0")
Content copied to clipboard
Example
Imagine you have an awesome retrofit service class like below
interface GitHubService {
@GET("users/{user}/repos")
suspend fun listRepos(@Path("user") user: String): List<GithubRepo>
}
Content copied to clipboard
The integration test would look like this
class GitHubServiceTest {
@get:Rule
val retrofitTestRule = RetrofitTestRule(GitHubService::class.java, logLevel = LogLevel.BODY)
@Test
@RestRequest(
validations = RequestValidations(
path = "/users/my_user/repos",
method = "GET"
), // Validations on the request
responseOptions = ResponseOptions(bodyText = "[]") // Response configurations
)
fun testListRepos() = runTest {
// Get the service instance
val service = retrofitTestRule.getService()
// Make the API call
service.listRepos("my_user")
}
}
Content copied to clipboard
Under the hood
This rule makes use of OkHttp's MockWebServer and creates a mock server for the retrofit service to use.