1

我正在尝试为 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")
}
4

2 回答 2

1

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

这里需要直接从 JUnit5 网站粘贴 conf :

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'
于 2017-05-11T13:41:13.403 回答
0

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

于 2017-05-11T10:44:40.047 回答