标签: kotlintest

kotlin-test 与 kotlin-test-junit

我有一个 kotlin 项目,已编译为 java。我的测试库是junit。我使用 Maven 作为我的依赖管理和 Intellij IDEA。最近我在我的 pom 中收到了这个奇怪的警告。

检查信息:如果您有 kotlin-test 和 junit 依赖项,那么很可能您最好包含 kotlin-test-junit 而不是仅仅包含 kotlin-test

kotlin-test 和 kotlin-test-junit 有什么区别?

从我读到的内容看来 kotlin-test 并没有被弃用,那么为什么 intellij 推荐 kotlin-test-junit 而不是 kotlin-test 呢?

在此输入图像描述

junit kotlin kotlintest

13
推荐指数
1
解决办法
3278
查看次数

是否有AsotJ库的Kotlin等价物?

我正在将一些测试从Java转换为Kotlin.对于Java测试,我使用AssertJ库,它非常强大并且具有丰富的断言集.我的问题是,对于Kotlin测试我不能使用AssertJ和Kotlin JUnit(org.jetbrains.kotlin:kotlin-test-junit)具有非常有限的断言集.

是否存在与AssertJ等效的Kotlin或更好的断言方式?

我找到了Kluent库,但我仍然不确定这是否是最好的库.

assertions kotlin kotlintest

11
推荐指数
2
解决办法
3860
查看次数

如何使用Gradle运行kotlintest测试?

从Intellij启动时,kotlintest测试运行良好,但是当我尝试使用gradle test task命令运行它们时,仅发现并运行了我的常规JUnit测试。

kotlintest代码:

import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.StringSpec

class HelloKotlinTest : StringSpec() {
    init {
        println("Start Kotlin UnitTest")

        "length should return size of string" {
            "hello".length shouldBe 5
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

build.gradle:

apply plugin: 'org.junit.platform.gradle.plugin'

buildscript {
    ext.kotlinVersion = '1.1.3'
    ext.junitPlatformVersion = '1.0.0-M4'

    repositories {
        maven { url 'http://nexus.acompany.ch/content/groups/public' }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "org.junit.platform:junit-platform-gradle-plugin:$junitPlatformVersion"
    }
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin'
    test.kotlin.srcDirs += 'test/main/kotlin'
}

(...) 

dependencies {
    // Kotlin
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jre8', version: kotlinVersion …
Run Code Online (Sandbox Code Playgroud)

junit kotlin junit-runner build.gradle kotlintest

6
推荐指数
1
解决办法
4703
查看次数

如何使用Gradle Kotlin DSL运行kotlintest测试?

我想要什么?

我想使用kotlintest运行测试,并且单击了测试类旁边的图标,从而成功地从IntelliJ运行了它们。我的项目中也有JUnit 5测试。我现在开始使用Gradle Kotlin DSL,并且设法运行了check执行JUnit 5任务的Gradle 任务。

问题是什么?

kotlintest测试未运行!似乎它没有被Gradle发现,因为失败的测试使Gradle任务成功。所以,

如何同时使用Gradle Kotlin DSL和JUnit 5运行kotlintest测试?

我尝试了什么?

如何使用Gradle运行kotlintest测试?基本上是问题的旧版本,但是由于使用了不同的技术:JUnit 4和Gradle而不是Gradle Kotlin DSL,已经过时了。我可以找到的所有其他问题是针对JUnit 4还是不带kotlintest的JUnit 5

项目设置

我正在使用Gradle 4.6。我的测试是

import io.kotlintest.matchers.exactly
import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.FunSpec

class KotlinTest: FunSpec() {

    init {
        testCalculate()
    }

    /** This failing test will not fail the Gradle check. */
    fun testCalculate() = test("one plus one is two") {
        (1+1).toDouble() shouldBe exactly(42.0)
    }

}
Run Code Online (Sandbox Code Playgroud)

我的build.gradle.kts

import org.gradle.api.plugins.ExtensionAware

import org.junit.platform.gradle.plugin.FiltersExtension …
Run Code Online (Sandbox Code Playgroud)

gradle kotlin junit5 kotlintest gradle-kotlin-dsl

6
推荐指数
2
解决办法
2107
查看次数

在 KotlinTest 中 shouldBe 和 shouldBe 之间有什么区别?

这是使用 KotlinTest 1.3.5 的测试代码。

val expect = 0.1
val actual: Double = getSomeDoubleValue() 
actual shouldBe expect
Run Code Online (Sandbox Code Playgroud)

并且在运行代码时打印了此警告。

[警告] 比较双打时考虑使用容差,例如:a shouldBe b plusOrMinus c

在这种情况下,我不想使用plusOrMinus. 所以,我将代码固定为

val expect = 0.1
val actual: Double = getSomeDoubleValue() 
actual shouldBe exactly(expect)
Run Code Online (Sandbox Code Playgroud)

现在,没有警告。
不过,我想知道的区别shouldBeshouldBe exactly。它是什么?

kotlin kotlintest

5
推荐指数
1
解决办法
1693
查看次数

How can I initialize variable before each test using kotlin-test framework

I'm trying to find a way to set up variable before each test. Just like the @Before method in Junit. Go through the doc from kotlin-test, I found that I can use interceptTestCase() interface. But unfortunately, the code below will trigger exception:

kotlin.UninitializedPropertyAccessException: lateinit property text has not been initialized

class KotlinTest: StringSpec() {
lateinit var text:String
init {
    "I hope variable is be initialized before each test" {
        text shouldEqual "ABC"
    }

    "I hope variable is be initialized before …
Run Code Online (Sandbox Code Playgroud)

kotlin kotlintest

5
推荐指数
1
解决办法
1538
查看次数

尝试在 Intellij 2018.2.RC 中执行 JUnit 4 测试时出现 NoClassDefFoundError

当尝试在 Android 中执行一个非常简单的 Hello World 测试时,使用 KotlinTest:

class ExampleUnitTest : FreeSpec() {

init {
    "Test" {
        2 + 2 shouldBe 4
    }
   }
}
Run Code Online (Sandbox Code Playgroud)

尝试在 IntelliJ 中执行此操作时,通过单击绿色图标,我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:45)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.TestExecutionListener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... …
Run Code Online (Sandbox Code Playgroud)

android intellij-idea gradle kotlin kotlintest

5
推荐指数
1
解决办法
2127
查看次数

使用 springmockk 时 Kotlintest 不执行测试

我试图为我的 kotlin spring 应用程序编写一个集成测试。为此,我使用了 kotlintest 框架。因为我需要在我的应用程序中模拟其中一个 bean,所以我还添加了带有 springmockk 扩展的 mockk。添加 springmockk 扩展后,不再执行测试。

我注意到只要将 springmockk 添加到 gradle testImplement 依赖项中就会发生这种情况,它甚至不必导入到应用程序代码本身中。

buildscript {
    ext.kotlin_version = '1.3.21'
    ext.kotlintestVersion='3.4.2'
    ext.spring_boot_version='2.1.4.RELEASE'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBoot_version")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
    }
}

...

dependencies {
    ...
    testImplementation("org.springframework.boot:spring-boot-starter-test:$springBoot_version") {
    testImplementation("io.kotlintest:kotlintest-runner-junit5:$kotlintestVersion")
    testImplementation("io.kotlintest:kotlintest-extensions-spring:$kotlintestVersion")
    testImplementation("io.mockk:mockk:1.9.3")
//    testImplementation("com.ninja-squad:springmockk:2.0.0")
}
Run Code Online (Sandbox Code Playgroud)

在 github 上,我发现了一个问题,遗憾的是已经关闭了,但没有任何将这两个框架一起使用的正确方法:https : //github.com/Ninja-Squad/springmockk/issues/26

编辑:

这是一个示例测试,它在使用 mockkito 时有效,但在使用 springmockk 时无效。

@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@WithMockUser(authorities = ["ROLE_TESTUSER"])
internal class MockTest : AnnotationSpec() {

    override fun listeners() = listOf(SpringListener)

    @Autowired
    lateinit …
Run Code Online (Sandbox Code Playgroud)

spring unit-testing kotlin kotlintest mockk

5
推荐指数
1
解决办法
518
查看次数

Intellij 显示 build.gradle.kts 中的每一行都是红色的

我从 VCS(Kotlin 和 Gradle)创建了一个新项目。我添加了三个testImplementation()电话到build.gradle.kts。IntelliJ 没有接受更改,所以我做了File -> Invalidate caches / Restart,现在 Intellij以红色显示每个implementation()呼叫和每个testImplementation()呼叫。事实上,每个模块中每个 build.gradle.kts 中的每一行都是红色的。

Intellij 失去了理智。当涉及到 Gradle 中的 kotlintest 时,我必须做什么才能恢复 IntelliJ 的智能?

intellij-idea gradle kotlin kotlintest

5
推荐指数
2
解决办法
544
查看次数

如何使用 init{} 中的 API 调用来测试 ViewModel + Flow

我有 ViewModel 将流程暴露给片段。我从 ViewModel 的 init 调用 API,它会发出不同的状态。我无法编写单元测试来检查所有发出的状态。

我的视图模型

class FooViewModel constructor( fooProvider : FooProvider){

private val _uiState = MutableSharedFlow<UiState>(replay = 1)
// Used in fragment to collect ui states.
val uiState = _uiState.asSharedFlow()

init{
_uiState.emit(FetchingFoo)
viewModelScope.runCatching {
// Fetch shareable link from server [users.sharedInvites.list].
fooProvider.fetchFoo().await()
}.fold(
onSuccess = { 
_uiState.emit(FoundFoo)
},
onFailure = {
_uiState.emit(EmptyFoo)
}
)
}

sealed class UiState {
object FetchingFoo : UiState()
object FoundFoo : UiState()
object EmptyFoo : UiState()
}
}
Run Code Online (Sandbox Code Playgroud)

现在我想测试这个 ViewModel 以检查是否发出了所有状态。 …

android turbine kotlin kotlintest kotlin-coroutines

5
推荐指数
1
解决办法
3887
查看次数