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

Do not use @Autowired private variables #5

Closed
TuxCoder opened this issue Jun 21, 2020 · 2 comments
Closed

Do not use @Autowired private variables #5

TuxCoder opened this issue Jun 21, 2020 · 2 comments

Comments

@TuxCoder
Copy link

TuxCoder commented Jun 21, 2020

Please do not use @Autowired on private variables.

This makes it more or less impossible to implement nice unit tests.
(At least from my experience with spring framework)

Use auto injection into the constructor, so in a unit test the constructor can given a dummy service and it is not needed to deal with changing private variables.

ref to unit tests: #3

If I'm wrong please provide an example unit test how to replace the injected bean.

edit: don't whant to refer to a user, so quoted @Autowired

@TuxCoder
Copy link
Author

List found with grep Autowire -A 1 . -r

./src/main/java/at/roteskreuz/covidapp/CovidappApplication.java:	@Autowired
./src/main/java/at/roteskreuz/covidapp/CovidappApplication.java-	private ExposureRepository exposureRepository;
--
./src/main/java/at/roteskreuz/covidapp/validation/PublishValidator.java:	@Autowired
./src/main/java/at/roteskreuz/covidapp/validation/PublishValidator.java-	private AuthorizedAppService authorizedAppService;
./src/main/java/at/roteskreuz/covidapp/validation/PublishValidator.java:	@Autowired
./src/main/java/at/roteskreuz/covidapp/validation/PublishValidator.java-	private DeviceCheckService deviceCheckService;
./src/main/java/at/roteskreuz/covidapp/validation/PublishValidator.java:	@Autowired
./src/main/java/at/roteskreuz/covidapp/validation/PublishValidator.java-	private SafetynetAttestationService safetynetAttestationService;
--
--
./src/main/java/at/roteskreuz/covidapp/validation/ExposureKeyValidator.java:	@Autowired
./src/main/java/at/roteskreuz/covidapp/validation/ExposureKeyValidator.java-	private PublishProperties publishProperties;

@TuxCoder
Copy link
Author

Think also got fixed.

And for sure, to mention the autowired in CovidappApplication.java was not nessasary.

Haven't yet saw the this lombok framework yet, but does a good job.
Nice solution!

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

1 participant