java 导入 org.springframework.test.context.junit4.SpringRunner 无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45896905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The import org.springframework.test.context.junit4.SpringRunner cannot be resolved
提问by HKhan
I'm trying to resolve this from sometime now.But Still i didn't get it solved.I just downloaded a spring project form spring.io and just imported it as a Maven project to Eclipse IDE.After that i get this above mentioned error.
我现在正在尝试解决这个问题。但我仍然没有解决它。我只是从 spring.io 下载了一个 spring 项目,然后将它作为 Maven 项目导入到 Eclipse IDE。之后我得到了上面提到的错误。
The import org.springframework.test.context.junit4 cannot be resolved
导入 org.springframework.test.context.junit4 无法解析
I have attached my code below.Together with the full pom.xml content.
我在下面附上了我的代码。连同完整的 pom.xml 内容。
package com.Ticket.Booking.TicketBooking;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;//I got the error in this line
@RunWith(SpringRunner.class)//Also in this line too.
@SpringBootTest
public class TicketBookingApplicationTests {
@Test
public void contextLoads() {
}
}
Here's my pom.xml file.
这是我的 pom.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.Ticket.Booking</groupId>
<artifactId>Ticket-Booking</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Ticket-Booking</name>
<description>Project For Book Movie Tickets Online.</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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-starter-data-rest</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
Please help me to solve this issue.It's very much important for me as a student.
请帮我解决这个问题。这对我作为学生来说非常重要。
回答by Jose Martinez
It could be the scope, which is set to test
.
它可能是范围,设置为test
.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Please make sure that the class that has the SpringRunner
is in your /src/test/java
folder.
请确保具有 的班级SpringRunner
在您的/src/test/java
文件夹中。
回答by da-vinci-code
I had the latest eclipse and also the latest junit 4.12 jar but still I use to get this problem. I finally fixed it. Follow these steps:
我有最新的 Eclipse 和最新的 junit 4.12 jar,但我仍然使用它来解决这个问题。我终于修好了。跟着这些步骤:
- Close your maven project created in eclipse and close eclipse or STS tool.
- Go to Maven repository:
- 关闭在 eclipse 中创建的 Maven 项目并关闭 eclipse 或 STS 工具。
- 转到 Maven 存储库:
C:\Users\.m2\repository\org\springframework\
C:\Users\.m2\repository\org\springframework\
and delete everything inside springframework folder.
并删除 springframework 文件夹中的所有内容。
- Now open eclipse/STS tool and then open your project.
- The project will build automatically and the error disappears.
- If it doesn't then
- 现在打开 eclipse/STS 工具,然后打开您的项目。
- 项目将自动构建,错误消失。
- 如果没有那么
right click on project -> Maven -> Update Project.. -> check the Force Update and click OK
右键单击项目 -> Maven -> 更新项目.. -> 检查强制更新并单击确定
回答by Sikandar
I think a dependency is missing:
我认为缺少依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.10.RELEASE</version>
<scope>test</scope>
</dependency>
回答by ValerioMC
Why are your using a BUILD-SNAPSHOT?
为什么要使用 BUILD-SNAPSHOT?
In your pom.xml you have
在你的 pom.xml 你有
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
use version
使用版本
<version>2.0.4.RELEASE</version>
or another RELEASE version.
或其他 RELEASE 版本。
Don't override library version unless is really necessary.
除非真的有必要,否则不要覆盖库版本。