Skip to content

Commit 3b6a04d

Browse files
Feature: Add MongoDB testing support to Grails Forge
1 parent cb72050 commit 3b6a04d

5 files changed

Lines changed: 148 additions & 7 deletions

File tree

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
@@ -296,6 +296,7 @@ testing:
296296
upgradingMixin: Upgrading From The Mixin Framework
297297
unitTestingControllers: Unit Testing Controllers
298298
unitTestingDomainClasses: Unit Testing Domain Classes
299+
testingMongodb: Testing GORM for MongoDB
299300
unitTestingServices: Unit Testing Services
300301
unitTestingTagLibraries: Unit Testing Tag Libraries
301302
unitTestingInterceptors: Unit Testing Interceptors
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
package org.grails.forge.feature.database;
20+
21+
import io.micronaut.core.annotation.NonNull;
22+
import jakarta.inject.Singleton;
23+
import org.grails.forge.application.ApplicationType;
24+
import org.grails.forge.application.generator.GeneratorContext;
25+
import org.grails.forge.build.dependencies.Dependency;
26+
import org.grails.forge.feature.Category;
27+
import org.grails.forge.feature.DefaultFeature;
28+
import org.grails.forge.feature.Feature;
29+
import org.grails.forge.options.Options;
30+
31+
import java.util.Set;
32+
33+
@Singleton
34+
public class MongoDBTestContainers implements DefaultFeature {
35+
36+
@NonNull
37+
@Override
38+
public String getName() {
39+
return "mongodb-testcontainers";
40+
}
41+
42+
@Override
43+
public String getTitle() {
44+
return "MongoDB Testcontainers";
45+
}
46+
47+
@Override
48+
public String getDescription() {
49+
return "Adds MongoDB testing support using Testcontainers";
50+
}
51+
52+
@Override
53+
public String getCategory() {
54+
return Category.TESTING;
55+
}
56+
57+
@Override
58+
public boolean shouldApply(ApplicationType applicationType, Options options, Set<Feature> selectedFeatures) {
59+
return selectedFeatures.stream().anyMatch(f -> f instanceof TestContainers) &&
60+
(selectedFeatures.stream().anyMatch(f -> f instanceof MongoFeature) ||
61+
selectedFeatures.stream().anyMatch(f -> f instanceof MongoGorm));
62+
}
63+
64+
@Override
65+
public void apply(GeneratorContext generatorContext) {
66+
generatorContext.addDependency(Dependency.builder()
67+
.groupId("org.apache.grails.testing")
68+
.artifactId("grails-testing-support-mongodb")
69+
.testImplementation());
70+
}
71+
72+
@Override
73+
public boolean supports(ApplicationType applicationType) {
74+
return true;
75+
}
76+
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,26 @@ 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)) {
87+
if (generatorContext.isFeaturePresent(MongoFeature.class)
88+
|| generatorContext.isFeaturePresent(MongoGorm.class)) {
8589
generatorContext.addDependency(testContainerTestDependency("mongodb"));
8690
}
8791
}

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
@@ -68,7 +68,7 @@ class TestContainersSpec extends ApplicationContextSpec {
6868
.render()
6969

7070
then:
71-
template.contains('testImplementation "org.testcontainers:mongodb"')
71+
template.contains('testImplementation "org.apache.grails.testing:grails-testing-support-mongodb"')
7272
template.contains('testImplementation "org.testcontainers:testcontainers"')
7373
}
7474

@@ -79,7 +79,7 @@ class TestContainersSpec extends ApplicationContextSpec {
7979
.render()
8080

8181
then:
82-
template.contains('testImplementation "org.testcontainers:mongodb"')
82+
template.contains('testImplementation "org.apache.grails.testing:grails-testing-support-mongodb"')
8383
template.contains('testImplementation "org.testcontainers:testcontainers"')
8484
}
8585

0 commit comments

Comments
 (0)