简体   繁体   English

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

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

I am running a spring boot application with spring-boot-starter dependencies, am facing a compile errors in my test cases if I don't import the following test dependencies我正在运行具有 spring-boot-starter 依赖项的 spring boot 应用程序,如果我不导入以下测试依赖项,我的测试用例中将面临编译错误

  • spring-boot-test弹簧引导测试
  • spring-test弹簧测试
  • assertj-core断言核心

My understanding is that these are present already in the spring-boot-starter-parent and I can see them too.我的理解是这些已经存在于 spring-boot-starter-parent 中,我也可以看到它们。 However, because of the compile time errors I am forced to import them into pom.xml as below, but then I get warnings that但是,由于编译时错误,我被迫将它们导入到 pom.xml 中,如下所示,但随后我收到警告

Duplicating managed version 1.5.6.RELEASE for spring-boot-test为 spring-boot-test 复制托管版本 1.5.6.RELEASE

Duplicating managed version 4.3.10.RELEASE for spring-test为 spring-test 复制托管版本 4.3.10.RELEASE

and similiarly for assertj-core和 assertj-core 类似

You can see the places where the warnings occur in pom.xml here可以在这里查看 pom.xml 中出现警告的地方在此处输入图片说明

And my pom.xml is as follows而我的 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>

Parts of my code where the compile errors occurs if I dont include the test dependencies is below.如果我不包含测试依赖项,则会出现编译错误的部分代码如下。 The @SpringBootTest and the TestRestTemplate cannot be imported if the dependencies are not present.如果依赖项不存在,则无法导入 @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

Why is this happening ?为什么会这样?

  1. Why are the test dependencies having to be manually included in the pom.xml if they are already present in the spring-boot-starter-parent如果测试依赖项已经存在于 spring-boot-starter-parent 中,为什么必须手动包含在 pom.xml 中
  2. Once included, it is showing a duplcating managed version warning (probably rightly so...).一旦包含在内,它就会显示重复的托管版本警告(可能是正确的......)。

I am probably doing something silly/wrong - please help !我可能在做一些愚蠢/错误的事情 - 请帮忙!

As you can see in the pom:正如你在 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>
 ...

The dependency is under the tag <dependencyManagement> .依赖项位于标签<dependencyManagement> That means, if you need it in your project you get the version 1.5.6.RELEASE这意味着,如果您在项目中需要它,您将获得1.5.6.RELEASE版本

So you have only to add所以你只需要添加

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

Without the version number and the warning should go away.如果没有版本号,警告应该会消失。

用“编译”替换所有范围“测试”。

in project properties, java build path, check its running on JRE?在项目属性,java 构建路径中,检查其在 JRE 上的运行情况? if it's JRE, make it as JDK.如果是 JRE,则将其设为 JDK。 I faced the same issue and able to solve it.我遇到了同样的问题并且能够解决它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从spring-boot-starter-parent中排除特定的依赖项 - How to exclude specific dependencies from spring-boot-starter-parent 无法达到 spring-boot-starter-parent 依赖项 - Cannot reach spring-boot-starter-parent dependencies Spring Boot使用的默认值-spring-boot-starter-parent - Defaults used by Spring boot - spring-boot-starter-parent 在 spring-boot-starter-parent 2.7.3 升级到 SnakeYaml 1.31 - upgrade to SnakeYaml 1.31 in spring-boot-starter-parent 2.7.3 spring-boot-starter-parent的不可解析的POM - Non Parsable POM for spring-boot-starter-parent 添加spring-boot-starter-parent会删除javax.validation。* - Adding spring-boot-starter-parent removes javax.validation.* 升级spring-boot-starter-parent后,WebApplicationInitializer无法正常工作 - WebApplicationInitializer not working after upgrading spring-boot-starter-parent 如何在不继承&#39;spring-boot-starter-parent&#39;模块的情况下测试我的Spring-Boot应用程序? - How can I test my Spring-Boot application without inheriting 'spring-boot-starter-parent' module? 我可以轻松地用spring-boot替换spring-boot-starter以避免使用spring-boot-starter-parent吗? - Can I easily replace spring-boot-starter by spring-boot to avoid use of spring-boot-starter-parent? 更新到最新的 Spring 引导版本 spring-boot-starter-parent 2.6.2 后,我的测试停止执行 - After updating to latest Spring boot version, spring-boot-starter-parent 2.6.2, my tests stop executing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM