When writing tests in Kotlin, prefer Kotlin-specific testing libraries and language features over their Java equivalents. Use Mockito Kotlin instead of standard Mockito for more idiomatic syntax, and leverage Kotlin language features like `apply` blocks to make test setup code more concise and readable.
When writing tests in Kotlin, prefer Kotlin-specific testing libraries and language features over their Java equivalents. Use Mockito Kotlin instead of standard Mockito for more idiomatic syntax, and leverage Kotlin language features like apply
blocks to make test setup code more concise and readable.
For example, instead of:
import org.mockito.Mockito.verify
// ...
val field = OkHttpClientProvider::class.java.getDeclaredField("sClient")
field.isAccessible = true
field.set(null, null)
Use:
import org.mockito.kotlin.verify
// ...
val field = OkHttpClientProvider::class.java.getDeclaredField("sClient").apply {
isAccessible = true
set(null, null)
}
This approach improves test code readability, reduces boilerplate, and takes advantage of Kotlin’s expressive syntax to make tests more maintainable.
Enter the URL of a public GitHub repository