Skip to content

Commit a9d783b

Browse files
Feature: Add MongoDB testing support to Grails Forge
1 parent 6c1d3d8 commit a9d783b

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
////
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
////
19+
20+
The `grails-testing-support-mongodb` library provides support for testing GORM for MongoDB with Testcontainers. This library is suitable for both unit and integration testing.
21+
22+
== Installation
23+
24+
To use the MongoDB testing support, add the following dependency to your `build.gradle`:
25+
26+
[source,groovy,subs="attributes"]
27+
testImplementation "org.apache.grails.testing:grails-testing-support-mongodb"
28+
29+
== Usage
30+
31+
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.
32+
33+
[source,groovy]
34+
----
35+
import grails.test.mongodb.MongoSpec
36+
import spock.lang.Specification
37+
38+
class MyMongoSpec extends Specification implements MongoSpec {
39+
40+
void "test mongo connection"() {
41+
expect:
42+
mongoClient != null
43+
}
44+
}
45+
----
46+
47+
The `MongoSpec` trait ensures that a MongoDB container is running and provides a `mongoClient` bean.
48+
49+
== Advanced Configuration
50+
51+
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`.
52+
53+
Example of setting the version in `build.gradle`:
54+
55+
[source,groovy]
56+
----
57+
tasks.withType(Test) {
58+
systemProperty "mongodbContainerVersion", "7.0.25"
59+
}
60+
----

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ testing:
304304
usefulProperties: Useful Properties
305305
integrationTesting: Integration Testing
306306
functionalTesting: Functional Testing
307+
testingMongodb: Testing GORM for MongoDB
307308
bestPractices:
308309
title: Building a Successful Application
309310
codeAnalysisGroovy: Code Analysis and Styling for Groovy

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ public void apply(GeneratorContext generatorContext) {
8383
}
8484

8585
if (generatorContext.isFeaturePresent(MongoFeature.class) || generatorContext.isFeaturePresent(MongoGorm.class)) {
86-
generatorContext.addDependency(testContainerTestDependency("mongodb"));
86+
generatorContext.addDependency(Dependency.builder()
87+
.groupId("org.apache.grails.testing")
88+
.artifactId("grails-testing-support-mongodb")
89+
.testImplementation());
8790
}
8891
}
8992

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

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

7171
then:
72-
template.contains('testImplementation "org.testcontainers:mongodb"')
72+
template.contains('testImplementation "org.apache.grails.testing:grails-testing-support-mongodb"')
7373
template.contains('testImplementation "org.testcontainers:testcontainers"')
7474
}
7575

@@ -80,7 +80,7 @@ class TestContainersSpec extends ApplicationContextSpec {
8080
.render()
8181

8282
then:
83-
template.contains('testImplementation "org.testcontainers:mongodb"')
83+
template.contains('testImplementation "org.apache.grails.testing:grails-testing-support-mongodb"')
8484
template.contains('testImplementation "org.testcontainers:testcontainers"')
8585
}
8686

0 commit comments

Comments
 (0)