Integration Test with Spring Boot and Spock

后端 未结 4 1762
天涯浪人
天涯浪人 2020-12-02 11:10

What is the best way to run an integration test (e.g., @IntegrationTest) with Spock? I would like to bootstrap the whole Spring Boot application and execute some HTTP calls

4条回答
  •  鱼传尺愫
    2020-12-02 11:55

    The problem is that Spock Spring is looking for Spring's @ContextConfiguration annotation and doesn't manage to find it. Strictly speaking MyTestSpec is annotated with @ContextConfiguration as it's a meta-annotation on @SpringApplicationConfiguration but Spock Spring doesn't consider meta-annotations as part of its search. There's an issue to address this limitation. In the meantime you can work around it.

    All that @SpringApplicationConfiguration is doing is customising @ContextConfiguration with a Boot-specific context loader. This means that you can achieve the same effect by using an appropriately configured @ContextConfiguration annotation instead:

    @ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyServer.class)
    @WebAppConfiguration
    @IntegrationTest
    class MyTestSpec extends Specification {
        …
    }
    

    Update: Just to make sure it's clear (and based on the comments, it wasn't), for this to work you need to have org.spockframework:spock-spring on the classpath.

提交回复
热议问题