title: Spring Boot Error When Running Unit Tests
body: <p>After almost 8 years, I have to dust my Spring knowledge and things were good as long as I did not have to write unit tests. I have the following unit test that would test one of my service and when I tried to run it, it would fail with an:</p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException
  </code></pre>
  
  <p>and it is not able to resolve the eMailNotificationService service!</p>
  
  <p>So here is my Unit test:</p>
  
  <pre><code>@ActiveProfiles("test")
  @ComponentScan(basePackages = {"com.middleware.service.email", "it.ozimov.springboot.templating.mail"})
  @RunWith(SpringRunner.class)
  public class EMailNotificationServiceTest {
  
      @Autowired()
      private EMailNotificationService eMailNotificationService;
  
      @MockBean(name = "emailService")
      private EmailService emailService;
  
      @Test
      public void sendResetPasswordEMailNotification() {
  
          System.out.println(eMailNotificationService);
  
          // TODO: complete the test
      }
  }
  </code></pre>
  
  <p>The EMailNotificationService is as below and this is defined in the com.middleware.service.email package:</p>
  
  <pre><code>@Service()
  @Scope("singleton")
  public class EMailNotificationService {
  
      private static Log logger = LogFactory.getLog(EMailNotificationService.class);
  
      @Value("${service.email.sender}")
      String senderEMail;
  
      @Value("${service.email.sender.name}")
      String senderName;
  
      @Value("${service.email.resetPassword.link}")
      String resetPasswordLink;
  
      @Autowired
      public EmailService emailService;
  
      public void sendResetPasswordEMail(List&lt;EMailUser&gt; userList) {
          List&lt;String&gt; allEMails = userList.stream()
                                      .map(EMailUser::getUserEMail)
                                      .collect(Collectors.toList());
          userList.stream().forEach(emailUser -&gt; {
              final Email email;
              try {
                  email = DefaultEmail.builder()
                          .from(new InternetAddress(senderEMail, senderName))
                          .to(Lists.newArrayList(new InternetAddress(emailUser.getUserEMail(), emailUser.getUserName())))
                          .subject("Reset Password")
                          .body("")//Empty body
                          .encoding(String.valueOf(Charset.forName("UTF-8"))).build();
  
                  // Defining the model object for the given Freemarker template
                  final Map&lt;String, Object&gt; modelObject = new HashMap&lt;&gt;();
                  modelObject.put("name", emailUser.getUserName());
                  modelObject.put("link", resetPasswordLink);
  
                  emailService.send(email, "resetPasswordEMailTemplate.ftl", modelObject);
              } catch (UnsupportedEncodingException | CannotSendEmailException ex) {
                  logger.error("error when sending reset password EMail to users " + allEMails, ex);
              }
          });
      }
  }
  </code></pre>
  
  <p>How do I write my unit test such that my service is injected / autowired?</p>