i usually use `@Autowired lateinit var dependency:...
# spring
l
i usually use
@Autowired lateinit var dependency: Dependency
4
m
Super, thank you. I think I tried lateinit with
val
, and that didn’t work obviously; I wasn’t thinking it through properly.
s
Be aware that the var dependency: Dependency is mutable. I would also consider looking at constructor injection where you can make the property a val again.
m
I always use constructor injection in my application classes; but that doesn’t seem to work in a junit test class.
s
why not? I tend to always use it, have not had issues with JUnit tests.
m
interesting. It’s actually the SpringJUnit class runner:
java.lang.Exception: Test class should have exactly one public zero-argument constructor
s
ah, you are injecting in a test class... I overlooked that part
my bad 😉
m
oh sorry, yeah. Trying to inject my repositories for an integration test to test the state afterwords.
s
For a test I wouldnt be that bothered 🙂 Currently its a limitation of junit4 which will be/is fixed with junit5, https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection
m
woohoo! Honestly, I haven’t even looked a little at junit 5 yet - I’m hoping I survive java 9. 😉
d
meanwhile java 10 is coming 🙂