Hi everyone! I’m currently working with Grails 5.3...
# questions
u
Hi everyone! I’m currently working with Grails 5.3.5 and trying to use Spock together with Testcontainers, but I’m facing some difficulties. Has anyone had experience with this combination and could share some tips?
u
Grails supports using Spock with Testcontainers for integration testing. For MongoDB, you can extend from
grails.test.mongodb.MongoSpec
and use Testcontainers to start a MongoDB container in your tests; similar patterns apply for other databases. Example code and setup are shown in the official documentation, which recommends using Testcontainers to avoid running a local database instance manually. See Grails MongoDB Testing Guide for detailed examples and setup instructions. Would you like more detail or a code sample for a specific database? Sources: - Grails MongoDB Testing Guide - Grails MongoDB Testing Guide (mirror) - Grails MongoDB Testing Guide (index) - Grails MongoDB Testing Guide (index mirror) šŸ‘ 0 button šŸ‘Ž 0 button šŸ’” Hint: Mention @runllm in the thread for followups.
u
A message from kapa.ai
u
An alternative approach to using Testcontainers with Spock in Grails is to leverage the
@WithSession
annotation introduced in Grails integration testing. According to the [GitHub Pull Request #15002](https://github.com/apache/grails-core/pull/15002), this annotation allows integration tests to run with a Hibernate session bound to the thread without starting a transaction. This setup mirrors the Open Session In View (OSIV) pattern used in running applications, ensuring that integration tests behave more like the actual application runtime. This approach is particularly useful if you are testing database operations that need to reflect the application's runtime behavior, such as
save()
without
flush
or
save(flush: true)
failing without
@Transactional
. It avoids the need for Testcontainers and simplifies the testing setup by using Grails' built-in features. If you are interested in exploring this option, I can provide more details on how to configure and use the
@WithSession
annotation effectively. Sources: - [GitHub Pull Request #15002](https://github.com/apache/grails-core/pull/15002)
u
Sorry @User, I'm currently experiencing some technical difficulties. Please try again in a few minutes. If this issue persists, please reach out to the administrators of this Slack workspace.