codeconsole commented on code in PR #16496: URL: https://github.com/apache/grails-core/pull/16496#discussion_r4175359265
########## grails-data-mongodb/boot-plugin/src/test/groovy/org/grails/datastore/gorm/mongodb/boot/autoconfigure/discovered/MongoDbGormAutoConfigurationDiscoverySpec.groovy: ########## @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.datastore.gorm.mongodb.boot.autoconfigure.discovered + +import grails.gorm.annotation.Entity +import grails.gorm.transactions.Transactional + +import org.springframework.boot.WebApplicationType +import org.springframework.boot.autoconfigure.AutoConfiguration +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.builder.SpringApplicationBuilder +import org.springframework.boot.context.annotation.ImportCandidates +import org.springframework.context.ConfigurableApplicationContext +import org.springframework.stereotype.Service + +import org.apache.grails.testing.mongo.AbstractMongoGrailsExtension +import org.apache.grails.testing.mongo.AutoStartedMongoSpec +import org.grails.datastore.gorm.mongodb.boot.autoconfigure.MongoDbGormAutoConfiguration +import org.grails.datastore.mapping.mongo.MongoDatastore + +/** + * Spring Boot finds an auto-configuration by reading + * {@code META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports}. Nothing here imports + * {@link MongoDbGormAutoConfiguration}, so the application gets GORM only if that file names it. + */ +class MongoDbGormAutoConfigurationDiscoverySpec extends AutoStartedMongoSpec { + + @Override + boolean shouldInitializeDatastore() { + false + } + + void 'the GORM for MongoDB auto-configuration is registered where Spring Boot reads it'() { + expect: + MongoDbGormAutoConfiguration.name in ImportCandidates.load(AutoConfiguration, getClass().classLoader).candidates + } + + void 'a Spring Boot application gets GORM for MongoDB without importing anything'() { + when: + ConfigurableApplicationContext context = new SpringApplicationBuilder(DiscoveringApplication) Review Comment: Done in edecc4cf47. `ApplicationSpec` starts `example.Application` the way `main` does, against the example's MongoDB container and a database of its own. It checks that Spring injects the `BookService` GORM implements into `BookController`, that the books the application saves on startup are listed over HTTP, and that `/books/It` finds one through the service. With the imports file moved back to `META-INF/services/spring/`, the application fails to start with `No qualifying bean of type 'example.BookService' available`. Writing it turned up two faults in `SpringBootStartMongoExtension`, fixed in 44eed4de74. Its shared-initializer interceptor never called `proceed()`, so any `@Shared` field a spec initialized itself stayed null. And its `stop()` called `start()` on the container. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
