Unit testing of Spring Service with constructor dependencies
Constructor based dependency injection is the recommended way to use dependency injection in Spring. In this post I'm going to demonstrate how to write unit test of a service class which has dependencies on other beans e.g. repositories. Let's assume our service class is like the following: @Service class MyService { private final UserRepository userRepository; private final AddressRepository addressRepository; @Autowired public MyService(UserRepository userRepository, AddressRepository addressRepository) { this.userRepository = userRepository; this.addressRepository = addressRepository; } public boolean isUserHasAddress(String username){ // omitting the method details, will return true always return true; } } Now we need to write unit test for the above class. Here is a way to do it: @RunWith(S...