Spring-boot-test 依赖项必须手动导入,尽管它们存在于 Spring-boot-starter-parent - 为什么?

Spring-boot-test dependencies having to be manually imported inspite of them being present in Spring-boot-starter-parent - Why?

我是 运行 具有 spring-boot-starter 依赖项的 spring 启动应用程序,如果我不导入以下测试,我的测试用例将面临编译错误依赖项

我的理解是,这些已经存在于 spring-boot-starter-parent 中,我也可以看到它们。但是,由于编译时错误,我被迫将它们导入到 pom.xml 中,如下所示,但随后我收到警告

Duplicating managed version 1.5.6.RELEASE for spring-boot-test

Duplicating managed version 4.3.10.RELEASE for spring-test

and similiarly for assertj-core

您可以在pom.xml这里看到出现警告的地方

而我的pom.xml如下

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

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <!-- <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency> -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <version>1.5.6.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.10.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>${json.version}</version>
    </dependency>

如果我不包含测试依赖项,我的部分代码会发生编译错误,如下所示。如果依赖项不存在,则无法导入@SpringBootTest 和TestRestTemplate。

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import com.fasterxml.jackson.core.JsonProcessingException;

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 
public class MatchControllerTest {

    // Test RestTemplate to invoke the APIs.
    @Autowired
    private TestRestTemplate testRestTemplate;
    //....and other part of the code

为什么会这样?

  1. 如果测试依赖项已经存在于 spring-boot-starter-parent
  2. 中,为什么必须手动将它们包含在 pom.xml 中
  3. 包含后,它会显示重复的托管版本警告(可能是正确的...)。

我可能正在做某事silly/wrong - 请帮忙!

如您在 pom 中所见:

<dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>1.5.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <type>test-jar</type>
                <version>1.5.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>1.5.6.RELEASE</version>
            </dependency>
 ...

依赖项在标签 <dependencyManagement> 下。这意味着,如果您在项目中需要它,您将获得版本 1.5.6.RELEASE

所以你只需添加

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

没有版本号,警告应该会消失。

将所有范围 "test" 替换为 "compile"。

在项目属性中,java 构建路径,检查其在 JRE 上的 运行?如果是 JRE,则将其设置为 JDK。我遇到了同样的问题并且能够解决它。