Cannot use methods of spring-boot-starter-test from maven in Single Unit Test - Spring Boot

HungryBird :

I am a beginner in Spring Boot, I am trying to run a unit test, I use maven for package managing, but it cannot import corresponding packages, which lead to some errors, for example, I cannot use the annotations from org.springframework.boot.test. Could anyone tell me how to solve this problems? Thanks!

enter image description here

The colour of spring-boot-starter-test is grey. enter image description here

Below is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mastering.spring</groupId>
  <artifactId>springboot-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M1</version>
 </parent>
 <properties>
    <java.version>1.8</java.version>
 </properties>

 <repositories>
    <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-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
        <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
 </pluginRepositories>

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
 </build>

 <dependencies>
    <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-tomcat</artifactId>
    <scope>provided</scope>
 </dependency>

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

</dependencies> 
</project>
FlyingBurger :

If you are using eclipse to develop spring boot application with maven project. when you run a unit test, you should create the test file in src/test/java enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103856&siteId=1