2016-06-22 12 views
0

I Frühling bin mit + in meinem Projekt Hibernate und unten ist mein Java-CodeMocking Frühling + Hibernate mockmvc und Mockito mit

@Repository 
    @Transactional 
    public class DomainDaoImpl implements DomainDao { 

    static Logger logger = LoggerFactory.getLogger(DomainDaoImpl.class); 

    @Autowired 
    private SessionFactory hibernateSessionFactory; 

    private static final String queryForDomains = "from Domain"; 
    @Override 
    public List<Domain> getListOfALMDomains() throws CustomException { 
    logger.info("Getting List of ALM domains"); 
    List<Domain> domains = null; 
    try { 
     Session session = null; 
     domains = this.hibernateSessionFactory.getCurrentSession().createQuery(queryForDomains).list(); 
     logger.info("Exiting getListOfALMDomains method"); 
    } catch (Exception e) { 
     logger.error("Exception occurred : " + e); 
     throw new CustomException("Please contact your administrator. Unable to retrieve the domains."); 
    } 

    return domains; 
     } 
    } 

Unten ist mein Gerät zu testen und ich versuche hibernateSessionFactory zu verspotten.

@Test 
@Transactional 
public void getListOfDomainFromDomainImpl() throws Exception{ 
String queryForDomains = "from Domain"; 
Domain domainOne = new Domain(); 
domainOne.setDomainId(4); 
domainOne.setDomainName("ADP"); 
Domain domainSecond = new Domain(); 
domainSecond.setDomainId(11); 
domainSecond.setDomainName("ABP"); 
List<Domain> domains = new ArrayList<Domain>(); 
domains.add(domainOne); 
domains.add(domainSecond); 
DomainDaoImpl domainDaoImpl = new DomainDaoImpl(); 
domainDaoImpl = mock(DomainDaoImpl.class); 
when(domainDaoImpl.getListOfALMDomains()).thenReturn(domains); 
this.hibernateSessionFactory = mock(SessionFactory.class); 
when(hibernateSessionFactory.getCurrentSession(). 
createQuery(queryForDomains) .list()).thenReturn(domains); 
} 
} 

Ich erhalte Nullpointer in Zeile

when(hibernateSessionFactory.getCurrentSession(). 
createQuery(queryForDomains).list()).thenReturn(domains); 

Ich bin nicht eine Möglichkeit bekommen hibernateSessionFactory zu verspotten. Kann mir bitte jemand zu diesem Thema helfen?

Antwort

1
  1. Sie müssen den ganzen Weg auf die Liste verspotten() -Methode:

Session session = mock(Session.class); Query query = mock(Query.class); when(hibernateSessionFactory.getCurrentSession()).thenReturn(session); when(session.createQuery(anyString())).thenReturn(query); when(query.list()).thenReturn(domains);

  1. DomainDaoImpl nicht spotten Sie, da dies das ist Klasse, die Sie testen.

domainDaoImpl.setSessionFactory(hibernateSessionFactory);

Sie haben diese Methode nicht, so dass Sie es hinzufügen müssen, und verwenden Sie @Autowired auf diese Methode anstatt auf dem Feld:

@Autowired 
public void setSessionFactory(SessionFactory sf) {this.hibernateSessionFactory = sf;} 
  1. Legen Sie Ihre Mock Sitzung Fabrik DomainDaoImpl im Test:

domainDaoImpl.setSessionFactory(hibernateSessionFactory);

+0

Vielen Dank. Das hat funktioniert – Nagendra

0

Mit diesen Zeilen Code

Session session = mock(Session.class); 
    SessionFactory sessionFactory = mock(SessionFactory.class); 
    Query query = mock(Query.class); 
when(sessionFactory.getCurrentSession()).thenReturn(session);  
when(session.createQuery(queryForDomains)).thenReturn(query); 
when(query.list()).thenReturn(domains); 
Verwandte Themen