[英]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 应用程序,如果我不导入以下测试依赖项,我的测试用例中将面临编译错误
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 ?为什么会这样?
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.