强制Spring Boot不使用EmbeddedWebApplicationContext? [英] Forcing Spring Boot to NOT use EmbeddedWebApplicationContext?

查看:100
本文介绍了强制Spring Boot不使用EmbeddedWebApplicationContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spock测试用例,我想加载一个Spring应用程序。我有一个非常基本的Groovy类,它是我的配置对象:

  @Configuration 
@EnableAutoConfiguration
class TestConfig {
@Bean
Map createSillyMsgMap(){
[sillyMsg:这是一个愚蠢的消息]
}
public static void main(String [ ] args){
printlnTestConfig.main($ {args})
}
}

很明显,这不是很有用,但它是一个例子。为了方便起见,我还添加了该类中的主要方法。我的测试类只是试图将这个实例化为Spring Boot Application。例如:

  class AppContextSpec extends Specification {
deftestSpringApplication(){
given: Spring配置
SpringApplication app = new SpringApplication(TestConfig)
app.headless = true
app.webEnvironment = false
app.applicationContextClass = AnnotationConfigApplicationContext

expect:Spring应用程序能够启动
ConfigurableApplicationContext ctxt = app.run((String [])[blah])
}
}

我想通过将webEnvironment属性设置为false来强制Spring Boot不使用EmbeddedWebApplicationContext。但是不管我做什么,Spring Boot都坚持启动一个Tomcat服务器,并且它确实吸引了源代码树中用@Component和/或@Configuration标记的其他资源。在classpath上还有几个其他的应用程序上下文,当然还有一些暗示web服务类型的jar文件,但我很惊讶,类路径上的内容应该优先于通过webEnvironment属性的显式配置。我使用Gradle 1.12作为构建系统,而Spring Boot版本是1.1.4,我使用Groovy 2.3.6和Spock 0.7-groovy-2.0。任何帮助,这是表示赞赏。



我在这里完全不符合标准吗?谢谢。

解决方案

您可能不应该像您一样尝试引导Spring Boot应用程序上下文。



下面的代码创建了一个集成测试环境,而无需启动嵌入式servlet容器(尽管如果您想启动嵌入式servlet容器,使用更多注释会很容易) :

  @ContextConfiguration(loader = SpringApplicationContextLoader.class,classes = TestConfig.class)
class ExampleSpec extends Specification { b
$ b // ...

}

您还必须在测试类路径中包含'org.spockframework:spock-spring:0.7-groovy-2.0'依赖项

查看页Spring Boot官方文档的艺术和这个 SO问题


I have a Spock test case where I want to load a Spring application. I have a very rudimentary Groovy class that is my config object:

@Configuration
@EnableAutoConfiguration
class TestConfig {
  @Bean
  Map createSillyMsgMap() {
    ["sillyMsg" : "This is a silly message"]
  }
  public static void main(String[] args) {
    println "TestConfig.main(${args})"
  }
}

Obviously, this is isn't very useful, but it serves as an example. For convenience, I've also added the main method in that class. My test class just tries to instantiate this as a Spring Boot Application. Something like this:

class AppContextSpec extends Specification {
  def "testSpringApplication"() {
    given: "a Spring Configuration"
    SpringApplication app = new SpringApplication(TestConfig)
    app.headless = true 
    app.webEnvironment = false
    app.applicationContextClass = AnnotationConfigApplicationContext

    expect: "the Spring Application to be able to start"
    ConfigurableApplicationContext ctxt = app.run((String[]) ["blah"])
  }
}

I'm trying to force Spring Boot to NOT use the EmbeddedWebApplicationContext by explicitly setting the webEnvironment property to false. But no matter what I do, Spring Boot insists on starting a Tomcat server, and it does seem to pull in other resources in the source tree that are marked with @Component and/or @Configuration. There are several other application contexts on the classpath, and certainly jar files that imply web service kind of stuff, but I'm very surprised that what's on the classpath should take precedence over explicit configuration through the webEnvironment property. I'm using Gradle 1.12 as the build system, and the Spring Boot version is 1.1.4, and I'm using Groovy 2.3.6 with Spock 0.7-groovy-2.0. Any help with this is appreciated.

Am I doing something completely out of the norm here? Thanks.

解决方案

You probably shouldn't be trying to bootstrap the Spring Boot application context on your own like you are doing.

The following code creates an integration testing environment without starting the embedded servlet container (although if you want to start the embedded servlet container it would be very easy to do with a couple more annotations):

@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = TestConfig.class)
class ExampleSpec extends Specification {

    // ...

}

You'll also have to have the 'org.spockframework:spock-spring:0.7-groovy-2.0' dependency present on your test classpath

Check out this part of the Spring Boot official documentation and this SO question

这篇关于强制Spring Boot不使用EmbeddedWebApplicationContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆