Spring无法在 Spring 启动测试中解析 ApplicationConfiguration
SpringApplicationConfiguration cannot be resolved in a Spring Boot test
我使用 Spring Initializr 生成了一个 Spring Boot web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
使用的技术:
Spring 引导 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat 嵌入 8.5.6 , 行家 3, Java 8.
现在我想使用 class SpringApplicationConfiguration
实施测试,所以我导入了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
但是即使我已经多次更新项目,我仍会收到此错误
The import org.springframework.boot.test.SpringApplicationConfiguration cannot be resolved
我还尝试通过右键单击 Eclipse 中的 pom.xml 和 'Maven Add Dependency' 、
来添加依赖项
做 mvn clean dependency:tree
依赖是存在的,但是做 mvn clean compile
和 mvn dependency:purge-local-repository
我得到了 package org.springframework.test.annotation does not exist
[INFO] +- org.springframework.boot:spring-boot-starter-aop:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.2.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.24:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.24:compile
[INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.24:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] | +- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
[INFO] | | \- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
[INFO] | \- org.aspectj:aspectjweaver:jar:1.8.9:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.2.RELEASE:compile
[INFO] | +- org.apache.tomcat:tomcat-jdbc:jar:8.5.11:compile
[INFO] | | \- org.apache.tomcat:tomcat-juli:jar:8.5.11:compile
[INFO] | \- org.springframework:spring-jdbc:jar:4.3.7.RELEASE:compile
[INFO] | \- org.springframework:spring-tx:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:4.3.7.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:4.3.7.RELEASE:compile
[INFO] | +- org.springframework:spring-context-support:jar:4.3.7.RELEASE:compile
[INFO] | \- com.sun.mail:javax.mail:jar:1.5.6:compile
[INFO] | \- javax.activation:activation:jar:1.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-security:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.security:spring-security-config:jar:4.2.2.RELEASE:compile
[INFO] | | \- org.springframework.security:spring-security-core:jar:4.2.2.RELEASE:compile
[INFO] | \- org.springframework.security:spring-security-web:jar:4.2.2.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:1.5.2.RELEASE:compile
[INFO] | +- org.thymeleaf:thymeleaf-spring4:jar:2.1.5.RELEASE:compile
[INFO] | | \- org.thymeleaf:thymeleaf:jar:2.1.5.RELEASE:compile
[INFO] | | +- ognl:ognl:jar:3.0.8:compile
[INFO] | | +- org.javassist:javassist:jar:3.21.0-GA:compile
[INFO] | | \- org.unbescape:unbescape:jar:1.1.0.RELEASE:compile
[INFO] | \- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:1.4.0:compile
[INFO] | \- org.codehaus.groovy:groovy:jar:2.4.9:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.2.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.11:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.11:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.11:compile
[INFO] | +- org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile
[INFO] | +- org.springframework:spring-web:jar:4.3.7.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-batch:jar:1.5.2.RELEASE:compile
[INFO] | \- org.springframework.batch:spring-batch-core:jar:3.0.7.RELEASE:compile
[INFO] | +- com.ibm.jbatch:com.ibm.jbatch-tck-spi:jar:1.0:compile
[INFO] | | \- javax.batch:javax.batch-api:jar:1.0:compile
[INFO] | +- com.thoughtworks.xstream:xstream:jar:1.4.9:compile
[INFO] | | +- xmlpull:xmlpull:jar:1.1.3.1:compile
[INFO] | | \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] | +- org.codehaus.jettison:jettison:jar:1.2:compile
[INFO] | \- org.springframework.batch:spring-batch-infrastructure:jar:3.0.7.RELEASE:compile
[INFO] | \- org.springframework.retry:spring-retry:jar:1.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.5.2.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:1.5.2.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.2.RELEASE:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.2.0:test
[INFO] | | \- net.minidev:json-smart:jar:2.2.1:test
[INFO] | | \- net.minidev:accessors-smart:jar:1.1:test
[INFO] | | \- org.ow2.asm:asm:jar:5.0.3:test
[INFO] | +- junit:junit:jar:4.12:test
[INFO] | +- org.assertj:assertj-core:jar:2.6.0:test
[INFO] | +- org.mockito:mockito-core:jar:1.10.19:test
[INFO] | | \- org.objenesis:objenesis:jar:2.1:test
[INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.4.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
[INFO] | \- org.springframework:spring-test:jar:4.3.7.RELEASE:test
[INFO] +- com.icegreen:greenmail:jar:1.5.3:test
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.24:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:compile
[INFO] | \- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.2.RELEASE:compile
[INFO] +- org.hsqldb:hsqldb:jar:2.3.3:runtime
[INFO] \- mysql:mysql-connector-java:jar:5.1.41:runtime
SpringApplicationConfiguration
已弃用,取而代之的是 SpringBootTest
。它在最新的 spring-boot 版本中被删除。
您提到您正在使用 Spring Boot 1.4.2
,但实际上我看到了对 1.5.2
的依赖,这就是您无法导入它的原因。
我建议您在测试中加载 spring 上下文时使用 SpringBootTest
。
使用@SpringBootTest 代替@SpringApplicationConfiguration
我使用 Spring Initializr 生成了一个 Spring Boot web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
使用的技术:
Spring 引导 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat 嵌入 8.5.6 , 行家 3, Java 8.
现在我想使用 class SpringApplicationConfiguration
实施测试,所以我导入了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
但是即使我已经多次更新项目,我仍会收到此错误
The import org.springframework.boot.test.SpringApplicationConfiguration cannot be resolved
我还尝试通过右键单击 Eclipse 中的 pom.xml 和 'Maven Add Dependency' 、
来添加依赖项做 mvn clean dependency:tree
依赖是存在的,但是做 mvn clean compile
和 mvn dependency:purge-local-repository
我得到了 package org.springframework.test.annotation does not exist
[INFO] +- org.springframework.boot:spring-boot-starter-aop:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.5.2.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.2.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.24:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.24:compile
[INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.24:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] | +- org.springframework:spring-aop:jar:4.3.7.RELEASE:compile
[INFO] | | \- org.springframework:spring-beans:jar:4.3.7.RELEASE:compile
[INFO] | \- org.aspectj:aspectjweaver:jar:1.8.9:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.2.RELEASE:compile
[INFO] | +- org.apache.tomcat:tomcat-jdbc:jar:8.5.11:compile
[INFO] | | \- org.apache.tomcat:tomcat-juli:jar:8.5.11:compile
[INFO] | \- org.springframework:spring-jdbc:jar:4.3.7.RELEASE:compile
[INFO] | \- org.springframework:spring-tx:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:4.3.7.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:4.3.7.RELEASE:compile
[INFO] | +- org.springframework:spring-context-support:jar:4.3.7.RELEASE:compile
[INFO] | \- com.sun.mail:javax.mail:jar:1.5.6:compile
[INFO] | \- javax.activation:activation:jar:1.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-security:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.security:spring-security-config:jar:4.2.2.RELEASE:compile
[INFO] | | \- org.springframework.security:spring-security-core:jar:4.2.2.RELEASE:compile
[INFO] | \- org.springframework.security:spring-security-web:jar:4.2.2.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:1.5.2.RELEASE:compile
[INFO] | +- org.thymeleaf:thymeleaf-spring4:jar:2.1.5.RELEASE:compile
[INFO] | | \- org.thymeleaf:thymeleaf:jar:2.1.5.RELEASE:compile
[INFO] | | +- ognl:ognl:jar:3.0.8:compile
[INFO] | | +- org.javassist:javassist:jar:3.21.0-GA:compile
[INFO] | | \- org.unbescape:unbescape:jar:1.1.0.RELEASE:compile
[INFO] | \- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:1.4.0:compile
[INFO] | \- org.codehaus.groovy:groovy:jar:2.4.9:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.2.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.11:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.11:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.11:compile
[INFO] | +- org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.7:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.8.7:compile
[INFO] | +- org.springframework:spring-web:jar:4.3.7.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:4.3.7.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-batch:jar:1.5.2.RELEASE:compile
[INFO] | \- org.springframework.batch:spring-batch-core:jar:3.0.7.RELEASE:compile
[INFO] | +- com.ibm.jbatch:com.ibm.jbatch-tck-spi:jar:1.0:compile
[INFO] | | \- javax.batch:javax.batch-api:jar:1.0:compile
[INFO] | +- com.thoughtworks.xstream:xstream:jar:1.4.9:compile
[INFO] | | +- xmlpull:xmlpull:jar:1.1.3.1:compile
[INFO] | | \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] | +- org.codehaus.jettison:jettison:jar:1.2:compile
[INFO] | \- org.springframework.batch:spring-batch-infrastructure:jar:3.0.7.RELEASE:compile
[INFO] | \- org.springframework.retry:spring-retry:jar:1.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.5.2.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:1.5.2.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.2.RELEASE:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.2.0:test
[INFO] | | \- net.minidev:json-smart:jar:2.2.1:test
[INFO] | | \- net.minidev:accessors-smart:jar:1.1:test
[INFO] | | \- org.ow2.asm:asm:jar:5.0.3:test
[INFO] | +- junit:junit:jar:4.12:test
[INFO] | +- org.assertj:assertj-core:jar:2.6.0:test
[INFO] | +- org.mockito:mockito-core:jar:1.10.19:test
[INFO] | | \- org.objenesis:objenesis:jar:2.1:test
[INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.4.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-core:jar:4.3.7.RELEASE:compile
[INFO] | \- org.springframework:spring-test:jar:4.3.7.RELEASE:test
[INFO] +- com.icegreen:greenmail:jar:1.5.3:test
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.24:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:1.5.2.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:1.5.2.RELEASE:compile
[INFO] | \- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.2.RELEASE:compile
[INFO] +- org.hsqldb:hsqldb:jar:2.3.3:runtime
[INFO] \- mysql:mysql-connector-java:jar:5.1.41:runtime
SpringApplicationConfiguration
已弃用,取而代之的是 SpringBootTest
。它在最新的 spring-boot 版本中被删除。
您提到您正在使用 Spring Boot 1.4.2
,但实际上我看到了对 1.5.2
的依赖,这就是您无法导入它的原因。
我建议您在测试中加载 spring 上下文时使用 SpringBootTest
。
使用@SpringBootTest 代替@SpringApplicationConfiguration