我想在Sprint Boot Controller上运行集成测试和单元测试
package com.steinko.reactspringboottutorial.webserver;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Controller
public class HomeController {
@Value("${frontendurl}")
private String frontendurl;
private final Logger log = LoggerFactory.getLogger(HomeController.class);
@RequestMapping("/" )
String index(HttpServletRequest request) {
log.info("Request Mapping");
log.info(request.toString());
return "index";
}
@RequestMapping("/frontendurl" )
String frontendurl() {
return frontendurl;
}
}
启动Spring Boot程序的类如下: 包com.steinko.reactspringboottutorial.webserver;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.CommandLineRunner;
@SpringBootApplication
public class FrontendWebServer {
public static ConfigurableApplicationContext configurableApplicationContext;
public static void main(String[] args) {
configurableApplicationContext = SpringApplication.run(FrontendWebServer.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
我想用看起来像这样的集成测试来测试控制器
package com.steinko.reactspringboottutorial.webserver;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.steinko.reactspringboottutorial.webserver.HomeController;
import org.springframework.test.context.TestPropertySource;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.containsString;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
@ExtendWith(SpringExtension.class)
@TestPropertySource("frontendurl=localhost:4000")
@WebMvcTest(controllers = HomeController.class)
public class HomeControllerIntegrationTest {
@Autowired
private MockMvc mvc;
@Test
public void givenHomeUrl_whenGetHomePage_thenStatus200()
throws Exception {
mvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().contentType("text/html; charset=UTF-8"))
.andExpect(content().string(containsString("Todo fronten")));
}
@Test
public void shouldReciveFrontendUrl() throws Exception {
this.mvc.perform(get("/frontendurl"))
.andExpect(status().isOk())
.andExpect(content().contentType("text/plain; charset=UTF-8"))
.andExpect(content().string(containsString("localhost:4000")));
}
}
我通过运行gradle build执行测试。 gradle.build文件如下所示:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '2.1.5.RELEASE'
id "com.moowork.node" version "1.3.1"
id 'jacoco'
}
apply plugin: 'io.spring.dependency-management'
jacoco {
toolVersion = "0.8.3"
reportsDir = file("$buildDir/customJacocoReportDir")
}
jacocoTestReport {
reports {
xml.enabled = true
}
}
group = 'com.steinko.reactspringboottutorial.webserver'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
sourceSets {
main {
resources {
srcDirs = [
'src/main/resources'
]
}
}
}
ext {
set('springCloudVersion', "Greenwich.SR1")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-gcp-starter'
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.2.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
node {
/* gradle-node-plugin configuration
https://github.com/srs/gradle-node-plugin/blob/master/docs/node.md
Task name pattern:
./gradlew npm_<command> Executes an NPM command.
*/
// Version of node to use.
version = '10.16.0'
// Version of npm to use.
npmVersion = '6.9.0'
// If true, it will download node using above parameters.
// If false, it will try to use globally installed node.
download = false
}
执行测试结果时出现错误信息 java.lang.IllegalStateException:无法加载ApplicationContext
我该怎么做才能加载ApplicationContext?