@SpringBootTest的问题:上下文没有加载,Spock无法@Autowire

tho*_*omi 5 spring spring-test spock spring-boot

我刚刚转到Spring Boot 1.4.0来使用新的测试功能.但是,我似乎无法将它们与Spock Framework结合使用.请考虑以下规范:

@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

    }

}
Run Code Online (Sandbox Code Playgroud)

问题是注释似乎甚至无法找到MainWebApplication.class(坐在层次结构中较高的包上),因此,没有上下文,所以没有任何内容Autowire.我意识到这是一个非常新的东西,但到目前为止的文档并不是很好,所以可能这个问题也会对其他人有所帮助.

Mil*_*vić 3

当前版本的 Spock 与 Spring Boot 1.4 的@SpringBootTest注解不兼容,如果您想使用新的注解,您应该将 Spock 版本升级到 1.1。

欲了解更多详情,请参阅此答案