首页 > 解决方案 > SpringBootTest 不加载应用程序上下文,只是挂起没有错误

问题描述

我有一个非常基本的集成测试,这是从 Spring Initializr 初始化基于 Spring 的应用程序时的默认设置。

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class EventConsumerApplicationTests {

    @Test
    fun contextLoads() {
    }

}

当我从 IntelliJ 运行它时,日志仅显示以下内容。

> Task :compileKotlin UP-TO-DATE
> Task :compileJava NO-SOURCE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestKotlin UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources
> Task :testClasses
> Task :test

它会永远保持这种状态。根本没有例外。

我的主要应用程序类看起来像这样。

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

@SpringBootApplication
class EventConsumerApp

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

我有一个配置类。

import com.something.eventconsumer.service.EventService
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.joda.time.DateTime
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class EventConsumer(private val eventService: EventService) {
    @Bean
    fun consumer() {
        return runBlocking {
            launch {
                createCoroutine()
            }
        }
    }

    // other functions...
}

EventService这样的。

import org.springframework.stereotype.Service
import com.something.eventconsumer.repository.EventRepo

@Service
class EventService(
    private val eventRepo: EventRepo
) {
    suspend fun persist(event: Event) {
        // some code
    }

    // some other codes...
}

EventRepo.

import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
import org.springframework.data.repository.kotlin.CoroutineCrudRepository
import java.math.BigDecimal
import java.time.LocalDateTime

@Table
data class Event(
    @Id val id: Long = 0,
    val eventId: String,
    val reference: String,
    val baseAmount: BigDecimal,
    val valueDate: LocalDateTime? = null,
)

interface EventRepo : CoroutineCrudRepository<Event, Long>

如果我在 中运行该main功能,则EventConsumerApp该应用程序运行良好。

build.gradle.kts就是这样(只包括相关的)。

plugins {
    id("org.springframework.boot") version "2.4.4"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.4.31"
    kotlin("plugin.spring") version "1.4.31"
}

java.sourceCompatibility = JavaVersion.VERSION_11

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")

    implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")
    implementation("io.r2dbc:r2dbc-postgresql")

    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

我试图像这样在 Spring Boot Test 中手动指定类。

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest(classes = [EventConsumer::class])
class EventConsumerApplicationTests {

然后,我得到了一个例外。

EventConsumerApplicationTests > contextLoads() FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
        Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at ConstructorResolver.java:800
            Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException at DefaultListableBeanFactory.java:1790

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.something.eventconsumer.service.EventService' available

有人可以指出我正确的方向吗?SpringBootTest在无法自动加载上下文之前,我从来没有遇到过这个问题。

标签: springspring-bootkotlinspring-data-r2dbc

解决方案


推荐阅读