14

我是 Spring 的新手,这也是我在 StackOverflow 上的第一个问题,所以我将尝试让这个问题尽可能容易理解。

我正在尝试在教程中使用 Spring 和 Maven 创建 Web 服务客户端:我收到此错误:无法解析导入 org.springframework.test.context.junit4

这是我的代码:

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() {
    }

}

这是我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>

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

谢谢。

4

7 回答 7

25

您需要添加对spring-boot-starter-test

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
于 2015-05-28T17:36:10.407 回答
12

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

于 2017-04-23T10:35:21.483 回答
10

摇篮

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

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

到依赖项块,如下所示:

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

之后,应该可以在班级顶部编写导入语句

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

允许您使用注释:

@RunWith(SpringJUnit4ClassRunner.class)
public class MyAppTest {

}
于 2015-10-20T09:21:03.593 回答
1

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

  1. 我能够删除 <scope> test</scope> 标签并删除了限制
  2. 我在进行测试时遇到了 intellij 没有正确处理它的问题,所以我确保它被标记为测试根,我将它从绿色 -> 灰色 -> 改回绿色测试根,这解决了我的问题。
于 2018-09-13T03:58:23.277 回答
1

只是为了改善情况。

我设法像这样修复它:

之前,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 指责注解@Runwith 是 Junit 4,因此,逻辑上要做的事情是删除排除。

最终 POM 依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>         
</dependency>
于 2019-12-12T02:40:40.653 回答
0

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

find . -name *progre*

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

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

于 2018-08-25T14:14:16.217 回答
0

如果您没有使用 spring boot,而只使用 spring 框架,则应该为 spring-test 添加依赖项。

<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>2.5</version>
    <scope>test</scope>
</dependency>

您可以使用 mvn 检查 spring-test 的最新版本,并进行相应更新。

如果您使用的是 spring-boot 项目而不是添加 spring-boot 依赖项,如果您不使用 spring-boot,则以下内容将起作用,但不推荐。

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

注意:如果您使用的是 spring-boot 项目,<version>则无需添加。

于 2021-01-04T07:52:40.940 回答