DEV Community

loading...

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

Collapse
khmarbaise profile image
Info Comment hidden by post author - thread only visible in this permalink
Karl Heinz Marbaise

The abstraction from a class which is a service does not make sense because you implement a service with a particular function. So you can use the service directly without the need for an interface. Apart from that having only a @Qualifier will not help here ...it would be easier to use @Service(name="A") and a second implementation which can be simply being used via `@Service(name="B")... the usage of an interface would only make sense if you like to implement a strategy pattern (see dev.to/khmarbaise/spring-boot-stra...) ... with some limitations...

Furthermore you should never start with an interface. Always implement the service directly and introduce an interface only if you really need it...as you wrote for future expansion violates the YAGNI principle which means you aint gonna need it.. means you cant look into the future. Only create an interface if you really need it... Also I would be getting very sceptical if I need a different implementation for the same service? (Design?)...

Switching in implementations you usually don't know upfront.. (if it really would happen).

Apart from that using @Autowired for constructor is superfluous. Also the test class do not need to be public nor the methods because you are using JUnit Jupiter ...

You should reconsider making the whole user controller (UserResource) non public because it is not necessary.

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