The code is based on a tutorial from JetBrains. Below we see an example of this mechanism for the JVM. Besides functions, this mechanism also works for other constructs such as classes, enums, and annotations. With this mechanism, a common source set defines an expected declaration, and platform-specific source sets provide the actual declaration corresponding to the expected declaration and the actual implementation. Kotlin provides a mechanism of expected and actual declarations to tackle this situation. We could roll our own implementation or look for an open-source library, but some of our target platforms already provide this functionality out of the box. For instance, Kotlin’s multiplatform standard library does not provide a way to encode a string in base64. Sometimes, there are functions that we want to use in our common code, but they can be implemented much more efficiently with platform-specific code. To be able to use the JUnit5 features, we must first add the dependencies: testImplementation(':junit-jupiter-api:5.8.2') testImplementation(':junit-jupiter-engine:5.8. The cool part however is that this function can now be used in all possible platforms, which can be a huge bonus if we write all the business logic here. Basics on testing with JUnit5 Setup For the Gradle users, heres the setup. Import io.Īs we can see, testing common code works the same as in ‘normal’ Kotlin projects. We can provide the test code in `commonTest`: package nl.bjornvanderlaan.factorial Below we see an example where commonTest defines Kotest dependencies used for all platforms, and the JVM-specific dependencies are added to the jvmTest source set.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |