Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApplicationContext does not Autowire when injecting in Spock Specification

Although I followed the Spring Boot Guide, when trying:

@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())
  }
}

I just get the message that a WebApplicationContext just is not injected. I do have

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

In my .pom, also, just as the guide advises, yet still no success. Anything I am missing? I need the app context so all the beans are injected. Any ideas?

like image 578
thomi Avatar asked Oct 30 '22 00:10

thomi


1 Answers

Can you try moving the mockMvc construction to the setup method?

def setup() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build()
}
like image 76
Deepak Sarda Avatar answered Nov 16 '22 07:11

Deepak Sarda