【问题标题】:tests for spring boot application are not run未运行 Spring Boot 应用程序的测试
【发布时间】:2017-05-11 09:44:42
【问题描述】:

我正在尝试为 Spring Boot 应用程序编写集成:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = { "com.lapots.tree.model.web" })
public class TreeModelApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(TreeModelApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() throws Exception {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new RootServlet(), "/tree-model-app");
        registrationBean.setLoadOnStartup(1);
        registrationBean.setAsyncSupported(true);
        return registrationBean;
    }
}

测试看起来像这样

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        classes = TreeModelApplication.class)
@ExtendWith(SpringExtension.class)
public class TreeModelApplicationIntegrationTest {
    private static final String PING_URL = "http://localhost:8080/tree-model-app/ping";

    private RestTemplate restTemplate = new RestTemplate();

    @Test
    public void routerReturnsTrueOnPing() {
        String response = restTemplate.getForObject(PING_URL, String.class);
        assertEquals("false", response, "Ping response should be the same as expected.");
    }
}

但是,当我运行 gradlew test 时 - 没有任何反应 - 构建成功并且报告为空。有什么问题?

我的build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.0.BUILD-SNAPSHOT'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

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

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    jcenter()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
    maven { url 'https://jitpack.io' }
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-webflux')
    compile('org.springframework.boot:spring-boot-starter-websocket')
    compile('org.springframework.boot:spring-boot-starter-jetty')

    runtime('org.springframework.boot:spring-boot-devtools')

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile("org.springframework:spring-test")
    testCompile("org.junit.platform:junit-platform-runner:$junitPlatformVersion")
    testCompile("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion")
    testCompile("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion")
    testCompile("com.github.sbrannen:spring-test-junit5:1.0.0.M4")
}

【问题讨论】:

  • @Test 导入是否正确?我期待看到 org.junit.Test。看起来 spring 没有找到任何测试
  • 嗯,我用的是Junit5所以我用了它
  • 你能单独运行测试吗?我的意思是来自 IDE
  • 我的意思是 - 你有 gradle jUnit5 插件集吗?应用插件:'org.junit.platform.gradle.plugin'
  • 太好了,所以测试没问题。

标签: java spring-boot spring-test junit5


【解决方案1】:

所以问题是 gradle 插件没有设置为运行 jUnit 5 测试

这里需要的conf直接粘贴from JUnit5 website:

buildscript {
    repositories {
        mavenCentral()
        // The following is only necessary if you want to use SNAPSHOT releases.
        // maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4'
    }
}

apply plugin: 'org.junit.platform.gradle.plugin'

【讨论】:

【解决方案2】:

尝试将此@RunWith(SpringRunner.class) 添加到测试类声明中

【讨论】:

  • 这是jUnit5,Runner替换为@ExtendWith(SpringExtension.class)
猜你喜欢
  • 2019-05-02
  • 1970-01-01
  • 2016-07-01
  • 2020-04-25
  • 2017-01-26
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 2020-07-09
相关资源
最近更新 更多