Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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?

Danny Preussler

April 21, 2017
Tweet

More Decks by Danny Preussler

Other Decks in Technology

Transcript

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

    flickr.com/photos/84935187@N04/16171765851
  2. @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. @PreusslerBerlin @Test fun `should ignore scroll if nothing moved`() {

    tested.setOverScrollListener(scrollListener) tested.onScroll(START_SCROLL, START_SCROLL) verify(scrollListener, never()).onStopped() }
  4. @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. @PreusslerBerlin @Test fun `should ignore scroll if nothing moved`() {

    tested.setOverScrollListener(scrollListener) tested.onScroll(START_SCROLL, START_SCROLL) verify(scrollListener, never()).onStopped() }
  6. @PreusslerBerlin Class CalculatorTest @Nested @DisplayName(”on addition") class addition { @Test

    @DisplayName(”should return the sum ") fun test() {…} @Nested @DisplayName(”on substraction") class substraction{ … }
  7. @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. @PreusslerBerlin SimpleSpec: Spek({ describe("a calculator") { val calculator = SampleCalculator()

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