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