{"id":5771,"date":"2018-01-07T18:57:37","date_gmt":"2018-01-07T18:57:37","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5771"},"modified":"2018-01-07T18:57:37","modified_gmt":"2018-01-07T18:57:37","slug":"unit-testing-your-kotlin-code","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/unit-testing-your-kotlin-code\/","title":{"rendered":"Unit testing your Kotlin code"},"content":{"rendered":"<p>Let&#8217;s start with our simple little Calculator class<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot\r\n\r\nclass Calculator {\r\n    fun add(a : Int, b : Int): Int{\r\n        return a + b\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Creating our unit test(s)<\/strong><\/p>\n<p>Add a Kotlin class file and copy the following code into it<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot\r\n\r\nimport kotlin.test.assertEquals\r\nimport org.junit.Test as test\r\n\r\nclass TestCalculator() {\r\n    @test fun testAdd() {\r\n        val c = Calculator()\r\n\r\n        assertEquals(c.add(5, 2), 7)\r\n    }\r\n}\r\n<\/pre>\n<p>IntelliJ will help us add the dependencies, but if you need to add them manually, select File | Project Structure and in the Project Settings | Modules we need to add JUnit4.<\/p>\n<p>Obviously we&#8217;re able to import Java compiled code into our project as Kotlin and Java share the JVM bytecode, so in this instance we&#8217;re using JUnit&#8217;s Test annotation (renaming to test for this example), we&#8217;re also using Kotlin, test assertions.<\/p>\n<p>That&#8217;s all there is to it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s start with our simple little Calculator class package com.putridparrot class Calculator { fun add(a : Int, b : Int): Int{ return a + b } } Creating our unit test(s) Add a Kotlin class file and copy the following code into it package com.putridparrot import kotlin.test.assertEquals import org.junit.Test as test class TestCalculator() { @test [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[196],"tags":[],"class_list":["post-5771","post","type-post","status-publish","format-standard","hentry","category-kotlin"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5771"}],"version-history":[{"count":1,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5771\/revisions"}],"predecessor-version":[{"id":5772,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5771\/revisions\/5772"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}