Kotlin all your tests (Mobius 2017)

Kotlin all your tests (Mobius 2017)

Kotlin was the rising star of programming languages in 2016. It not only became final with version 1.0 but also got rapidly adopted in the android community. But many companies are struggling to add Kotlin to an existing Java project. It's not easy to add a new language to an existing team. But it's much easier to introduce Kotlin to your project using the backdoor: your unit tests. So let's look at Kotlin more from a testing perspective. How does it look like to write tests in Kotlin, what possibilities of the language will help us?

A8b79d304b5184e5a5b0a109590f6683?s=128

Danny Preussler

April 21, 2017
Tweet

Transcript

  1. 9.

    @PreusslerBerlin Fly into new horizons ! (c) Sunny M5, CC

    flickr.com/photos/84935187@N04/16171765851
  2. 31.

    @PreusslerBerlin String jsonText = "{\r\n \"deal\" : {\r\n \"isTipped\" :

    true,\r\n \"id\" : \"D- Company-241907\",\r\n \"areas\" : [],\r\n \"smallImageUrl\" : \"http://uat- static.groupon.de/00/00/small0000.jpg\",\r\n \"finePrint\" : \"MULTI DEAL ext Codes\",\r\n \"says\" : {\r\n \"emailContent\" : \"\",\r\n \"id\" : \"\",\r\n \"websiteContent\" : \"\",\r\n \"title\" : \"\",\r\n \"websiteContentHtml\" : \"\",\r\n \"emailContentHtml\" : \"\"\r\n },\r\n \"endAt\" : \"2014-03-31T22:59:00Z\",\r\n \"shortAnnouncementTitle\" : \"D-Company: 50% sparen in QA_AutoTestCity2\",\r\n \"placeholderUrl\" : \"http://assets2.grouponcdn.com/images/groupon/grayPlaceholder.png\",\r\n \"shippingAddressRequired\" : false,\r\n \"type\" : \"groupon\",\r\n \"isSoldOut\" : false,\r\n \"priority\" : 0,\r\n \"options\" : [\r\n {\r\n \"status\" : \"open\",\r\n \"title\" : \"MULTI DEAL ext Codes Option1\",\r\n \"isLimitedQuantity\" : false,\r\n \"tax\" : {\r\n \"amount\" : 16,\r\n \"currencyCode\" : \"EUR\",\r\n \"formattedAmount\" : \"0,16 \u20AC\"\r\n },\r\n \"minimumPurchaseQuantity\" : 1,\r\n \"expiresInDays\" : 62,\r\n \"discount\" : {\r\n \"amount\" : 100,\r\n \"currencyCode\" : \"EUR\",\r\n
  3. 40.

    @PreusslerBerlin @Test fun `should ignore scroll if nothing moved`() {

    tested.setOverScrollListener(scrollListener) tested.onScroll(START_SCROLL, START_SCROLL) verify(scrollListener, never()).onStopped() }
  4. 41.

    @PreusslerBerlin fun ScrollContainer.onScroll(started : Float, end: Float) { whenever(actionDown.action) .thenReturn(MotionEvent.ACTION_DOWN)

    whenever(actionDown.y) .thenReturn(started) whenever(actionMove.action) .thenReturn(MotionEvent.ACTION_MOVE) … onTouchEventInternal(actionDown) onTouchEventInternal(actionMove) onTouchEventInternal(actionUp) }
  5. 42.

    @PreusslerBerlin @Test fun `should ignore scroll if nothing moved`() {

    tested.setOverScrollListener(scrollListener) tested.onScroll(START_SCROLL, START_SCROLL) verify(scrollListener, never()).onStopped() }
  6. 79.

    @PreusslerBerlin Class CalculatorTest @Nested @DisplayName(”on addition") class addition { @Test

    @DisplayName(”should return the sum ") fun test() {…} @Nested @DisplayName(”on substraction") class substraction{ … }
  7. 81.

    @PreusslerBerlin SimpleSpec: Spek({ describe("a calculator") { val calculator = SampleCalculator()

    on("addition") { val sum = calculator.sum(2, 4) it("should return the sum") { assertEquals(6, sum) } } } }
  8. 83.

    @PreusslerBerlin SimpleSpec: Spek({ describe("a calculator") { val calculator = SampleCalculator()

    on("addition") { ... } on(”substraction") { ... } } describe("a broken calculator") {… }