springboottest不加载应用程序上下文,只是挂起而没有错误

qc6wkl3g  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(254)

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

  1. import org.junit.jupiter.api.Test
  2. import org.springframework.boot.test.context.SpringBootTest
  3. @SpringBootTest
  4. class EventConsumerApplicationTests {
  5. @Test
  6. fun contextLoads() {
  7. }
  8. }

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

  1. > Task :compileKotlin UP-TO-DATE
  2. > Task :compileJava NO-SOURCE
  3. > Task :processResources UP-TO-DATE
  4. > Task :classes UP-TO-DATE
  5. > Task :compileTestKotlin UP-TO-DATE
  6. > Task :compileTestJava NO-SOURCE
  7. > Task :processTestResources
  8. > Task :testClasses
  9. > Task :test

它将永远这样。没有例外。
我的主要应用程序类如下所示。

  1. import org.springframework.boot.autoconfigure.SpringBootApplication
  2. import org.springframework.boot.runApplication
  3. @SpringBootApplication
  4. class EventConsumerApp
  5. fun main(args: Array<String>) {
  6. runApplication<EventConsumerApp>(*args)
  7. }

我有一个配置类。

  1. import com.something.eventconsumer.service.EventService
  2. import kotlinx.coroutines.launch
  3. import kotlinx.coroutines.runBlocking
  4. import org.joda.time.DateTime
  5. import org.springframework.context.annotation.Bean
  6. import org.springframework.context.annotation.Configuration
  7. @Configuration
  8. class EventConsumer(private val eventService: EventService) {
  9. @Bean
  10. fun consumer() {
  11. return runBlocking {
  12. launch {
  13. createCoroutine()
  14. }
  15. }
  16. }
  17. // other functions...
  18. }

以及 EventService 是这样的。

  1. import org.springframework.stereotype.Service
  2. import com.something.eventconsumer.repository.EventRepo
  3. @Service
  4. class EventService(
  5. private val eventRepo: EventRepo
  6. ) {
  7. suspend fun persist(event: Event) {
  8. // some code
  9. }
  10. // some other codes...
  11. }

以及 EventRepo .

  1. import org.springframework.data.annotation.Id
  2. import org.springframework.data.relational.core.mapping.Table
  3. import org.springframework.data.repository.kotlin.CoroutineCrudRepository
  4. import java.math.BigDecimal
  5. import java.time.LocalDateTime
  6. @Table
  7. data class Event(
  8. @Id val id: Long = 0,
  9. val eventId: String,
  10. val reference: String,
  11. val baseAmount: BigDecimal,
  12. val valueDate: LocalDateTime? = null,
  13. )
  14. interface EventRepo : CoroutineCrudRepository<Event, Long>

如果我运行 mainEventConsumerApp ,应用程序运行得非常好。
这个 build.gradle.kts 是这样的(只包括相关的)。

  1. plugins {
  2. id("org.springframework.boot") version "2.4.4"
  3. id("io.spring.dependency-management") version "1.0.11.RELEASE"
  4. kotlin("jvm") version "1.4.31"
  5. kotlin("plugin.spring") version "1.4.31"
  6. }
  7. java.sourceCompatibility = JavaVersion.VERSION_11
  8. dependencies {
  9. implementation("org.jetbrains.kotlin:kotlin-reflect")
  10. implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
  11. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
  12. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive")
  13. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
  14. implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")
  15. implementation("io.r2dbc:r2dbc-postgresql")
  16. testImplementation("org.springframework.boot:spring-boot-starter-test")
  17. }
  18. tasks.withType<Test> {
  19. useJUnitPlatform()
  20. }

我试着在spring启动测试中手动指定类。

  1. import org.junit.jupiter.api.Test
  2. import org.springframework.boot.test.context.SpringBootTest
  3. @SpringBootTest(classes = [EventConsumer::class])
  4. class EventConsumerApplicationTests {

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

  1. EventConsumerApplicationTests > contextLoads() FAILED
  2. java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
  3. Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at ConstructorResolver.java:800
  4. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException at DefaultListableBeanFactory.java:1790
  5. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.something.eventconsumer.service.EventService' available

有人能给我指一下正确的方向吗?我以前从没遇到过这个问题 SpringBootTest 无法自动加载上下文。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题