What is difference between using and not using @Autowired Constructor in springboot test with kotlin

Viewed 399

I wanna write test code using JUnit5 in spring boot 2.3.0.

First of all, I tried dependency injection using constructor.

Like below

enter image description here

But when I started my test code, whole of test was failed.

Error log was No ParameterResolver registered for parameter blah blah blah...

So I thought Bean Injenction is failed even though I was using constructor.

In addition, The Bean Icon didn't show up even though I used the constructor In Intellij IDE.

But when I was added @Autowired constructor at constructor, Bean Icon began to appear.

Like below

enter image description here

Resources Class is a bean which is only uesd in test code.

I could solve the problem but I really didn't understand what happend.

I wanna know What is difference between using and not using @Autowired Constructor

Please let me know how this magical @Autowired Constructor works

1 Answers

@Autowired on the constructor of a test case is a signal to the test framework (in this case to the Jupiter SpringExtension) to resolve constructor parameters using the Spring context. Without the annotation Jupiter tries to find parameter resolvers through other mechanisms but doesn’t find any, hence the error.

Related