Java 导入 org.springframework.test.context.junit4.SpringJUnit4ClassRunner 无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30513375/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:45:07  来源:igfitidea点击:

The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved

javaspringmavenspring-mvcspringjunit4classrunner

提问by Juan Carlos

I'm a newbie at Spring and this is also my very first question on StackOverflow so I'm going to try to make this as understandable as possible.

我是 Spring 的新手,这也是我在 StackOverflow 上的第一个问题,所以我会尽量让这个问题易于理解。

I'm trying to make a web service client using Spring and Maven on thistutorial: and I get this error: The import org.springframework.test.context.junit4 cannot be resolved

我正在尝试在教程中使用 Spring 和 Maven 制作 Web 服务客户端:并且出现此错误:无法解析导入 org.springframework.test.context.junit4

Here is my code:

这是我的代码:

package demo;

import hello.WsClientApplication;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //this won't import


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WsClientApplication.class)
public class WsClientApplicationTests {

    @Test
    public void contextLoads() {
    }

}

Here is my pom.xmlin case you need it.

这是我的pom.xml,以防你需要它。

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
    <artifactId>gs-consuming-web-service</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
           <dependency>
               <groupId>org.springframework.ws</groupId>
               <artifactId>spring-ws-core</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- tag::wsdl[] -->
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.12.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>hello.wsdl</generatePackage>
                    <schemas>
                        <schema>
                            <url>http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>
            <!-- end::wsdl[] -->
        </plugins>
    </build>

</project>

I have tried some other solutions in StackOverflow but I can't get it to work.

我在 StackOverflow 中尝试了其他一些解决方案,但我无法让它工作。

Thanks.

谢谢。

采纳答案by Wim Deblauwe

You need to add a dependency on spring-boot-starter-test:

您需要添加依赖项spring-boot-starter-test

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

回答by Baker

Gradle

摇篮

In gradle this would be done by adding

在gradle中,这将通过添加来完成

testCompile("org.springframework.boot:spring-boot-starter-test")

to the dependencies block as in:

到依赖项块,如下所示:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

After that it should be possible to write an import statement at the top of your class

之后应该可以在类的顶部编写导入语句

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Allowing you to use the annotation:

允许您使用注释:

@RunWith(SpringJUnit4ClassRunner.class)
public class MyAppTest {

}

回答by SekharKari

Now, if you are using latest spring-boot, 1.5.2 RELEASE, @SpringApplicationConfiguration is no longer available, instead you must use @SpringBootTest. Reference here(@spring boot starter test in Spring Boot)

现在,如果您使用最新的 spring-boot 1.5.2 RELEASE,@SpringApplicationConfiguration 不再可用,您必须使用 @SpringBootTest。参考这里(@spring boot starter test in Spring Boot

回答by Pompeu

sometimes maven start downloading dependencies and doesn't finish. Go to your .m2 folder and type:

有时 maven 开始下载依赖项并且没有完成。转到您的 .m2 文件夹并键入:

find . -name *progre*

this command will show you every file with the tag "in-progess", which are the files missing or not finished.

此命令将向您显示带有“in-progess”标签的每个文件,即丢失或未完成的文件。

delete the folder and try to update the dependencies again.

删除文件夹并尝试再次更新依赖项。

回答by Micheal

I know this is answered kind of late, but I had the same problem and I found I was able to fix it 2 ways.

我知道这回答有点晚,但我遇到了同样的问题,我发现我可以通过两种方式修复它。

  1. I was able to remove the < scope > test < / scope> tags and that removed the restriction
  2. I had an issue with intellij not properly handling it when doing tests, so I made sure it was marked as test-root, I changed it from green -> gray -> back to the green test-root and that resolved my issue.
  1. 我能够删除 <scope> test </scope> 标签并删除了限制
  2. 我在做测试时遇到了 intellij 没有正确处理它的问题,所以我确保它被标记为 test-root,我将它从绿色 -> 灰色 -> 改回绿色 test-root 并解决了我的问题。

回答by Eduardo B

Just to improve in the situation.

只是为了改善这种情况。

I managed to fix it like this:

我设法像这样修复它:

before, spring boot initializr created a dependency to spring-boot-starter-testbut with the exclusion, like this:

之前,spring boot initializr 创建了对spring-boot-starter-test的依赖, 但排除了,如下所示:

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Eclipse accused the annotation @Runwith to be Junit 4, therefore, the logic thing to do was to remove the exclusion.

Eclipse 指责注释@Runwith 是 Junit 4,因此,逻辑上要做的是删除排除项。

Final POM dependency:

最终的 POM 依赖:

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