我正在通过使端点一次反应一个来将 Spring Boot 应用程序转换为使用 webflux。大多数(Spock)测试运行良好,但我在测试中自动连接 JPA 存储库以插入数据的测试却没有。当资源(我们正在测试的)从中读取时,存储库总是空的。我用不同的存储库进行了数十次这样的测试,它们都有相同的问题。
这是一个测试示例(我们正在测试的资源只是 findById 并从存储库返回示例):
@SpringBootTest
@AutoConfigureWebTestClient(timeout = "60000")
@Transactional
class PaymentControllerIntegrationTest extends Specification {
@Autowired
WebTestClient client
@Autowired
PaymentRepository repo
def "GET /example/{id} returns correct example"() {
given:
def needle = new Example(id: 1L)
def haystack = repo.saveAll([needle, new Example(id: 2L), new Example(id: 3L)])
when:
def response = client.get().uri(EXAMPLE_URL, [id: needle.id.toString()]).exchange()
then:
response.expectStatus().isOk()
response.returnResult(ExampleResponse.class).getResponseBody().blockLast().id == needle.id
}
当我在控制器中放置断点并执行 findAll() 时,存储库始终为空。
- 我尝试改用 TestEntityManager.persistAndFlush
- 我尝试使用 repo.saveAllAndFlush 代替
- 我尝试过自动装配 ApplicationContext ,然后从中构建 WebTestClient ,但我从来没有让自动装配工作
我目前最好的猜测是测试设置不正确(应用程序上下文?)所以测试中的存储库与应用程序中的存储库不同。