Open piotrgajow opened 6 years ago
One workaround is to use hibernate in your test:
package sandbox
import grails.transaction.Rollback
import org.grails.orm.hibernate.HibernateDatastore
import org.springframework.transaction.PlatformTransactionManager
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification
import java.time.Year
class TestDomainSpec extends Specification {
@AutoCleanup
@Shared
HibernateDatastore hibernateDatastore
@Shared
PlatformTransactionManager transactionManager
void setupSpec() {
hibernateDatastore = new HibernateDatastore(TestDomain)
transactionManager = hibernateDatastore.getTransactionManager()
}
@Rollback
void "Dynamic finder should work with java.time.Year"() {
given:
new TestDomain(year: Year.of(2000)).save(flush: true, failOnError: true)
expect:
TestDomain.findByYear(Year.of(2000))
}
}
Another way to accomplish basically the same thing:
package sandbox
import grails.test.hibernate.HibernateSpec
import java.time.Year
class TestDomainSpec extends HibernateSpec {
void "Dynamic finder should work with java.time.Year"() {
given:
new TestDomain(year: Year.of(2000)).save(flush: true, failOnError: true)
expect:
TestDomain.findByYear(Year.of(2000))
}
List<Class> getDomainClasses() {
[TestDomain]
}
}
Both of those approaches should work with your version of Grails (3.2.8) and GORM (6.0.9.RELEASE).
I have tried the HibernateSpec
workaround and it did fix the issue, thank you!
Using dynamic finders for properties of java.time.Year type in test code results in
InvalidDataAccessResourceUsageException
-Cannot query (...) on non-existent property: (...)
, while in regular code it works fine.Debugging had shown that java.time.Year is not recognized as a simple type in MappingFactory.
Task List
Steps to Reproduce
year
)findByYear()
)Expected Behaviour
Dynamic finder should work as in non-test code.
Actual Behaviour
Using dynamic finder throws exception:
Environment Information
Example Application
GitHub repository: https://github.com/piotrgajow/Sandbox Branch:
grails-JavaTimeYearPropertyIssue
- https://github.com/piotrgajow/Sandbox/tree/grails-JavaTimeYearPropertyIssueNotable files:
Domain class. Finder working properly in Bootstrap. Finder not working in test code.