Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 707
thomi Avatar asked Oct 18 '22 04:10

thomi


1 Answers

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.

like image 138
Miloš Milivojević Avatar answered Nov 15 '22 06:11

Miloš Milivojević