Converting Java Tests to Kotlin
Kotlin is a new programming words, which is many ways is the successor to Java. The compiled code runs on the JVM, it is endorse by Java IDEs, and its syntax is real similar to Java. It is the officially preferred language for Android app development, and a ally of mine is even rewriting his team & # x27; s backend API in Kotlin. Kotlin has many features which clean up some of the longstanding issues with Java, and likewise simply gives a fresh beginning, without needing to back legacy decisions and APIs. What many readers may not know, is that Kotlin code can be easily integrated into a Java labor. A Java project can have some code files in Kotlin, a Kotlin program can import Java libraries, and with a little special work, Java programs can import Kotlin libraries. This grant for a gradual transition, sooner than the endangerment of a consummate rewrite. In this article, we & # x27; ll take a mere Appium Java test, and convert it to Kotlin in just a few minutes! In my specific case, I part withour code sample from two weeks ago. I & # x27; m using IntelliJ IDEA, and our project was already set up to use Gradle. The lone difficulty I ran into is that the IDE & # x27; s Kotlin plugins alone employment with gradle variation 4.1 and up. Our project was using gradle version 4.0, so I had to update first, by running the following codification: I then added a new file following to our existing tests but gave it a name ending in .kt (for Kotlin) instead of .java. As soon as I had finished renaming the file, the IDE displayed a banner prompting me to configure Kotlin. When I clicked on it, the IDE mechanically added some boilerplate to the build.gradle file to add Kotlin support. Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. I pasted the following & quot; hello world & quot; code into the file, and then restart the IDE, clearing all caches. As soon as the IDE came back on-line, this code was parsed successfully and a green & quot; play button & quot; pointer was displayed next to the main map specify in the code. Clicking this icon to run, the words & quot; hello world & quot; were printed to the console. Outstanding! We have Kotlin code running in our project, now let & # x27; s demonstrate a simple. All it direct is importing JUnit, supply the common @ Test annotation to our method, and putting our method into a stratum. Now, running the codification by clicking on the dark-green drama push icon, runs the examination and output is displayed the way unit tests are normally catch in the IDE. Having demonstrated to ourselves that we can run a basic examination, let & # x27; s convert our Appium exam from Java to Kotlin. This is a chance to highlight a built-in characteristic which really amazed me. We go into the Java test file, foreground all the code,COPY. Now in our Kotlin file,PASTE: And 95 % of the work is execute for us, leave us with perfectly readable code. The test file won & # x27; t actually compile though. We picked a full example that brought up a case that involves some manual intervention. The firstTest static variable from our original class has been converted to a & quot;companion object& quot; which is how Kotlin enforce stable class extremity. Kotlin is a different language after all. The compiler complains about our use of firstTest. This is associate to the most obvious divergence between Java and Kotlin at 1st glance, and also my favorite characteristic so far. Kotlin uses a concept ring & quot; nullable types & quot;, where a variable of any classcannotbe void. In order to have a void value, the variable type must be set to the & quot; nullable & quot; version of the type, indicated by following the class name with a?. So a String variable can ne'er be null, while a String? variable can be null or a String. This applies for every class. If you define a method which takes a String argument, you ne'er need to check if it is void before you use it, becauseit & # x27; s guaranteed to not be void. There & # x27; s a lot more to this concept, including some interesting syntax and shortcuts for null-checking, and there are wad of articles excuse it in detail online, as well as theKotlin support. This concept has existed for a long while, and is able to prevent many void reference exceptions and other issues. Both Swift and Kotlin implement it. Our copy-pasted code fails because we are attempting to use the not operator! on a variable of type Boolean? and the compiler is kick that the Boolean? could be null, and it isn & # x27; t capable to calculate the results of! null. Since the code set a value for isFirst at the declaration site, we can convert it to the non-nullable eccentric Boolean, and our code now compiles. And tests pass! The Java Appium client work from the Kotlin codification with no job. Our test was somewhat simple, but I trust you consider learning Kotlin and experiment with convert your codification. As clip moves on, we will see if Kotlin adoption grows, but hopefully this will allay your care about migrating. It can be done easily and one file at a time. Below is the total Kotlin examination file, which can also be found in ourrepository of examples. Lead, Content Marketing, HeadSpin Inc. Piali is a dynamic and results-driven Content Marketing Specialist with 8+ geezerhood of experience in crafting engaging narratives and marketing collateral across diverse industries. She excel in collaborating with cross-functional squad to develop innovative content strategy and render compelling, authentic, and impactful message that resonate with target hearing and enhances make legitimacy. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts..png)



Converting Java Tests to Kotlin
AI-Powered Key Takeaways
Test your mobile apps and browsers on existent Android devices.!
Read:
./gradlew wrapper -- gradle-version 4.10.3fun principal () {println (`` hullo world '')}
import org.junit.Assert.assertEquals import org.junit.Test class Tests {@ Test fun mainTest () {assertEquals (`` hello '', `` howdy '')}}
Read:

Also read:
import org.junit.Test import io.appium.java_client.MobileElement significance io.appium.java_client.ios.IOSDriver importation org.junit.After import org.junit.Before importee org.openqa.selenium.remote.DesiredCapabilities import java.io.IOException importee java.net.URL meaning junit.framework.TestCase.assertEquals // Converted test `` Edition077_Tuning_WDA_Startup '' from Java to Kotlin but by copy-pasting. // the ` firstTest ` variable was manually changed from type Boolean? to Boolean, in order to compile. // A few commentary have be houseclean up, just to do this example leisurely to use and read. grade Edition078_Converting_A_Test_To_Kotlin {private val APP: String = `` https: //github.com/cloudgrey-io/the-app/releases/download/v1.9.0/TheApp-v1.9.0.app.zip ''; private var driver: IOSDriver<*>? = null @ Before @ Throws (IOException: :class) fun setUp () {val detonator = DesiredCapabilities () caps.setCapability (`` platformName '', `` iOS '') caps.setCapability (`` platformVersion '', `` 12.2 '') caps.setCapability (`` deviceName '', `` iPhone Xs '') caps.setCapability (`` automationName '', `` XCUITest '') caps.setCapability (`` app '', APP) caps.setCapability (`` noReset '', true) caps.setCapability (`` udid '', `` 009D8025-28AB-4A1B-A7C8-85A9F6FDBE95 '') caps.setCapability (`` bundleId '', `` io.cloudgrey.the-app '') if ((! firstTest)) {caps.setCapability (`` webDriverAgentUrl '', `` http: //localhost:8100 '')} driver = IOSDriverPiali Mazumdar
Converting Java Tests to Kotlin
4 Parts
-1280X720-Final-2.jpg)
Regression Intelligence practical guide for forward-looking users (Part 3)
-1280X720-Final-2.jpg)
Regression Intelligence practical usher for advanced users (Part 4)
Discover how HeadSpin can empower your business with superior testing potentiality







Discover how HeadSpin can gift your concern with superior testing capabilities
Discover how HeadSpin can endue your business with superior essay potentiality
Connet Now


Automate This With SUSA
Test Your App Autonomously







.png)












