Kotlin+Maven+Spring Boot+Kotest:无法初始化主类io.kotest.launcher.LauncherKt

问题描述 投票:0回答:2

我正在尝试在 Spring Boot 项目上运行一个用 Kotest 编写的简单单元测试。但不幸的是我收到一条错误消息

Testing started at 17:38 ...
Error: Unable to initialize main class io.kotest.launcher.LauncherKt
Caused by: java.lang.NoClassDefFoundError: io/kotest/core/engine/TestEngineListener

Process finished with exit code 1

我的 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-kotest-test</artifactId>
    <groupId>org.example</groupId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>consoleApp</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.code.style>official</kotlin.code.style>
        <kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
        <kotlin.version>1.7.10</kotlin.version>

    </properties>

    <repositories>
        <repository>
            <id>mavenCentral</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>

    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>1.7.10</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>MainKt</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.7.3</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>


        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>

        <dependency>
            <groupId>io.kotest</groupId>
            <artifactId>kotest-runner-junit5</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.kotest.extensions</groupId>
            <artifactId>kotest-extensions-spring</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.kotest</groupId>
            <artifactId>kotest-assertions-core-jvm</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>


    </dependencies>

</project>

Spring Boot项目的简单示例

package org.example.spring.kotest.test

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.stereotype.Service

@SpringBootApplication
open class SpringKotestTestApplication

@Service
class SomeService {
    fun foo() = 1
}

fun main(args: Array<String>) {
    runApplication<SpringKotestTestApplication>(*args)
}

我的单元测试:

package org.example.spring.kotest.test

import io.kotest.core.spec.style.DescribeSpec
import io.kotest.extensions.spring.SpringExtension
import io.kotest.matchers.shouldBe
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
@EnableAutoConfiguration
class SomeServiceTest(
    private val someService: SomeService
) : DescribeSpec() {

    override fun extensions() = listOf(SpringExtension)

    init {
        describe("test") {
            it("test") {
                someService.foo().shouldBe(1)
            }
        }
    }
}

我已经尝试解决这个问题很长时间了。看来 Kotest 与 Gradle 配合使用比与 Maven 配合使用效果更好:(

spring spring-boot maven kotlin kotest
2个回答
0
投票

我不认为 Maven 是罪魁祸首,但问题可能是某些依赖项错误或缺失。

错误消息中提到的

TestEngineListener
包含在工件
kotest-framework-engine-jvm
中,当您将
kotest-runner-junit5-jvm
添加到项目时,该工件会被传递包含。

所以,我想你只需要将 pom 中的

kotest-runner-junit5
替换为
kotest-runner-junit5-jvm


0
投票

从 Maven 切换到 Gradle 后,我们遇到了同样的问题。我们通过导航到

Run | Edit Configurations
然后选择此选项来再次运行测试:

Intellij Kotlin Run | Edit Configurations dialog

© www.soinside.com 2019 - 2024. All rights reserved.