October 2019
Intermediate to advanced
434 pages
11h 54m
English
By testing our Kotlin code from Java, we can gain a better understanding of how the interop works between the two languages. This can shed some light on where and how we may want to modify our Kotlin APIs to be more friendly vis-à-vis Java clients.
Let's look at one example. In this code snippet, we've added a factory method to our Student class via a companion object:
data class Student(val firstName: String, val lastName: String) { companion object { fun createDefaultStudent() = Student("Jane", "Doe") } }
Then, we can validate the behavior of that factory method using tests written in Java or Kotlin. In the following code, we've written a test in Java to validate the behavior of createDefaultStudent():
Read now
Unlock full access