Reputation: 53
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.
The import org.springframework.test.context.junit4 cannot be resolved
I have attached my code below.Together with the full pom.xml content.
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.
<?xml version="1.0" encoding="UTF-8"?>
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.
Upvotes: 5
Views: 32444
Reputation: 3166
Why are your using a BUILD-SNAPSHOT?
In your pom.xml you have
<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.
Don't override library version unless is really necessary.
Upvotes: 0
Reputation: 359
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:
C:\Users\.m2\repository\org\springframework\
and delete everything inside springframework folder.
right click on project -> Maven -> Update Project.. -> check the Force Update and click OK
Upvotes: 8
Reputation: 21
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>
Upvotes: 1
Reputation: 12022
It could be the scope, which is set to 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.
Upvotes: 13