【问题标题】:Spring-test not finding SpringRunner class in testsSpring-test 在测试中找不到 SpringRunner 类
【发布时间】:2018-01-22 13:38:44
【问题描述】:

当我使用 SpringInitializr 生成一个新项目并包含 spring-test 时,一切都很好......除了一件事。它创建的一个测试无法解析 SpringRunner 类。它包括一个 @RunWith(SpringRunner.class) 并且从我阅读的所有内容来看,这应该对 spring-boot 1.5.9 有效。这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</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</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>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

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

我正在使用 Eclipse。从我在 Maven 依赖项中看到的情况来看,一切正常。它使用的是 spring-test-4.3.13.RELEASE.jar。我打开它,看到它有 SpringRunner 类。任何想法我做错了什么?我从 SpringInitializr 创建的任何项目都是相同的(我在 SpringInitializer 中使用 1.5.9 默认创建它)

测试类开箱即用:

    package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
    }

}

--更新...所以,我可以在 pom.xml 文件中恢复 spring-test 次要版本...从 4.3.13 到 4.3.12 并找到它。它似乎只是 4.3.x 主要版本的最新(推荐)版本。在这一点上,我有一个解决方法,但如果有人有想法,我想成为最新的。正如我上面提到的,我可以打开 4.3.13 的 jar 文件并找到 SpringRunner 类

【问题讨论】:

  • 一个补充...如果我在 1.4.7 版本下运行初始化程序,eclipse 会发现 SpringRunner.class
  • 也粘贴你的测试类
  • 更新了测试类的帖子

标签: spring-boot


【解决方案1】:

将 spring-boot-starter-parent 更新为 2.0.1.RELEASE

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->

【讨论】:

  • 这是从我试图让它工作的地方遗留下来的......以及我如何发现这是我正在处理的 2.x 问题
猜你喜欢
  • 2019-07-30
  • 2020-11-11
  • 1970-01-01
  • 2017-03-06
  • 2018-03-10
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多