Java 运行测试时不能使用spring boot starter web依赖项
我有使用gradle的多模块项目(web、common、batch) 项目进展顺利 但是,当spring出现时,我尝试从批处理模块的测试文件夹运行junit时,我得到以下结果:Java 运行测试时不能使用spring boot starter web依赖项,java,spring,gradle,dependencies,spring-boot,Java,Spring,Gradle,Dependencies,Spring Boot,我有使用gradle的多模块项目(web、common、batch) 项目进展顺利 但是,当spring出现时,我尝试从批处理模块的测试文件夹运行junit时,我得到以下结果: Caused by: java.lang.NoClassDefFoundError: org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter 我从批处理模块中检查了依赖关系树: <batch>gradle -
Caused by: java.lang.NoClassDefFoundError: org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter
我从批处理模块中检查了依赖关系树:
<batch>gradle -q dependencies --configuration testRuntime
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:$project.ext.springBootVersion")
}
}
description = 'batch'
dependencies {
compile("org.springframework.boot:spring-boot-starter-batch:$project.ext.springBootVersion")
testRuntime("org.springframework.batch:spring-batch-test:$project.ext.springBatchVersion") {
exclude(module: 'spring-test')
exclude(module: 'spring-jdbc')
exclude(module: 'commons-io')
exclude(module: 'commons-collections')
}
/* compile ('org.springframework.integration:spring-integration-core:4.1.2.RELEASE')*/
compile project(":common")
testRuntime("org.springframework.boot:spring-boot-starter-web")
}
这是批处理模块上的my build.gradle:
<batch>gradle -q dependencies --configuration testRuntime
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:$project.ext.springBootVersion")
}
}
description = 'batch'
dependencies {
compile("org.springframework.boot:spring-boot-starter-batch:$project.ext.springBootVersion")
testRuntime("org.springframework.batch:spring-batch-test:$project.ext.springBatchVersion") {
exclude(module: 'spring-test')
exclude(module: 'spring-jdbc')
exclude(module: 'commons-io')
exclude(module: 'commons-collections')
}
/* compile ('org.springframework.integration:spring-integration-core:4.1.2.RELEASE')*/
compile project(":common")
testRuntime("org.springframework.boot:spring-boot-starter-web")
}
知道怎么了吗
谢谢。几个选项,更改:
testRuntime(“org.springframework.boot:springbootstarterweb”)
致:
compile(“org.springframework.boot:springbootstarterweb”)
另外,我的Spring Bootbuild.gradle
文件没有使用:$project.ext.springBatchVersion
变量。它似乎很好地设置了变量——例如,下面是我的(浓缩)示例build.gradle
文件:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
您或者需要在声明依赖项时提供版本号,或者需要应用Spring引导插件(您已经将插件声明为构建脚本依赖项,但似乎没有应用它)
当应用Spring Boot插件时,它会自动将Spring Boot starter web的版本设置为与插件相同的版本。您可以在文档中看到插件可以为您设置的选项。它是运行时或编译时?运行时所必需的。在编译时,我没有遇到任何问题,但是当我运行测试,spring容器出现时,我得到了这个异常。挖掘之后,我看到了SpringBootStarerWebDependecy中的方法。这个项目可以在线使用吗?有点问题,但如果我没有选择,我会为它工作。请告诉我你是否需要更多的细节来理解这个问题。。我想这是解决问题的最佳选择:/当我将执行器依赖项添加为testRuntime时,我在使用gradle:+--org.springframework.boot:spring boot starter web:failed+--org.springframework.boot:spring boot starter执行器:failed再次运行依赖项树时失败了。我想我的下一步将是查找项目在线,比如@Opal。如果您可以将其剥离到产生错误的最小值,这将是很有帮助的。如果其他人可以构建它,可能是配置问题。我按照您的建议提供了版本。还是一样的错误。编译(“org.springframework.boot:springbootstarterweb:$project.ext.springBootVersion”)gradle--dependencies现在说什么?之前它说
org.springframework.boot:springbootstarterweb:FAILED
(注意缺少的版本号)。如果您已经提供了版本号,那么现在应该是类似于org.springframework.boot:springbootstarterweb:1.2.1.RELEASE
你搞定了,伙计!!请解释为什么我需要应用spring插件才能工作?