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

Jua*_*los 14 java spring spring-mvc maven springjunit4classrunner

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

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢.

Wim*_*uwe 22

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

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)


Sek*_*ari 12

现在,如果您使用的是最新的spring-boot,1.5.2 RELEASE,则不再提供@SpringApplicationConfiguration,而是必须使用@SpringBootTest.这里参考(Spring Boot中的 @ spring boot starter test)


Bak*_*ker 9

摇篮

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

testCompile("org.springframework.boot:spring-boot-starter-test")
Run Code Online (Sandbox Code Playgroud)

到依赖关系块,如:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}
Run Code Online (Sandbox Code Playgroud)

之后,应该可以在类的顶部编写一个import语句

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Run Code Online (Sandbox Code Playgroud)

允许您使用注释:

@RunWith(SpringJUnit4ClassRunner.class)
public class MyAppTest {

}
Run Code Online (Sandbox Code Playgroud)