JUNIT Test How To Mock Entity Manager Factory. i'm writing junit test case for Entity Manager Factory but here in side when (when().then()) getting null.
Asked
Active
Viewed 2,492 times
1 Answers
0
Add @ExtendWith(MockitoExtension.class)
at the top of you Test class.
This code is working for me. You should remove @ExtendWith (SpringExtension.class). Also remove that Mockito.mock since we have added @Mock on top of those fields.
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TestClass {
@Mock
private EntityManager entityManager;
@Mock
private EntityManagerFactory entityManagerFactory;
@Mock
private CriteriaBuilder criteriaBuilder;
@Mock
private CriteriaQuery<User> criteriaQuery;
@Test
void getSearchData() {
when(entityManagerFactory.createEntityManager()).thenReturn(entityManager);
when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
when(criteriaBuilder.createQuery(User.class)).thenReturn(criteriaQuery);
when(criteriaQuery.from(User.class)).thenReturn(new Root<>());//you can also mock Root object
}
}

Sagar Rawat
- 76
- 4
-
With this annotation also getting same null value from EntityManagerFactory...Please provide me another solution so i can try that. – Krish Gaur Mar 10 '22 at 07:08
-
Can you share the code snippet that you are trying? – Sagar Rawat Mar 10 '22 at 07:10
-
Autowired private EntityManagerFactory postgresEntityManagerFactory; EntityManager em = postgresEntityManagerFactory.createEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery
cq = cb.createQuery(User.class); Root – Krish Gaur Mar 10 '22 at 07:17cdRet = cq.from(User.class); List predicates = new ArrayList<>(); -
I'm Autowired EntityManagerFactory postgresEntityManagerFactory and trying to mock like above code... – Krish Gaur Mar 10 '22 at 07:30
-
Mock private EntityManager entityManager; Atowired Test void getSearchData() { entityManager = Mockito.mock(EntityManager.class); EntityManagerFactory entityManagerFactory = Mockito.mock(EntityManagerFactory.class); when(entityManagerFactory.createEntityManager()).thenReturn(entityManager); – Krish Gaur Mar 10 '22 at 07:31
-
I tried Above code in junit test case – Krish Gaur Mar 10 '22 at 07:32
-
Yes i'm Using @Mock only on EntityManagerFactory – Krish Gaur Mar 10 '22 at 07:34
-
Will you be able to post your full test file in you question with formatting. It will be easier to understand there. :) – Sagar Rawat Mar 10 '22 at 07:36
-
ExtendWith(SpringExtension.class) ExtendWith(MockitoExtension.class) class User { Mock private EntityManager entityManager; Test void getSearchData(){entityManager= Mockito.mock(EntityManager.class); EntityManagerFactory entityManagerFactory=Mockito.mock(EntityManagerFactory.class); when(entityManagerFactory.createEntityManager()).thenReturn(entityManager);}} \\I removed sine from annotations because stackoverflow is not allowing so please – Krish Gaur Mar 10 '22 at 08:36
-
Try code in the updated answer. – Sagar Rawat Mar 10 '22 at 10:01
-
Whatever code u sent it's Very useful for me Thank you so much for this but As i mention in my previous comment [Autowired private EntityManagerFactory postgresEntityManagerFactory; EntityManager em = postgresEntityManagerFactory.createEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery
cq = cb.createQuery(User.class); Root – Krish Gaur Mar 10 '22 at 11:20cdRet = cq.from(User.class);] After em value : em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(User.class); error will throw because CriteriaQuery is not mocked thats -
Please see once my above comment Because whatever code you sent i agree it's mocking EntityManagerFactory but whenever we want some value from EntityManagerFactory it will we null because we are only mocking not giving any value ...so please I will reach my ending solution – Krish Gaur Mar 10 '22 at 11:44
-
you just need to mock other objects as well. Do when().then() for others as well. – Sagar Rawat Mar 10 '22 at 14:07