WebApplicationContext does not Autowire when injecting in Spock Specification


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?





Duplicate of stackoverflow.com/questions/24405727/…?
– Andy Wilkinson
Jul 12 '16 at 21:38





Unlikely, since I tried that workaround with no success.
– thomi
Jul 13 '16 at 4:18




2 Answers
2



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


setup


def setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build()
}





That seemed to have done the trick! Great! Thank you!
– thomi
Jul 14 '16 at 17:17



You could also use SpringBean annotation available in Spock 1.2: https://objectpartners.com/2018/06/14/spock-1-2-annotations-for-spring-integration-testing/



it could be easier with this :


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

class FirstSpec extends Specification{
@Autowired
WebApplicationContext webApplicationContext

@Autowired
MockMvc mockMvc

// if any service to mock
@SpringBean
MyService myService

def "Root returns 200 - OK"(){

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

then:
response.andExpect(status().isOk())
}
}



If you don't want to mock your services you can use directly @SpringBootTest which does the same job with only one annotation


@SpringBootTest(webEnvironment = RANDOM_PORT)
@ActiveProfiles("integration-test")

class FirstSpec extends Specification{
@Autowired
WebApplicationContext webApplicationContext

@Autowired
MockMvc mockMvc

// if any service to mock
@SpringBean
MyService myService

def "Root returns 200 - OK"(){

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

then:
response.andExpect(status().isOk())
}
}





Vincent, while this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Answers that are little more than a link may be deleted.
– Shree
2 days ago





I added more details about my answer thanks for your feedback
– Vincent Couturier
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV