spring - 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
在无法自动加载上下文之前,我从来没有遇到过这个问题。
解决方案
推荐阅读
- flutter - 使用 ThemeData 根据颤动中的周围颜色更改文本颜色
- winapi - 音频渲染客户端位置的有效采样
- pyspark - AttributeError:“GroupedData”对象在 Azure Databricks 中没有属性“applyInPandas”
- javascript - 如何修改我的api的数字小数?
- javascript - 在 ReactJS 中的 onclick 内制作 onclick
- reactjs - 语义 ui 反应下拉列表 - 更新选项并取消选择下拉列表
- jquery - Bootstrap ColorPicker 不适用于动态创建的元素
- python - 如何根据 Folder2 文件名重命名 Folder1 中的文件?
- python - cv2 SimpleBlobDetector 困难
- docker - 在容器内使用 docker-compose 进行奇怪的绑定安装行为