@Autowired variables access in Spock setupSpec()
Asked Answered
C

1

6

I need to execute block of code once upon startup on the Spock tests. I cannot use @Autowired in setupSpec() which is default method for such initialisation, however @Beans would not be loaded till that time.

Found on web (dating back to 2015) source :

The behavior is a consequence of the design of Spring's TestContext framework. I don't see a way to change it without hitting other problems. The situation isn't any different when using the TestContext framework with JUnit.

It's been 6 years already, is there any clean way to do this? I want to omit dirty workarounds

Communicable answered 28/1, 2021 at 11:17 Comment(0)
A
7

You are in luck, thanks to @erdi for implementing this in Add support for injection into @Shared fields in spock-spring module, you can try the feature in the Spock Snapshot 2.0 builds, and it will be in the Spock-2.0M5 release. You need to opt-in into @Shared injection via placing @EnableSharedInjection on your specification, also really important, that you read the javadoc and understand the mentioned implications of doing that.

Amando answered 28/1, 2021 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.