[Kotlin Unit test] Spek & Mock
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) { } }
分类:
Kotlin
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-10-18 [Flutter & Dart] Await a Future void function
2017-10-18 [AngularFire] Resolve snapshotChanges doesn't emit value when data is empty
2016-10-18 [CSS] Use CSS Counters to Create Pure CSS Dynamic Lists
2016-10-18 [CSS] Target empty elements using the :empty pseudo-class
2016-10-18 [Angular2 Animation] Basic animation
2016-10-18 [Angular2 Router] Auxiliary Routes bit by bit
2015-10-18 [Javascript] Querying an Immutable.js Map()