30

在 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>
4

3 回答 3

101

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

@RunWith(SpringRunner.class)

@SpringBootTest(classes = YourApplicationMainClass.class)

@WebAppConfiguration
于 2017-03-14T14:01:33.060 回答
25

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。

于 2017-02-22T16:26:56.317 回答
23

由于该错误是由于 Spring Boot 从 1.4 升级到 1.5 造成的,重要的是要注意(从下面)1.4 中引入的几个新类弃用了一些现有类,导致最终在 1.5 中被删除。可以在以下位置找到此类的详细信息:Spring Boot 发行说明

引用自网站(已编辑):

此外,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).

于 2017-12-04T07:39:59.290 回答