【问题标题】:How can I solve this problem with run spring boot test code?如何通过运行 spring boot 测试代码来解决这个问题?
【发布时间】:2020-12-07 16:05:09
【问题描述】:

这是我在 Spring Boot 中的测试代码。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.myname.pjt.springboot.web.HelloController;

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HelloController.class)
public class HelloControllerTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void hello_will_return() throws Exception {
        String hello = "hello";
        mvc.perform(get("/hello")).andExpect(status().isOk()).andExpect(content().string(hello));
    }
}

这是错误信息。

Testing started at 9:34 PM ...

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [com.myname.test.springboot.web.HelloControllerTest.hello_will_return](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 1s
3 actionable tasks: 2 executed, 1 up-to-date

我不知道如何解决这个问题...

我试过这些。

  1. 更改设置 > 构建、执行、部署 > 构建工具 > Gradle > 运行测试使用 > 将“Gradle(默认)”更改为“IntelliJ IDEA”

  2. @WebMvcTest中添加secure = false

-> @WebMvcTest(controllers = HelloController.class)@WebMvcTest(controllers = {HelloController.class}, secure = false)

但它仍然不起作用...... 请帮帮我...

这也是我的build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.7.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group 'com.myname.pjt'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

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

【问题讨论】:

  • 请详细说明
  • 只需按下public class HelloControllerTest 旁边的绿色按钮,如果仍然无法使用,请出示您的 gradle 构建文件
  • 哦,我错过了。对不起,这是我第一次使用 stackoverflow。我按下了public class HelloControllerTest 旁边的绿色按钮,我得到了那个错误。

标签: java spring spring-boot gradle


【解决方案1】:

您似乎正在使用 JUnit 5 运行测试,但使用 JUnit 4 注释(org.junit.Testorg.junit.jupiter.api.Test)声明您的测试,这就是它没有在您的测试中起作用的原因。

【讨论】:

  • 真的很抱歉,这是我第一次使用 gradle。我怎么知道我现在在用什么? JUnit4 还是 JUnit5?
猜你喜欢
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 2020-01-06
  • 1970-01-01
  • 2021-04-11
相关资源
最近更新 更多