title: Spring MVC Service Junit Test Case
body: <p>I am having a Java Spring MVC application. Here is my Service class.</p>
  
  <pre><code>import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.integration.Message;
  import org.springframework.integration.MessagingException;
  import org.springframework.integration.channel.DirectChannel;
  import org.springframework.integration.core.MessageHandler;
  import org.springframework.stereotype.Service;
  
  @Service
  public class EmailService implements MessageHandler {
  
      @Autowired
      @Qualifier("receiveChannel")
      private DirectChannel messageChannel;
  
      private final Log logger = LogFactory
          .getLog(EmailService.class);
  
      public void init() {
          messageChannel.subscribe(this);
      }
  
      @Override
      public void handleMessage(Message&lt;?&gt; message) throws MessagingException {
           logger.info("Message: " + message);
  
      }
  }
  </code></pre>
  
  <p>I want to write a Test Case for the above class. This is what I have tried</p>
  
  <pre><code>import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.Mockito;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.web.WebAppConfiguration;
  import org.springframework.web.context.WebApplicationContext;
  
  //other imports
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration("classpath:webapptest")
  @ContextConfiguration(locations = {"classpath:test-applicationcontext.xml"})
  public class EmailServiceTest {
  
      @Autowired
      private EmailService emailService;
  
      @Autowired
      WebApplicationContext wContext;
  
      @Test
      public void testEmailService() throws Exception {
          emailService=Mockito.spy(new EmailService());
          Mockito.doNothing().when(emailService).init();
      }
  }
  </code></pre>
  
  <p>In my <strong>applicationcontext.xml</strong>, I am specifying one Gmail and its password (Its IMAP Access is enabled)</p>
  
  <p>What it should is when I send some message in that mail from other mails, it should just log the message (implemented in the other method of Service class)</p>
  
  <p>When I run the test case, it shows test case is success. But when I run any message to that mail id, it is not printing in my console.</p>