Kotlin unit test (KAD22) and kotlinkad22 on Android

Source: Internet
Author: User

Kotlin unit test (KAD22) and kotlinkad22 on Android

By Antonio Leiva

Time: Apr 25,201 7

Link: https://antonioleiva.com/unit-tests-android-kotlin/

 

 

Of course,KotlinIt also allows us to perform unit tests in a very simple way.It is very similar to what we do in Java.

 

 

When we use the Mockito library, there are some small complexities, but we will see some tips to make it easier.

 

Kotlin unit test

 

Although a single test question is always controversial, I do not intend to detail it here.

 

 

For our example, it is enough to realize that unit tests do not require devices to run. The IDE can execute them, display the results, and identify what is executed and what is failed.

 

Configure Gradle

 

You need to add jUnit to your dependency. When creating a project, this is the default configuration and may already be included. We need to add Mockito, which will be used later.

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

 

Create the first test

 

 

In the app/src/test folder (if it does not exist, you can create it), you can create a new class named MyTest, which is similar to this:

1 class MyTest {2 3     @Test4     fun testsWork() {5         assertTrue(true)6     }7 }

 

As you can see, it is very similar to what we use in Java.

 

How to Use Mockito

 

In Kotlin, although you may find it complicated to use Mockito, you can still use Mockito like other libraries. We need to solve these complex points.

 

This is an example in the book:

 1 @Test  2 fun emptyDatabaseReturnsServerValue() { 3     val db = Mockito.mock(ForecastDataSource::class.java) 4     val server = Mockito.mock(ForecastDataSource::class.java) 5     `when`(server.requestForecastByZipCode(any(Long::class.java), any(Long::class.java))) 6             .then { ForecastList(0, "city", "country", listOf()) } 7  8     val provider = ForecastProvider(listOf(db, server)) 9     assertNotNull(provider.requestByZipCode(0, 0))10 }

 

You see, everything is very similar. You can create your own mock and use them seamlessly throughout the code. You can also use 'mockitojunitrunner 'and annotations.

 

The when word is a reserved word of Kotlin, so you need to use a reverse Comma. You can even use import to rename it and give it the required name:

1 import org.mockito.Mockito.`when` as _when

 

When you enter the mock type, the problem occurs. It does not allow null values.By default,MockitoFor mockNullValue. This problem will happen sooner or later..

 

 

One technique can be used for this: mockit-kotlin, which does not need to be null. Instead, it gives a specific value for each type by default to solve this problem. In addition,It also utilizesKotlinProvides other functions to make things easier..

 

 

Another problem is that by default, all classes and functions of Kotlin are disabled, that is, they cannot be extended. Because they cannot be simulated, This Is A Mockito problem.

 

But this is no longer a problem. Mockito 2 allows simulating the final object.

 

In subsequent articles, we will read how to use it.

 

An interesting little thing

 

 

Kotlin allows us to name functions more flexibly than Java.If we reverse the comma, we can put any text we think of in it..

 

 

This is very useful for testing. The most important thing is that the test name perfectly describes what you want to do so that you can be more standardized.

 

 

Therefore, you can have a test with the following name:

 

1 @Test fun `test something works as expected`() {2     Assert.assertTrue(false)3 }

 

 

In addition to improving readability, it is best to output results that are easier to understand when a test fails. You will see a clearer error.

 

 

If you use it in an Android project,You will seeLintError indicating AndroidProject methods cannot contain spaces. In my tests, I didn't notice that this could be a problem. There is no problem with Gradle running them, so you can add comments to ignore errors.

 

 

In any case, remember to use only for testing.

 

Conclusion

 

 

Theoretically, in Java, the testing tool we can use should be fine in Kotlin, but adding null to code based on reflection brings us some problems.

 

KotlinPay close attention to invalid code, which may be a sticking point in some cases.. However, there are more and more alternative methods that can be implemented in a simple way, and all these problems will disappear compared with Mockito 2.

 

 

In addition to these small defects,Everything elseJavaSame.

 

 

Want to continue learning Kotlin? Read all my articles on this topic.

 

 

 

 

 

 

 

 

 

 

 

 

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.