我正在创建自定义启动器,并希望添加示例应用程序,以演示自动配置并对其进行测试。但是当我在控制台中运行META-INF/spring.factories
时,Gradle似乎并不了解./gradlew test
。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate.
这是我的示例应用程序的
build.gradle
,它是多模块项目中的模块:plugins {
id 'org.jetbrains.kotlin.jvm'
id 'org.jetbrains.kotlin.plugin.spring'
id 'org.jetbrains.kotlin.plugin.jpa'
id 'org.springframework.boot'
id 'io.spring.dependency-management'
}
dependencies {
implementation project(':custom-spring-boot-starter')
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation "org.flywaydb:flyway-core:5.2.4"
implementation 'com.h2database:h2:1.4.197'
// Test
testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation 'org.springframework.boot:spring-boot-test-autoconfigure:2.1.7.RELEASE'
testImplementation "org.hamcrest:hamcrest:2.1"
testImplementation "org.testng:testng:6.14.3"
testImplementation "com.github.javafaker:javafaker:0.17.2"
testImplementation "org.awaitility:awaitility:3.0.0"
}
test {
useTestNG()
jacoco {
destinationFile = file("$rootDir/build/jacoco/test.exec")
}
}
它是普通的Spring Boot应用程序:
@SpringBootApplication
class Application
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}
我为所有上下文测试配置基类,如下所示:
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
abstract class ApplicationTest : AbstractTestNGSpringContextTests() {
@Autowired
lateinit var restTemplate: TestRestTemplate
@LocalServerPort
private var port: Int = 0
fun getRequest(uri: String, responseType: Class<*>) =
restTemplate.getForEntity("http://localhost:$port$uri", responseType)
}
那么,我应该怎么做才能在Gradle命令执行下应用自定义自动配置?
最佳答案
在官方的Spring指南here(源代码here)中了解一下Spring Boot多模块项目,他们在其中使用了自定义jar任务(“库”模块)。如果运行“gradle build
”,则生成的 Artifact 在库module --> libs
文件夹中。这是没有主类的Spring引导库。另一个模块(“应用程序”)当然是主应用程序,它使用简单的自定义“bootJar
”任务。两个模块均包含测试。