DEV Community


Discussion on: Dependency Injection and Unit Testing with Mockito In Spring Boot Java

vtechjm profile image
Kirk-Patrick Brown Author • Edited

Thanks for your comment.The use of the interface here gives a level of abstraction from the class that will implement it.Also it allows for the addition of another implementation to be created.Say UserService2Impl, Spring boot will now have two beans and the @Qualifier annotation can be used to tell which bean should be used.

public UserResource(@Qualifier("userService2Impl") IUserService userService) {
this.userService = userService;

This in my opinion allows you to not be tightly coupled and can easily switch implementation details.I agree that for the post you could have used the concrete class and use mockito to mock the methods.However think the level of abstraction was ok for the purpose of the flexibility it adds via the interface and to be able to easily swap out the implementation class via the @Qualifier for future expansion.

Some comments have been hidden by the post's author - find out more