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

Validate that JUnit Jupiter test methods and lifecycle methods are not annotated with @Autowired #25966

Closed
rwinch opened this issue Oct 23, 2020 · 1 comment
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@rwinch
Copy link
Member

rwinch commented Oct 23, 2020

It would be nice to consider adding validation of @Autowired on tests. For example, a user may be trying to use parameter injection of their beans and accidentally place @Autowired on the method rather than on the parameters. For example the following:

@Test
@Autowired
@WithMockUser
void go(MessageService messageService) {
	assertThat(messageService).isNotNull();
	assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
}

will be invoked when the test is autowired. It would fail because Spring Security's TestExecutionListener is not invoked until the time of test rather than injection. The failure looks something like this:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'example.demo.WithMockUserAutowiredMethodTest': Injection of autowired dependencies failed; nested exception is java.lang.AssertionError: 
Expecting actual not to be null
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:392)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
	at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:98)
	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$5(ClassBasedTestDescriptor.java:341)
	...
Caused by: java.lang.AssertionError: 
Expecting actual not to be null
	at example.demo.WithMockUserAutowiredMethodTest.withMockUserWhenAutowiredMethodThenSecurityContextSetup(WithMockUserAutowiredMethodTest.java:23)

I think it would be nice to add validation around what methods are @Autowired to ensure they are not on methods that are also annotated with JUnit Jupiter methods to give users a more clear explanation as to what is wrong.

Related Discussion: #18629 (comment)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 23, 2020
@sbrannen sbrannen changed the title @Autowired validation of test methods Validate that test methods are not annotated with @Autowired Oct 23, 2020
@sbrannen sbrannen added in: test Issues in the test module type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 23, 2020
@sbrannen sbrannen added this to the 5.3 GA milestone Oct 23, 2020
@sbrannen
Copy link
Member

Tentatively slated for 5.3 GA in case I determine that it's feasible to implement it before the release.

@sbrannen sbrannen self-assigned this Oct 26, 2020
@sbrannen sbrannen modified the milestones: 5.3 GA, 5.3.1 Oct 26, 2020
@jhoeller jhoeller modified the milestones: 5.3.1, 5.3.2 Nov 10, 2020
@sbrannen sbrannen changed the title Validate that test methods are not annotated with @Autowired Validate that JUnit Jupiter test methods and lifecycle methods are not annotated with @Autowired Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants