java - Spring Boot公共库项目中的单元测试
问题描述
我的问题是如何配置 gradle 和 spring boot 以在不应用org.springframework.boot
插件的情况下运行公共库项目的单元测试?
我有一个使用gradle
. 问题是:
我不想将
org.springframework.boot
插件应用到我的项目中,因为它应该用于 Spring Boot 应用程序,而是一个库项目。我需要在我的测试中连接(@Autowired)一些 bean。
我试图配置我的
build.gradle
文件如下
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
junitPlatformVersion = '1.0.0-M2'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
//apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'my-common-lib'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:$springBootVersion"
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.jetbrains.kotlin:kotlin-reflect")
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude module: 'junit'
}
testCompile group: 'org.assertj', name: 'assertj-core', version: assertjCoreVersion
testImplementation('org.junit.jupiter:junit-jupiter-api')
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine')
}
- 我的测试如下:
@ExtendWith(SpringExtension::class)
@SpringBootTest
class MyCommonLibraryTest(@Autowired val myBean: MyBean) {
@Test
...
}
- 最后,我
BeanCreationException
在运行测试时被抛出
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anotherBean' defined in class path resource [com/pipedbits/spiralg/templateengine/infrastructure/configuration/TemplateConfiguration.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: Resource not found in classpath: kotlin/coroutines/coroutines.kotlin_builtins
解决方案
推荐阅读
- cplex - 在 CPLEX OPL 中使用元组从元组中的集合中读取数据时出错
- blazor - (string[] args) 如何在 Blazor WebAssembly 应用程序中传递给 Program.Main?
- c - 给定三个边1.1、2.2、3.3,用C语言判断它们是否可以组成一个三角形,但是有一个奇怪的问题
- cookies - 设置 JupyterHub SameSite Cookie 属性
- r - R 中的混合线性整数规划(使用 OMPR 包)
- javascript - 如何将数据从窗口方法传递到 Vuejs 应用程序中的数据函数
- javascript - 使用 Moment JS 的格式转换为相同的日期格式会给出无效的日期
- mongodb - 是否可以使用公共字段在 Mongodb 中加入两个不同的数据库集合
- c# - ASP.NET 核心 MVC;EF Core:类型不能隐式转换为
- spring-boot - 允许管理员访问所有映射 Spring 安全性