package de.tudarmstadt.ukp.wikipedia.api; import static org.junit.Assert.*; import java.util.HashSet; import java.util.Set; import org.hibernate.Transaction; import org.hibernate.LockOptions; import org.hibernate.Session; import org.hibernate.Session.LockRequest; import org.hibernate.SessionFactory; import org.junit.Assume; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import de.tudarmstadt.ukp.wikipedia.api.WikiConstants.Language; import de.tudarmstadt.ukp.wikipedia.api.exception.WikiApiException; import de.tudarmstadt.ukp.wikipedia.api.hibernate.WikiHibernateUtil; @RunWith(PowerMockRunner.class) @PrepareForTest(WikiHibernateUtil.class) public class TestPage { @Before public void setup() { } @Test public void fittestGetCategoriesTest() { SessionFactory sessionFactory = Mockito.mock(SessionFactory.class); DatabaseConfiguration dbConfig = Mockito.mock(DatabaseConfiguration.class); PowerMockito.mockStatic(WikiHibernateUtil.class); PowerMockito.when(WikiHibernateUtil.getSessionFactory(dbConfig)).thenReturn(sessionFactory); Wikipedia wiki = Mockito.mock(Wikipedia.class); Session session = Mockito.mock(Session.class); LockRequest lockReq = Mockito.mock(LockRequest.class); //PowerMockito.mockStatic(WikiHibernateUtil.class); de.tudarmstadt.ukp.wikipedia.api.hibernate.Page mockPage = Mockito.mock(de.tudarmstadt.ukp.wikipedia.api.hibernate.Page.class); Transaction transaction = Mockito.mock(Transaction.class); Set categories = new HashSet(); categories.add(1); Category cat1 = Mockito.mock(Category.class); Mockito.when(dbConfig.getLanguage()).thenReturn(Language._test); Mockito.when(dbConfig.getHost()).thenReturn("host"); Mockito.when(dbConfig.getUser()).thenReturn("user"); Mockito.when(dbConfig.getPassword()).thenReturn("pass"); Mockito.when(dbConfig.getDatabase()).thenReturn("db"); Mockito.when(mockPage.getPageId()).thenReturn(1); Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session); Mockito.when(session.get(Mockito.anyString(), Mockito.eq(1L))).thenReturn(mockPage); Mockito.when(session.getTransaction()).thenReturn(transaction); Mockito.when(mockPage.getCategories()).thenReturn(categories); Mockito.when(wiki.__getHibernateSession()).thenReturn(session); Mockito.when(session.buildLockRequest(LockOptions.NONE)).thenReturn(lockReq); Mockito.when(wiki.getDatabaseConfiguration()).thenReturn(dbConfig); Mockito.when(wiki.getCategory(1)).thenReturn(cat1); Page p; try { p = new Page(wiki, 1); assertEquals(1, p.getCategories().size()); } catch (WikiApiException e) { Assume.assumeNoException(e); } } }