可怕的“无法启动EmbeddedWebApplicationContext”错误

时间:2017-02-03 14:35:43

标签: spring gradle spring-boot

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

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 config

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

任何想法?
我非常感谢任何想法!

此致 诉

1 个答案:

答案 0 :(得分:0)

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

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

@ActiveProfiles("threadPool")