gpt4 book ai didi

spring - @SpringBootTest : Context not loading and Spock unable to @Autowire 的问题

转载 作者:行者123 更新时间:2023-12-04 15:54:52 24 4
gpt4 key购买 nike

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

@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

}

}

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

最佳答案

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

更多详情请见this answer .

关于spring - @SpringBootTest : Context not loading and Spock unable to @Autowire 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38764318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com