【问题标题】:SpringApplicationConfiguration not found: Erroneous spring-boot-starter-test content?找不到 SpringApplicationConfiguration:错误的 spring-boot-starter-test 内容?
【发布时间】:2017-07-12 17:20:55
【问题描述】:

在 Maven 中遇到编译错误:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/prototypes/demo-sse-spring-boot-master/src/test/java/com/cedric/demo/sse/SseDemoApplicationTests.java:[6,37] package org.springframework.boot.test does not exist
[ERROR] /C:/TITAN/demo-sse-spring-boot-master/src/test/java/com/cedric/demo/sse/SseDemoApplicationTests.java:[10,2] cannot find symbol
  symbol: class SpringApplicationConfiguration
[INFO] 2 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

Maven repo 似乎存在 jar:

然而,该 jar 中没有任何已编译的类。只有 META-INF 目录:

这是设计使然吗?我在哪里可以得到包含 SpringApplicationConfiguration 类的 jar 来让 Maven 开心?

这是我的 pom.xml 的相关部分:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars.bower</groupId>
            <artifactId>jquery</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

【问题讨论】:

    标签: maven testing spring-boot spring-boot-test


    【解决方案1】:

    在您的版本中,@SpringApplicationConfiguration 注释不再存在。新的注释是:

    @RunWith(SpringRunner.class)
    
    @SpringBootTest(classes = YourApplicationMainClass.class)
    
    @WebAppConfiguration
    

    【讨论】:

    • 这应该是选择的答案
    【解决方案2】:

    spring-boot-starter-test,就像所有其他 Spring Boot 启动器一样,实际上只是一个 pom,它可以传递地引入许多其他依赖项。它只有一个 jar 来让一些不喜欢 pom-only 依赖的构建系统保持快乐。

    您似乎已将应用程序从 Spring Boot 1.4 升级到 Spring Boot 1.5。 Spring Boot 1.5 删除了许多在 1.4 中已弃用的类,包括 org.springframework.boot.test.SpringApplicationConfiguration

    我建议退回到 Spring Boot 1.4.4.RELEASE 并修复所有弃用警告。然后,您应该可以毫无困难地升级到 Spring Boot 1.5.1.RELEASE。

    【讨论】:

    • 是的,这个技巧对我有用,谢谢。您能否指出我对这个 Maven 设置如何工作的全面描述,这使得它能够在不需要列出它们的情况下提取依赖项,不太清楚为什么 1.5.1.RELEASE 不能做到这一点,它是一个主要版本吗决定停止支持已弃用项目的依据是什么?再次感谢您。
    【解决方案3】:

    由于该错误是由于 Spring Boot 从 1.4 升级到 1.5 造成的,重要的是要注意(从下面),1.4 中引入的几个新类已弃用了一些现有类,导致最终在 1.5 中被删除.详情请见:Spring boot release notes

    引自网站(已编辑):

    此外,Spring Boot 1.4(及更高版本)试图合理化和简化运行 Spring Boot 测试的各种方式。您应该迁移以下内容以使用新的 @SpringBootTest 注释:

    @SpringApplicationConfiguration(classes=MyConfig.class)@SpringBootTest(classes=MyConfig.class)

    @ContextConfiguration(classes=MyConfig.class, loader=SpringApplicationContextLoader.class)@SpringBootTest(classes=MyConfig.class)

    @IntegrationTest@SpringBootTest(webEnvironment=WebEnvironment.NONE)

    @IntegrationTest@WebAppConfiguration@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT) (or RANDOM_PORT)

    @WebIntegrationTest@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT) (or RANDOM_PORT)

    提示在迁移测试时,您可能还想替换任何 @RunWith(SpringJUnit4ClassRunner.class) Spring 4.3 的声明 更具可读性@RunWith(SpringRunner.class)

    【讨论】:

    • 因此:@SpringBootTest(classes = YourApplicationMainClass.class)@WebIntegrationTest 从早期版本一起转换为 @SpringBootTest(classes = YourApplicationMainClass.class, webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT) 中的单个语句
    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 2019-03-22
    • 2021-05-31
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多