首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin+Maven+Spring Boot+Kotest:无法初始化主类io.kotest.launcher.LauncherKt

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

Stack Overflow用户
提问于 2022-08-28 14:51:14
回答 1查看 189关注 0票数 0

我试图在一个spring引导项目上运行一个用Kotest编写的简单单元测试。但不幸的是我收到了一条错误消息

代码语言:javascript
复制
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文件

代码语言:javascript
复制
<?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引导项目的简单示例

代码语言:javascript
复制
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)
}

我的单元测试:

代码语言:javascript
复制
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更好:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-30 13:13:34

我不认为应该归咎于Maven,但问题可能是存在某种依赖关系错误或缺失。

错误消息中提到的TestEngineListener包含在工件kotest-framework-engine-jvm中,当您将kotest-runner-junit5-jvm添加到项目时,工件kotest-framework-engine-jvm是临时包含的。

所以,我想您只需要用kotest-runner-junit5-jvm替换pom中的kotest-runner-junit5-jvm

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73519695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档