title: Mockito DAO interface from service impl
body: <p>I'm using Mockito for testing my API Service REST, i'm using Spring Framework 4.1.4.RELEASE and Mockito 1.9.5.</p>
  
  <p>I have a multi-brand system for access to unique DAOImpl (With unique for each brand @PersistenceContext(unitname = "emX"), where "X" it's brand char) , each brand extends from abstract DaoBase class (who implemements an interface called Dao) and i access from Service class obtaining the component via:</p>
  
  <p>applicationContext.getBean(qualifier.concat(brand.name()), clazz)</p>
  
  <p>(Where qualifier.concat(brand.name()) its = "DaoA" for example)</p>
  
  <p>This is the structure:</p>
  
  <blockquote>
    <ul>
    <li>es.app.service
    
    <blockquote>
      <ul>
      <li>ServiceClass.java calls DaoFactory to get Impl.</li>
      </ul>
    </blockquote></li>
    <li>es.app.dao
    
    <blockquote>
      <ul>
      <li>Dao.java</li>
      <li>DaoBase.java implements Dao</li>
      <li>DaoFactory.java</li>
      </ul>
    </blockquote></li>
    <li>es.app.dao.impl
    
    <blockquote>
      <ul>
      <li>DaoAImpl.java extends DaoBase @Repository("DaoA")</li>
      <li>DaoBImpl.java extends DaoBase @Repository("DaoB")</li>
      <li>DaoCImpl.java extends DaoBase @Repository("DaoC")</li>
      <li>DaoDImpl.java extends DaoBase @Repository("DaoD")</li>
      </ul>
    </blockquote></li>
    </ul>
  </blockquote>
  
  <p>Then i have a test class on Test Packages</p>
  
  <blockquote>
    <ul>
    <li>es.app.service
    
    <blockquote>
      <p>ServiceClassTest.java</p>
    </blockquote></li>
    </ul>
  </blockquote>
  
  <p>I put a little code for each class here:</p>
  
  <blockquote>
    <p>DaoFactory.java</p>
  </blockquote>
  
  <pre><code>@Component
  public class DaoFactory implements Serializable, ApplicationContextAware{
  
      private ApplicationContext  ctx;
      private static final long   serialVersionUID    = 1L;
      private static final Logger LOGGER  = LoggerFactory.getLogger(DaoFactory.class);
  
      public Dao getInstance(GroupBrand brand){
          switch(brand){
              case A:
              case B:
              case C:
              case D:
                  return (Dao)getComponent("Dao",Dao.class, brand);
              default: break;
          }
          throw new IllegalArgumentException("Unsupported brand ".concat(brand.name()));
      }
  
      private Object getComponent(String qualifier, Class&lt;?&gt; clazz, GroupBrand brand){
          Object instance = ctx.getBean(qualifier.concat(brand.name()), clazz);
          if(Utils.isNull(instance)){
              throw new IllegalArgumentException("Unkown business bean with Qualifier:  ".concat(qualifier.concat(brand.name())).concat("type: ").concat(clazz.getName()));
          }
          LOGGER.debug("DAO retrived: " + qualifier.concat(brand.name()) +" class: " + clazz.getName());
          return instance;
      }
  
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          this.ctx = applicationContext;
      }
  
  
  }
  </code></pre>
  
  <blockquote>
    <p>DaoBase.java</p>
  </blockquote>
  
  <pre><code>public abstract class DaoBase implements Dao {
  
  
      protected abstract EntityManager getEntityManager();
      private static final String EMPTY = "";
      private static final Logger     LOGGER  = LoggerFactory.getLogger(DaoBase.class);
  
      @Override
      public Boolean findAvailability(GroupBrand marca, String code) {
          EntityManager em = getEntityManager();
          Query query = em.createNamedQuery("Conn.isAvailability");
          query.setParameter(1, code);
          query.setParameter(2, marca.toCharCode());
          try{
              String value = (String)query.getSingleResult();
                          if(value.equalsIgnoreCase("S")){
                              return true;
                          }else{
                              return false;
                          }
          }catch(Exception ex){
              return Boolean.FALSE;
          }
      }
  
  }
  </code></pre>
  
  <blockquote>
    <p>DaoAImpl.java</p>
  </blockquote>
  
  <pre><code>@Repository("DaoA")
  public class DaoAImpl extends DaoBase {
  
      @PersistenceContext(unitName = "emA")
      private EntityManager em;
  
      @Override
      @Transactional("A")
      public Boolean findAvailability(GroupBrand marca, String code) {
          return super.findAvailability(marca, code);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>ServiceClass.java</p>
  </blockquote>
  
  <pre><code>    @Override
      public boolean isAvailable(GroupBrand brand, String code){
          Boolean available = getDMSQisAvailability(brand, code);
          return available;
      }
  
      protected Boolean isAvailability(GroupBrand marca, String kvps){
          boolean value = daoFactory.getInstance(marca).findDMSQisAvailability(marca, code);
          return value;
      }
  </code></pre>
  
  <blockquote>
    <p>ServiceClassTest.java</p>
  </blockquote>
  
  <pre><code>    @InjectMocks
          @Autowired
          @Qualifier("serviceClass")
          private ServiceClass srv;
  
      @Autowired
      @Qualifier("DaoFactory")
      protected DaoFactory daoFactory;
  
      @Mock
      protected Dao dao;
  
      @Override
      public void mockBehaivors() throws ResourceException, ResourceNotFoundException, ApiException {
          MockitoAnnotations.initMocks(this);
  
          dao = daoFactory.getInstance(marca);
  
          when(dao.findAvailability(any(GroupBrand.class), any(String.class))).thenReturn(Boolean.TRUE);
  
      }
  
      @Test
          public void isAvailableTest() throws ResourceException, Exception{
              Boolean available = srv.isAvailable(GroupBrand.A, "CODE");
          } 
  </code></pre>
  
  <p>I need to coverage ServiceClass.java class but i can't mock Dao calls from this Test class always do real call and don't return what i defined on "when", other tests works but are not using dao access.</p>
  
  <p>Anyone can help me?</p>
  
  <p>(Apologies for my poor english)..</p>
  
  <p>Regards,</p>