Skip to content

Make it easier to apply customisations to the SpringApplication created by @SpringBootTest #22405

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

Closed
wilkinsona opened this issue Jul 20, 2020 · 4 comments
Labels
type: enhancement A general enhancement
Milestone

Comments

@wilkinsona
Copy link
Member

wilkinsona commented Jul 20, 2020

When creating a SpringApplication in their application's main method, users can apply customizations to meet their needs. When these same customisations are also needed in their application's tests, reusing the same customizations isn't particularly easy. Without sub-classing SpringBootContextLoader, you can't get hold of the SpringApplication without jumping through some hoops. It's possible via a listener and the ApplicationEnvironmentPreparedEvent but that feels harder than it should.

Related: #22379, #15077

@wilkinsona wilkinsona added type: enhancement A general enhancement for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Jul 20, 2020
@philwebb philwebb added the status: pending-design-work Needs design work before any code can be developed label Jul 20, 2020
@snicoll
Copy link
Member

snicoll commented Jul 22, 2020

This looks like what we're doing for the war-based deployment stuff. What if your Spring Boot Application could implement an additional interface that provides a hook point with SpringApplicationBuilder? When this is present the callback would be invoked to customize the application. The added-benefit is that SpringBootContextLoader would do too when running an integration test.

The downside is that an existing application class would have to be rearranged a bit to move the customization of the builder in a dedicated method. That sounds like reasonable for those who care about such customizations.

@philwebb
Copy link
Member

One possible idea we discussed was something like this:

public interface SpringApplicationCustomizer {
    void customize(SpringApplication application)
}
@SpringApplication(customizer=Foo.class)
public class MyApp {
}

@philwebb philwebb added this to the 3.x milestone Jul 27, 2020
@philwebb philwebb removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Jul 27, 2020
@OLibutzki
Copy link

As #24583 got closed as duplicate I would like to expand this issue's scope to support context hierarchies.

@wilkinsona
Copy link
Member Author

@OLibutzki Any customizations that are made to a SpringApplication, either directly or via SpringApplicationBuilder, are in scope for this issue. That includes context hierarchies.

@scottfrederick scottfrederick added the status: blocked An issue that's blocked on an external project change label Nov 1, 2021
philwebb added a commit to philwebb/spring-boot that referenced this issue Sep 13, 2022
Add a new `useMainMethod` attribute to `SpringBootTest` which can be
used to determine how the test should run. The three available options
are:

	- `ALWAYS`
	- `NEVER`
	- `WHEN_AVAILABLE`

The default is `WHEN_AVAILABLE` which will attempt to launch the test
using the `main` method if there is one.

The `SpringBootContextLoader` has been updated to use the new
`SpringApplicationHook` interface when the main method is being used.

Closes spring-projectsgh-22405
philwebb added a commit to philwebb/spring-boot that referenced this issue Sep 13, 2022
Add a new `useMainMethod` attribute to `SpringBootTest` which can be
used to determine how the test should run. The three available options
are:

	- `ALWAYS`
	- `NEVER`
	- `WHEN_AVAILABLE`

The default is `WHEN_AVAILABLE` which will attempt to launch the test
using the `main` method if there is one.

The `SpringBootContextLoader` has been updated to use the new
`SpringApplicationHook` interface when the main method is being used.

Closes spring-projectsgh-22405
@snicoll snicoll modified the milestones: 3.x, 3.0.0-M5 Sep 15, 2022
@snicoll snicoll removed status: blocked An issue that's blocked on an external project change status: pending-design-work Needs design work before any code can be developed labels Sep 15, 2022
philwebb added a commit that referenced this issue Sep 15, 2022
Refine `SpringBootContextLoader` so that calls to the main method do
not exit early and the hook is only used when necessary.

See gh-22405
izeye added a commit to izeye/spring-boot that referenced this issue Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants