Skip to content

Commit 250b9b3

Browse files
Feature: Add MongoDB testing support to Grails Forge
This commit adds the missing grails-testing-support-mongodb dependency to the TestContainers feature when MongoDB is selected. It also includes the necessary documentation for unit testing with MongoDB.
1 parent cb72050 commit 250b9b3

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

grails-doc/src/en/guide/testing/unitTesting/installation.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ To install the unit testing support library add the following dependency to the
2323
[source,groovy,subs="attributes"]
2424
testCompile "org.apache.grails:grails-testing-support-datamapping"
2525
testCompile "org.apache.grails:grails-testing-support-web"
26+
testCompile "org.apache.grails:grails-testing-support-mongodb"
2627

2728
NOTE: The dependencies are only required to implement unit tests for Grails Artifacts. Also, if you are not unit testing domain activity, you may not need the GORM testing support library.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Unit Testing GORM for MongoDB
2+
3+
The `grails-testing-support-mongodb` library provides support for unit testing GORM for MongoDB with TestContainers.
4+
5+
## Installation
6+
7+
To use the MongoDB testing support, add the following dependency to your `build.gradle`:
8+
9+
[source,groovy,subs="attributes"]
10+
testImplementation "org.apache.grails:grails-testing-support-mongodb"
11+
12+
## Usage
13+
14+
You can use the `grails.test.mongodb.MongoSpec` trait to test MongoDB interactions. This trait will automatically start a MongoDB container using TestContainers if one is not already running.
15+
16+
[source,groovy]
17+
----
18+
import grails.test.mongodb.MongoSpec
19+
import spock.lang.Specification
20+
21+
class MyMongoSpec extends Specification implements MongoSpec {
22+
23+
void "test mongo connection"() {
24+
expect:
25+
mongoClient != null
26+
}
27+
}
28+
----
29+
30+
The `MongoSpec` trait ensures that a MongoDB container is running and provides a `mongoClient` bean.
31+
32+
## Advanced Configuration
33+
34+
The testing support uses `AbstractMongoGrailsExtension` to manage the container. You can configure the MongoDB version using the `mongodbContainerVersion` system property, which defaults to `7.0.19`.

grails-doc/src/en/guide/toc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ testing:
296296
upgradingMixin: Upgrading From The Mixin Framework
297297
unitTestingControllers: Unit Testing Controllers
298298
unitTestingDomainClasses: Unit Testing Domain Classes
299+
unitTestingMongodb: Unit Testing GORM for MongoDB
299300
unitTestingServices: Unit Testing Services
300301
unitTestingTagLibraries: Unit Testing Tag Libraries
301302
unitTestingInterceptors: Unit Testing Interceptors

grails-forge/grails-forge-core/src/main/java/org/grails/forge/feature/database/TestContainers.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,30 @@ public void apply(GeneratorContext generatorContext) {
6666
generatorContext.getFeature(DatabaseDriverConfigurationFeature.class).ifPresent(driverConfiguration -> {
6767
String driver = "org.testcontainers.jdbc.ContainerDatabaseDriver";
6868
if (driverFeature instanceof SQLServer) {
69-
generatorContext.addTemplate("sqlserverEula", new StringTemplate("src/test/resources/container-license-acceptance.txt", "mcr.microsoft.com/mssql/server:2019-CU4-ubuntu-16.04"));
69+
generatorContext.addTemplate("sqlserverEula",
70+
new StringTemplate("src/test/resources/container-license-acceptance.txt",
71+
"mcr.microsoft.com/mssql/server:2019-CU4-ubuntu-16.04"));
7072
}
7173
urlForDatabaseDriverFeature(driverFeature).ifPresent(url -> {
72-
Configuration testConfig = generatorContext.getConfiguration("test", ApplicationConfiguration.testConfig());
74+
Configuration testConfig = generatorContext.getConfiguration("test",
75+
ApplicationConfiguration.testConfig());
7376
testConfig.put(driverConfiguration.getUrlKey(), url);
7477
testConfig.put(driverConfiguration.getDriverKey(), driver);
7578
});
76-
artifactIdForDriverFeature(driverFeature).ifPresent(dependencyArtifactId ->
77-
generatorContext.addDependency(testContainerTestDependency(dependencyArtifactId)));
79+
artifactIdForDriverFeature(driverFeature).ifPresent(dependencyArtifactId -> generatorContext
80+
.addDependency(testContainerTestDependency(dependencyArtifactId)));
7881
});
7982
});
8083
testContainerArtifactIdByTestFramework(generatorContext.getTestFramework()).ifPresent(testArtifactId -> {
8184
generatorContext.addDependency(testContainerTestDependency(testArtifactId));
8285
});
8386

84-
if (generatorContext.isFeaturePresent(MongoFeature.class) || generatorContext.isFeaturePresent(MongoGorm.class)) {
85-
generatorContext.addDependency(testContainerTestDependency("mongodb"));
87+
if (generatorContext.isFeaturePresent(MongoFeature.class)
88+
|| generatorContext.isFeaturePresent(MongoGorm.class)) {
89+
generatorContext.addDependency(Dependency.builder()
90+
.groupId("org.apache.grails")
91+
.artifactId("grails-testing-support-mongodb")
92+
.testImplementation());
8693
}
8794
}
8895

grails-forge/grails-forge-core/src/test/groovy/org/grails/forge/feature/database/TestContainersSpec.groovy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ class TestContainersSpec extends ApplicationContextSpec {
6868
.render()
6969

7070
then:
71-
template.contains('testImplementation "org.testcontainers:mongodb"')
72-
template.contains('testImplementation "org.testcontainers:testcontainers"')
71+
template.contains('testImplementation "org.apache.grails:grails-testing-support-mongodb"')
7372
}
7473

7574
void "test mongo-gorm dependency is present for gradle"() {
@@ -79,8 +78,7 @@ class TestContainersSpec extends ApplicationContextSpec {
7978
.render()
8079

8180
then:
82-
template.contains('testImplementation "org.testcontainers:mongodb"')
83-
template.contains('testImplementation "org.testcontainers:testcontainers"')
81+
template.contains('testImplementation "org.apache.grails:grails-testing-support-mongodb"')
8482
}
8583

8684
void "test testcontainers core is present when no testcontainer modules are present for gradle"() {

0 commit comments

Comments
 (0)