Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grails Application is not annotated with @SpringBootApplication or @SpringBootConfiguration #14012

Open
jdaugherty opened this issue Feb 11, 2025 · 8 comments
Assignees

Comments

@jdaugherty
Copy link
Contributor

Issue description

In attempting a merge of the mongodb & data-mapping repos, it was noticed that unless the Application class is annotated with @SpringBootApplication then the grails test could not use a different application class. The test also had to be annotated with @SpringBootTest(useMainMethod = SpringBootTest.UseMainMethod.ALWAYS)

@jdaugherty
Copy link
Contributor Author

@rainboyan Do you have any thoughts on this problem? Someone mentioned you solved a similar issue with Grace.

@rainboyan
Copy link
Contributor

@jdaugherty Could you provide a minimal sample to reproduce the problem and give me some details about the errors?

@jdaugherty
Copy link
Contributor Author

You can see an example here: https://github.com/grails/grails-core/blob/7.0.x/grails-test-examples/app1/src/integration-test/groovy/functionaltests/ConfigTestControllerSpec.groovy

This class adds an Application class for the test, but it does not invoke that class. I only noticed this because we changed the banner and that test showed a Spring banner instead of a grails banner.

If you annotate the Application class with '@SpringBootApplicationand then add the annotation@SpringBootTest(useMainMethod = SpringBootTest.UseMainMethod.ALWAYS)` to the test. It will work. We are trying to decide if we should:

  1. Add the @SpringBootApplication to every grails application class.
  2. Change the testing plugins to add the useMainMethod when it adds the SpringBootTest annotation to a test class in the AST

@jdaugherty
Copy link
Contributor Author

The reason to do this is in case someone is doing any setup prior to calling run app. This ensures the tests also execute the same logic that the other run methods use.

@jamesfredley
Copy link
Contributor

The Spring Boot banner also displays on Grails commands instead of the Grails banner, such as:

./grails dbm-generate-changelog test.groovy

with grails 7.0.0-SNAPSHOT and grails-database-migration:6.0.0-M1

@rainboyan
Copy link
Contributor

@jdaugherty Sorry, I don't quite understand what you mean, the example app only have one application class Application, if you have not set the applicationClass = Application it will be search the default application class.

I think the problem you describe should be related to Grails integration testing, especially the use of '@Integration'. This is a Groovy ASTTransformation, What it does is add the annotation @SpringBootTest to the Spock tests.

As far as I know, Grails doesn't currently support the use of @SpringBootTest in Spring Boot applications.

Maybe I said before, Grails applications are not exactly Spring Boot applications. That why we should add @SpringBootApplication or @SpringBootConfiguration to the Grails application class.

We can do this in ApplicationClassInjector, try to add @SpringBootConfiguration to the application class, if Grails still doesn't plan to support @ComponentScan.

But I think '@SpringBootApplication' should be used to make sure Grails is consistent with Spring Boot.

@jdaugherty
Copy link
Contributor Author

@rainboyan Thank you. I think it makes sense to add one of these annotations based on your comment - this background is what I needed. You're right this test doesn't have multiple application classes, but if you debug it, you'll see it's never invoking the one that exists in the project for tests because of the missing annotation / spring boot test setting. This in turn is what's causing the wrong banner on running the test.

Thank you for also pointing out @SpringBootConfiguration vs @SpringBootApplication. @SpringBootConfiguration does fix this issue as well. If we add @SpringBootApplication, it will enable auto configuration by default in our grails apps. If we want to remain as close as the original implementation, @SpringBootConfiguration is what we should use.

@matrei @codeconsole @jamesfredley What are your thoughts about @EnableAutoConfiguration by default? Should we consider the @SpringBootApplication instead (which will enable autoconfiguration) or just use @SpringBootConfiguration?

@jdaugherty
Copy link
Contributor Author

Looking at the ApplicationClassInjector, it looks like it needs to take into account the @SpringBootApplication like it does for the EnableAutoConfiguration. This is likely a bug in the application injector.

@jdaugherty jdaugherty changed the title Grails Tests do not support loading a different Application class Grails Application is not annotated with @SpringBootApplication or @SpringBootConfiguration Feb 13, 2025
@jdaugherty jdaugherty self-assigned this Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants