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

Enable AopAutoConfiguration by default for slice tests #33415

Open
raddatzk opened this issue Nov 29, 2022 · 5 comments
Open

Enable AopAutoConfiguration by default for slice tests #33415

raddatzk opened this issue Nov 29, 2022 · 5 comments
Labels
type: enhancement A general enhancement
Milestone

Comments

@raddatzk
Copy link

Affected Version: 2.7.5

I created a @WebMvcTest for a controller that implements a generated interface from an openapi spec.
When one of the implemented handlers for example is annotated with @PreAuthorized, in case of a @WebMvcTest spring will create a JdkProxy, in the case when starting this application regularly it will create a cglib proxy.

Unfortunately spring will not detect any handlers in the case of a @WebMvcTest, as it can't find any annotations on the JdkProxy (RequestMappingHandlerMapping:isHandler(Class<?>)) and the test will fail with a 404.

I would expect that both cases will behave similar

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 29, 2022
@wilkinsona
Copy link
Member

Can you please share a minimal sample that reproduces the problem?

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Nov 29, 2022
@raddatzk
Copy link
Author

raddatzk commented Dec 6, 2022

https://github.com/raddatzk/spring-boot-33415

The ApplicationTest succeeds while PeopleControllerTest fails with 404

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 6, 2022
@wilkinsona
Copy link
Member

Thanks for the sample.

When you're using @SpringBootTest and, therefore, full auto-configuration, Spring Boot's default AOP configuration is used and this results in the use of class- rather than interface-based proxies. As a result, the annotations on PeopleController are found.

When you're using @WebMvcTest, AopAutoConfiguration isn't included and interface-based proxies are used instead. You can work around this by importing the auto-configuration in your test:

@WebMvcTest(PeopleController.class)
@Import(SecurityConfig.class)
@ImportAutoConfiguration(AopAutoConfiguration.class)
public class PeopleControllerTest {

I'd like to discuss the need for this with the rest of the team. Ideally, it wouldn't be needed but I am not sure that we should include AopAutoConfiguration in @WebMvcTest by default as it may have some side-effects that aren't wanted by those for whom things are working fine at the moment.

@wilkinsona wilkinsona added for: team-meeting An issue we'd like to discuss as a team to make progress and removed status: feedback-provided Feedback has been provided labels Jan 17, 2023
@bwgjoseph
Copy link

I've recently encountered this issue, and found this after some search.

It will be nice if this is being mentioned in the docs under @WebMvcTest portion

@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged for: team-meeting An issue we'd like to discuss as a team to make progress labels May 11, 2023
@philwebb philwebb changed the title No handlers found in WebMvcTest for proxied RestController Enable AopAutoConfiguration by default for slice tests May 11, 2023
@philwebb philwebb added this to the 3.2.x milestone May 11, 2023
@philwebb philwebb modified the milestones: 3.2.x, 3.x Sep 11, 2023
@snicoll
Copy link
Member

snicoll commented Sep 11, 2023

When looking at this, we may need to revisit PropertyPlaceholderAutoConfiguration as well as it should probably be available by default too.

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

6 participants