gpt4 book ai didi

spring - 可怕的 “Unable to start EmbeddedWebApplicationContext”错误

转载 作者:行者123 更新时间:2023-12-03 06:00:34 25 4
gpt4 key购买 nike

尝试运行集成测试时,我遇到以下异常:

org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

我阅读了许多论坛条目,但未找到任何解决方案。我的文件如下:

整合测试
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = WebInitializer.class)
@DataJpaTest
@Sql("/db/data.sql")
public class ReportEventIntTest {
@Autowired
private TestRestTemplate restTemplate;

@Test
public void reportEvent() {
Map<String, String> eventMap = new HashMap<>();
this.restTemplate.postForEntity("/worker/event", eventMap, String.class);
}
}

Spring Boot配置
@Configuration
@ComponentScan(basePackages = {"org.reaction.engine.collector.controller",
"org.reaction.engine.persistence.service",
"org.reaction.engine.persistence.converter",
"org.reaction.engine.service"})
@EnableAutoConfiguration
@ImportResource("classpath:applicationContext.xml")
@Profile("threadPool") // define the default profile: it can be overridden by -Dspring.profiles.active=...
public class WebInitializer extends SpringBootServletInitializer implements WebApplicationInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebInitializer.class);
}

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


@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
return factory;
}
}

Gradle 文件
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.3.RELEASE'
}
}

dependencies {
compile project(':common')

compile 'org.springframework:spring-context-support'
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-integration'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.apache.httpcomponents:httpclient:4.5.2'

providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

providedCompile 'javax.enterprise.concurrent:javax.enterprise.concurrent-api:1.0'

runtime 'mysql:mysql-connector-java'

// ---------------------- TESTING ----------------------
testCompile 'com.jayway.restassured:rest-assured:2.9.0'
testCompile 'org.springframework.boot:spring-boot-starter-test'

//testRuntime 'org.hsqldb:hsqldb'
testRuntime 'com.h2database:h2'
testRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

任何的想法?
我将不胜感激任何想法!

问候,
V.

最佳答案

我读了许多stackoverflow条目,但是我错过了以下内容:
@Profile cause Unable to start EmbeddedWebApplicationContext

问题是我在spring boot config类中定义了配置文件,但是我必须在测试类中进行配置。

@ActiveProfiles("threadPool")

关于spring - 可怕的 “Unable to start EmbeddedWebApplicationContext”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42026653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com