【问题标题】:The dreadful "Unable to start EmbeddedWebApplicationContext" error可怕的“无法启动 EmbeddedWebApplicationContext”错误
【发布时间】:2017-02-03 14:35:43
【问题描述】:

尝试运行集成测试时出现以下异常:

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'
}

有什么想法吗?
如果有任何想法,我将不胜感激!

问候, 五、

【问题讨论】:

  • 您是否使用 IntelliJ IDEA 运行测试?
  • 嗨,安迪,不,但与此同时我找到了答案。问题出在配置文件上,我必须在测试类中设置它...

标签: spring gradle spring-boot


【解决方案1】:

我阅读了许多 stackoverflow 条目,但我错过了以下一个:
@Profile cause Unable to start EmbeddedWebApplicationContext

问题是我在 Spring Boot 配置类中定义了配置文件,但我必须在测试类中这样做。

@ActiveProfiles("threadPool")

【讨论】:

    猜你喜欢
    • 2016-05-06
    • 2016-03-05
    • 2016-10-22
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2014-07-28
    • 1970-01-01
    相关资源
    最近更新 更多