In springboot project, setupSpec is execute before spring context
Describe the bug
Hi,
I am new to Spock, and I'm setting up a Spring Boot project for API automation using Spock and RestAssured.
To make HTTP calls, I need a token. I'm fetching the token in the setupSpec() method, but this method is executed before the Spring context initialization. As a result, properties can't be read from the application.properties file.
If I use setup(), it will be called for each test, and we have rate limits. I can't call it that many times.
Any help on this is highly appreciated.
To Reproduce
Do a Spring Boot project with Spock and RestAssured. add below code in any test class,
def setupSpec() { // authorizationToken.getAccessTokenFromIdp() println("setupSpec*") }
def setup() {
println("******setup*******")
}
def cleanup() {
println("******cleanup*******")
}
def cleanupSpec() {
println("******cleanupSpec*******")
}
def "Test 1"() { given: System.out.println("Test1 ") }
def "Test 2"() {
given:
System.out.println("Test2 ")
}
Expected behavior
looking for any alternate solution where I can get a token for each test class once.
Actual behavior
setupSpec() is failing to read properties from application.properties file as sping context is not initialized.
Java version
17
Buildtool version
maven 4
What operating system are you using
Windows
Dependencies
Additional context
No response
It's a feature. setupSpec() can be used to setup system properties before the Spring context is initialized to configure it (e.g. a database or Kafka connection details).
In your case you can assign a value fetched in setup() to the @Shared field and it will be visible in the others tests (assuming you will check for its present before making another call).
Alternatively, you can try to use @EnableSharedInjection to inject (with @Autowired) "something" providing a token into the @Shared field directly.
Got it. Thanks a ton for the quick response.
First of all, thanks for your previous reply.
Hi @szpak,
After spending some time researching, I've come to realize something.
TestNG's @BeforeTest annotation is commonly utilized to designate a method that should run before any test method (and after initializing the Spring context) within the associated classes.
Unfortunately, I couldn't find equivalent annotations in JUnit or Spock. This functionality appears to be missing in both frameworks.
Allow me to outline a specific scenario where TestNG's @BeforeTest annotation proves to be very useful:
Suppose we want to create an employee object using EmployeeService, which is @Autowired, and then use this employee object in all the test methods within the same class. Achieving this directly in JUnit or Spock isn't feasible. As an alternative, we can assign a value fetched in setup() to a @Shared field and include not-null checks to skip reinitialization for subsequent test executions.
Thank you.
Suppose we want to create an employee object using EmployeeService, which is @Autowired, and then use this employee object in all the test methods within the same class. Achieving this directly in JUnit or Spock isn't feasible.
Have you checked the aforementioned @EnableSharedInjection? It should allow you to inject a Spring service into a shared field and use it in setupSpec().