when is called whenever, but aside from that, almost everything should be similar. Mockito has a Kotlin variation of the library, you can get the dependency: :mockito-kotlin to get some better support than the Java one. If you are using Mockito, you can realize that the function when() is now invalid because it is a reserved keyword in Kotlin. JsonPropertyĭata class Forecast ( ( "forecastday" ) val days : List ) data class ForecastDay ( val date : LocalDate, ( "day" ) val temperature : Temperature, ) data class Location ( ( "name" ) val city : String, val region : String, val country : String, ) data class Temperature ( ( "maxtemp_c" ) val maxC : Double, ( "maxtemp_f" ) val maxF : Double, ( "mintemp_c" ) val minC : Double, ( "mintemp_f" ) val minF : Double, ( "avgtemp_c" ) val avgC : Double, ( "avgtemp_f" ) val avgF : Double, ) Testing The next step is now to upgrade our project to Kotlin. And thanks to the extension functions, you can even get additional methods on your Java types. For example, you don't need to use stream() whenever you want to do something from your Collections. The standard library of the language has many, many more convenient functions if we compare it to Java. You can then extend the functionality of classes that you don't own. Kotlin provides the ability to create functions from a receiver without inheriting the class. Knowing what can and what cannot change, it helps to reduce the amount of side effects produced. You can use val to mark your variables as constant references. ImmutabilityĮverything is immutable in Kotlin by default: the collections are immutable unless you specify otherwise. The compiler can know at compile time if a NullPointerException could happen, and will refuse to compile if such a case isn't handled. One major feature is that every type has the variation nullable and not nullable. Kotlin has many interesting features, some of them are even copied by Java in the latest versions. It also means that we can keep Java frameworks and libraries, so no other change is required there. The compiled code will be normal Java byte code, so it is even possible to have both Kotlin and Java files in the same project. Interestingly enough, Kotlin is a multiplatform language, initially used for Android development, but it is getting now more and more used as a backend language. You read it in the title, this is of course Kotlin. That upgrade is too interesting to skip, even if it means that we have to change the programming language! We could stop everything right here, but there is one possible upgrade that we can do at this point. We even have a safeguard to detect blocking calls, to verify the non-blocking nature of the code at runtime. Reactive with KotlinĪs of now, we have a reactive application, throughout the whole chain. In the second part, we will explore coroutines, and how we can use them. In the first part, we will focus on Kotlin and adapting the project to the language. We expect to start from a Spring Boot reactive project using Reactor, written in Java, see part 1. In this second part of the article, we will show that we can use Kotlin coroutines to have a reactive application while writing code in direct style.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |