buildscript { ext.kotlin_version = '1.1-M01' ext.kotlin_gradle_version = '1.1-M01' ext.mokito_kotlin_version = '0.6.0' ext.spek_version = '1.0.89' ext.junit_version = '5.0.0-ALPHA' ext.junit_platform_gradle_plugin_version = '1.0.0-M2' ext.hamkrest_version = '1.2.2.0' repositories { mavenCentral() maven { url "https://dl.bintray.com/kotlin/kotlin-eap-1.1" } maven { url "https://dl.bintray.com/jetbrains/spek/" } } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_gradle_version" classpath "org.junit.platform:junit-platform-gradle-plugin:$junit_platform_gradle_plugin_version" } } group 'com.rsk' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'kotlin' apply plugin: 'org.junit.platform.gradle.plugin' junitPlatform { engines { include 'spek' } } sourceCompatibility = 1.8 repositories { jcenter() maven { url "https://dl.bintray.com/kotlin/kotlin-eap-1.1" } maven { url "https://dl.bintray.com/jetbrains/spek/" } } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" testCompile "org.junit:junit5-api:$junit_version" testCompile "org.jetbrains.spek:spek-api:$spek_version" testCompile "com.nhaarman:mockito-kotlin:$mokito_kotlin_version" testCompile "com.natpryce:hamkrest:$hamkrest_version" runtime "org.junit.platform:junit-platform-launcher:$junit_platform_gradle_plugin_version" runtime "org.jetbrains.spek:spek-junit-platform-engine:$spek_version" }
package com.rsk import com.nhaarman.mockito_kotlin.mock import com.nhaarman.mockito_kotlin.verify import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.describe import org.jetbrains.spek.api.dsl.it import org.junit.gen5.api.Assertions class CalculatorSpek : Spek({ var calculator: Calculator? = null describe("the calculator") { beforeEach { calculator = Calculator(NullResult()) } it("should add two numbers") { val result = calculator?.add(12, 13) Assertions.assertEquals(25, result) } it("should accumulate one number") { calculator?.accumulate(23) Assertions.assertEquals(23, calculator?.total) } it("should accumulate two numbers") { calculator?.accumulate(2) calculator?.accumulate(3) Assertions.assertEquals(5, calculator?.total) } } describe("the output should be written correctly") { val result : Result = mock() val calculator = Calculator(result) it("should write the output amount") { calculator.accumulate(23) verify(result).write(23) } } })
package com.rsk class Calculator(val output : Result) { var total = 0 fun add(x: Int, y: Int): Int { return x + y } fun accumulate(x: Int) { total += x output.write(total) } } interface Result { fun write(answer: Int) } class NullResult : Result { override fun write(answer: Int) { } }