簡體   English   中英

運行測試時無法使用spring-boot-starter-web依賴

[英]Cant use spring-boot-starter-web dependecies when running tests

我有使用gradle(web,common,batch)的多模塊項目

該項目運行良好

但是,當我試圖從彈簧的批處理模塊的測試文件夾中運行junit時,我得到了:

Caused by: java.lang.NoClassDefFoundError: org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter

我從批處理模塊檢查了依賴樹:

<batch>gradle -q dependencies --configuration testRuntime

我在嘗試導入spring-boot-start-web時看到失敗

.....
|    |         +--- com.caucho:resin-hessian:4.0.23
|    |         +--- org.springframework:spring-jdbc:3.1.0.RELEASE -> 4.1.4.RELEASE (*)
|    |         +--- com.adobe.blazeds:blazeds-common:3.2.0.3978
|    |         +--- com.adobe.blazeds:blazeds-proxy:3.2.0.3978
|    |         +--- org.codehaus.jackson:jackson-core-asl:1.9.2
|    |         +--- org.codehaus.jackson:jackson-mapper-asl:1.9.2 (*)
|    |         \--- org.mockito:mockito-all:1.9.5
|    +--- com.mycompany.services.configuration:ConfigurationManager:2.0.8 (*)
|    +--- com.caucho:resin-hessian:4.0.23
|    +--- mysql:mysql-connector-java:5.1.34
|    \--- javax.inject:javax.inject:1
+--- org.springframework.boot:spring-boot-starter-test:1.2.1.RELEASE
|    +--- junit:junit:4.12
|    |    \--- org.hamcrest:hamcrest-core:1.3
|    +--- org.mockito:mockito-core:1.10.8
|    |    +--- org.hamcrest:hamcrest-core:1.1 -> 1.3
|    |    \--- org.objenesis:objenesis:2.1
|    +--- org.hamcrest:hamcrest-core:1.3
|    +--- org.hamcrest:hamcrest-library:1.3
|    |    \--- org.hamcrest:hamcrest-core:1.3
|    +--- org.springframework:spring-core:4.1.4.RELEASE (*)
|    \--- org.springframework:spring-test:4.1.4.RELEASE
|         \--- org.springframework:spring-core:4.1.4.RELEASE (*)
+--- org.springframework.batch:spring-batch-test:3.0.2.RELEASE
|    +--- junit:junit:4.11 -> 4.12 (*)
|    +--- org.hamcrest:hamcrest-all:1.3
|    \--- org.springframework.batch:spring-batch-core:3.0.2.RELEASE (*)
\--- org.springframework.boot:spring-boot-starter-web: FAILED

這是我在批處理模塊上的build.gradle:

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:spring-boot-starter-web")

至:

compile("org.springframework.boot:spring-boot-starter-web")

另外,我的Spring Boot build.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 Boot插件(您已經將該插件聲明為構建腳本依賴項,但似乎沒有應用)。

應用Spring Boot插件后,它將自動將spring-boot-starter-web的版本設置為與插件相同的版本。 您可以在文檔中看到該插件可以為您設置的所有版本列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM