Kotlin Unit test on Android (KAD22)

Source: Internet
Author: User

Antonio Leiva

Time:APR

Original link:https://antonioleiva.com/unit-tests-android-kotlin/

of course, Kotlin and allows us to do unit testing in a very simple way. , it's very similar to our Java in the same way.

when we use to Mockito Library When there are some small complications, however, we will see some tricks to make it easier.

unit tests for Kotlin

Although the single test topic is always controversial, I do not intend to elaborate on this here.

for our example, it is sufficient to realize that unit testing is a test that does not require the device to run. the IDE is able to execute them and display the results, identifying which ones were executed and which failed.

ConfigurationGradle

you need to add jUnit into your dependencies. When you create a project, it may already be included because it is the default configuration. We're going to add mockito, which we'll use in the back.

1 testcompile "junit:junit:4.12"2 testcompile "org.mockito:mockito-core:1.10.19"

Create the first Test

in the in the app/src/test folder (if it doesn't exist, you can create it), you can create a New class called MyTest, which looks like this:

1 class MyTest {23    @Test4fun    testswork () { 5         Asserttrue (true)6    }7 }

as you can see, it's very similar to our as used in Java.

How to useMockito

in the Kotlin , although you may find it somewhat complicated to use Mockito, you can still use it like other libraries Mockito. These are complicated points that we need to solve.

this is Examples in the book :

1 @Test2 Fun Emptydatabasereturnsservervalue () {3Val db = Mockito.mock (Forecastdatasource::class. Java)4Val Server = Mockito.mock (forecastdatasource::class. Java)5' When ' (Server.requestforecastbyzipcode (Long::class. Java), any (Long::class. Java )))6. then {forecastlist (0, "City", "Country", Listof ())}7 8Val Provider =Forecastprovider (listof (DB, server))9Assertnotnull (provider.requestbyzipcode (0, 0))Ten}

you see, all of them are very similar. You can create your own mocksanduse them seamlessly throughout your code, and you can also use ' Mockitojunitrunner ' and annotations.

when Word is Kotlin the reserved word, so you need to use the invert comma, even you can rename it with import , give it the desired name:

1 import Org.mockito.Mockito. ' When ' as _when

When you enter Mock type, the problem occurs and it does not allow NULL values. By default,Mockito specifies null for a mock object value, this problem will occur sooner or later .

There is a technique that can be used for this: Mockit-kotlin , it does not NULL , but the default is to give a specific value for each type to solve this problem. In addition, it uses The advantages of Kotlin to provide other functions that make things easier .

Another problem is that, by default, All classes and functions of Kotlin are closed, that is, they cannot be extended. Since they cannot be simulated, this is a mockito problem.

but this is no longer a problem, Mockito 2 allows the simulation of the final object.

In the next article, we read how to use it.

an interesting little thing.

Kotlin allow us to compare Java more flexible naming functions. If we reverse the comma, we can put any text we think of in it .

This is very useful for testing, the most important of which is that the test name perfectly describes what you want to do to be more prescriptive.

Therefore, you can have a test with such a name:

1 @Test fun ' Test something works as expected ' () {2     Assert.asserttrue (false)3 }

In addition to improving readability, it is best to output more understandable results when the test fails. You will see a clearer error.

if the Android use it in the project, you'll see it shows a Lint error, indicating Android The project method cannot have spaces . in my test, I didn't notice that this could be a problem. Gradle run them without problems, so you can add comments to ignore the errors.

In any case, remember to test only.

Conclusion

Although in theory, In Java , we can use the test tool in Kotlin , there should be no problem, but based on reflection and adding null to the code It does bring us some problems.

Kotlin very concerned about invalid code, which in some cases may be a sticky point . However, there are more and more alternatives that can be done in a simple way, and all of these problems will disappear compared to Mockito 2 .

In addition to these minor flaws, everything else is with Java same .

want to continue to learn Kotlin? just check out all the articles I wrote about the main topic .

Kotlin Unit test on Android (KAD22)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.