After upgrading to the latest Spring Boot Version, several tests in classes that have the annotation "@DataJpaTest" were no longer working. Many things broke, but what was specifically weird was that "findBy" methods do not seem to query data correctly (when I run integration tests or do manual tests, they work fine). Here is a basic test I ran:
@Test
fun `When findByActivationTokenId then return Account`() {
val firstName = "John"
val lastName = "Doe"
val phone = "1234"
val email = "[email protected]"
val providerId = "google"
val providerUserId = "123"
val activationToken = UUID.randomUUID()
val account = Account(
firstName = firstName,
lastName = lastName,
email = email,
phoneNumber = phone,
providerId = providerId,
providerUserId = providerUserId,
title = "Herr",
activationTokenId = activationToken
)
entityManager.persist(account)
entityManager.flush()
val allAccounts = accountRepository.findAll()
val found = accountRepository.findByActivationTokenId(activationToken)
found.get() shouldNotBe null
found.get().providerId shouldBe providerId
found.get().providerUserId shouldBe providerUserId
}
I called "findAll()" so I can look at the result with the debugger. What I noticed was that the repository does in fact return the account exactly the way I persisted it, with the right activation token ID. The "findByActivationTokenId", however, returns an empty optional. Is this a bug or an oversight on my part? I looked into the deprecation information and found nothing specific to Spring Data JPA that could effect tests only.
CodePudding user response:
The problem is related to the embedded h2 database. With the latest upgrade, a lot of features will no longer work with JPA. Here is the changelog: http://www.h2database.com/html/migration-to-v2.html
This is a more specific comment on the implications for migration to Spring Boot 2.7 and using the new Version: https://twitter.com/odrotbohm/status/1526594686932160515
CodePudding user response:
There are various things that might go wrong here, and just as mpbeaujean I suspect the new version of H2 to be the culprit.
There are various things you can try. Which one is the right one for you depends on your specific reason to use H2.
Don't use H2. Often H2 is used in tests while something else is used in production. This is a crutch at best and I recommend to switch to Testcontainers and a docker image of the exact database version you have in production.
You could revert to H2 1.4. Just note that there is a CVE for it. If I remember correctly you can mitigate it when you make sure that the JDBC URL used is trusted. But you should research the CVE in order to decide if it is a problem for you.
If you have to use the current version of H2, or if not using it doesn't resolve the problem, you need to find out what is going on. I'd try the answers given here first. If it doesn't help, please post the class definition of your entity, the DDL for your table and the SQL including parameters that gets executed.