在Spock规范中注入时,WebApplicationContext不会自动装配

tho*_*omi 5 spring maven spock spring-boot mockmvc

虽然我在尝试时遵循了Spring Boot Guide:

@SpringApplicationConfiguration(classes=MainWebApplication.class, initializers = ConfigFileApplicationContextInitializer.class)
@WebAppConfiguration
@ActiveProfiles("integration-test")

class FirstSpec extends Specification{
  @Autowired
  WebApplicationContext webApplicationContext

  @Shared
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build()

  def "Root returns 200 - OK"(){

      when:
      response = mockMvc.perform(get("/"))

      then:
      response.andExpect(status().isOk())
  }
}
Run Code Online (Sandbox Code Playgroud)

我只是得到了一个消息,即没有注入WebApplicationContext.我有

    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-spring</artifactId>
    </dependency>
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-maven</artifactId>
        <version>0.7-groovy-2.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

在我的.pom中,正如指南建议的那样,但仍然没有成功.我错过了什么?我需要应用程序上下文,因此所有bean都被注入.有任何想法吗?

Dee*_*rda 2

您可以尝试将mockMvc构造移至setup方法中吗?

def setup() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build()
}
Run Code Online (Sandbox Code Playgroud)