Issues with @SpringBootTest: Context not loading and Spock unable to @Autowire
Asked Answered
W

1

6

I just moved to Spring Boot 1.4.0 to use the new test features. However, I seem to be unable to use them in conjunction with the Spock Framework. Consider the following Spec:

@SpringBootTest(classes=[MainWebApplication.class], webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
class RestTemplateExampleSpec extends Specification{

@Autowired
private TestRestTemplate restTemplate

def "Web application is Reachable and Status is 200 - OK"(){

    when: "The root address is called"
    def response = restTemplate.getForObject("/", String.class)

    then: "The status code is 200 - OK"
    response.statusCode == HttpStatus.OK

}

def "Endpoint /admin/users is available"(){

    when: "/admin/users is called"
    def response = restTemplate.getForEntity("/admin/users", String.class)

    then: "The status code is 200 - OK"
    response.statusCode == HttpStatus.OK

    }

}

The issue is that the annotation does not even seem to be able to find the MainWebApplication.class (sitting on a package higher in the hierarchy), and consequently, there is no context, so nothing to Autowire. I realize this is pretty new stuff, But documentation so far is not very good, so probably, this question will help others as well.

Woodchopper answered 4/8, 2016 at 10:4 Comment(0)
I
3

The current version of Spock is incompatible with Spring Boot 1.4's @SpringBootTest annotation, you should upgrade your Spock version to 1.1 if you wish to use the new annotations.

For more details, see this answer.

Interspace answered 4/8, 2016 at 10:27 Comment(2)
Thank you. Strange that you cannot google that. The link in the answer helpedWoodchopper
I also found out the hard way; luckily, Spock's GitHub had commits with indicative names (e.g. Spring Boot 1.4 support or something along those lines) :)Dupuis

© 2022 - 2024 — McMap. All rights reserved.