Starting from Spring 5, we don't need to specify @Autowired inside a Spring bean.
For instance I can do this and it works just fine:
@Service
public class OwnerService {
private final OwnerRepository ownerRepository;
public OwnerService(OwnerRepository ownerRepository) {
this.ownerRepository = ownerRepository;
}
}
However, in a Spring Boot test, @Autowired still has to be explicit.
@SpringBootTest
public class VisitServiceTest {
private final VisitService visitService;
@Autowired
public VisitServiceTest(VisitService visitService) {
this.visitService = visitService;
}
}
Anybody understands why Spring Boot Tests are different?
Top comments (2)
thanks Stephane!
JUnit is creating your test, not Spring. The autowiring support there is actually an integration between JUnit and Spring and autowiring is just one kind of parameter that you can inject there.