简体   繁体   English

如何通过运行 spring 启动测试代码解决此问题?

[英]How can I solve this problem with run spring boot test code?

This is my test code in spring boot.这是我在 spring 启动时的测试代码。

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));
    }
}

And this is error message.这是错误信息。

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

I don't know how to solve this problem...我不知道如何解决这个问题...

I tried these.我试过这些。

  1. Changed Settings > Build, Execution, Deployment > Build Tools > Gradle > Run test using > Changed "Gradle (Default)" to "IntelliJ IDEA"已更改设置 > 构建、执行、部署 > 构建工具 > Gradle > 使用运行测试 > 将“Gradle(默认)”更改为“IntelliJ IDEA”

  2. Add secure = false in @WebMvcTest@WebMvcTest中添加secure = false

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

But it does not still work... Please help me...但它仍然不起作用......请帮助我......

Also this is my build.gradle .这也是我的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')
}

It looks like you are running your test using JUnit 5, but declare your test using a JUnit 4 annotation ( org.junit.Test vs. org.junit.jupiter.api.Test ) and that's why it isn't picking up on your test.看起来您正在使用 JUnit 5 运行测试,但使用 JUnit 4 注释声明您的测试( org.junit.Testorg.junit.jupiter.api.Test ),这就是为什么它没有接受您的原因测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我想使用存储库时如何解决这个 Spring Boot 问题 - How to solve this Spring Boot problem when I want to use a Repository 如何将 spring bean 添加到 Spring Boot 测试类? - How can I add a spring bean to a Spring Boot test class? 无法运行Spring Boot测试应用程序 - Can't run Spring Boot test application 如何解决 Spring Boot 中的 ConstraintViolationException - how do I solve the ConstraintViolationException in Spring Boot 当我运行我的测试用例时,我的 spring-boot 的主要方法被唤起并且我的 mockito 方法不起作用。 如何解决这个问题? - When I run my test case, my main method of spring-boot gets evoked and my mockito method doesn't work. How to solve this? 如何在Spring Boot应用中测试验证器? - How can I test a validator in a Spring Boot app? 在Spring引导测试中,如何创建一个bean并通过@Autowired注入? - In a Spring boot test, how can I create a bean and injects by @Autowired? 如何在 Spring Boot 中测试分页结果? - How can i test paginated result in spring boot? 如何解决Spring Boot应用程序中的FlyWay许可证问题 - How to solve FlyWay license problem in Spring Boot Application 如何解决spring开机测试中的null指针异常? - How to solve null pointer exception in spring boot test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM