Skip to content

Constructor injection in JUnit Jupiter test classes without @Autowired #26309

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
xenoterracide opened this issue Dec 22, 2020 · 5 comments
Closed
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue

Comments

@xenoterracide
Copy link

Juni5 5 supports constructor injection, it would be nice if springs test harness supported it, so you could write spring tests in the same way you write other classes in both junit and spring core. note: I searched this repo and google and didn't find anything.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 22, 2020
@ttddyy
Copy link
Contributor

ttddyy commented Dec 22, 2020

FYI, official document has a section for constructor injection in test with JUnit5.

@xenoterracide
Copy link
Author

derp, I are dumb... though I see now what I was really remembering... not wanting to have to put @Autowired on it, reading that section, is there a reason for needing some decorator? outside of, is it the SpringExtension that's included in all @Spring*Test decorators that makes spring work?

@snicoll snicoll changed the title [Feature] test junit5 constructor injection Test junit5 constructor injection Dec 23, 2020
@snicoll
Copy link
Member

snicoll commented Dec 23, 2020

@xenoterracide there is really no need to qualify your issue with something in brackets in the description, the team can triage the issue based on title and a description.

This was discussed before, more context in #22924

@snicoll snicoll closed this as completed Dec 23, 2020
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 23, 2020
@sbrannen
Copy link
Member

derp, I are dumb... though I see now what I was really remembering... not wanting to have to put @Autowired on it, reading that section, is there a reason for needing some decorator?

I'm not sure if you read the entire documentation on the subject, but you do not have to annotate constructors with @Autowired if you change the global default.

outside of, is it the SpringExtension that's included in all @Spring*Test decorators that makes spring work?

If you're talking about test annotations in Spring Boot, then yes. Most if not all of the test related annotations in Spring Boot Test are meta-annotated with @ExtendWith(SpringExtension.class) so that JUnit Jupiter support for Spring is always enabled.

@sbrannen sbrannen changed the title Test junit5 constructor injection Constructor injection in JUnit Jupiter test classes without @Autowired Dec 23, 2020
@sbrannen sbrannen added the in: test Issues in the test module label Dec 23, 2020
@xenoterracide
Copy link
Author

well, currently I'm using @SpringJUnitWebConfig and I missed this mentioned documentation because I didn't see any constructor injection being used in this code.

On a related note, if anyone has any ideas about this https://stackoverflow.com/q/65418290/206466. I've tried quite a few things :'( . However this isn't the place for questions, though having read over the docs in spring security on webmvc integration a couple of times I wonder if the docs there couldn't be improved.

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 status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants