Skip to content

Commit

Permalink
Added reproducer for quarkusio#16437 mocking EntityManager
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Apr 23, 2021
1 parent 65b8819 commit 648fd40
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import javax.persistence.LockModeType;
import javax.ws.rs.WebApplicationException;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -20,6 +23,17 @@
@QuarkusTest
public class PanacheMockingTest {

@InjectMock
Session session;

@BeforeEach
public void setup() {
Query mockQuery = Mockito.mock(Query.class);
Mockito.doNothing().when(session).persist(Mockito.any());
Mockito.when(session.createQuery(Mockito.anyString())).thenReturn(mockQuery);
Mockito.when(mockQuery.getSingleResult()).thenReturn(0l);
}

@Test
@Order(1)
public void testPanacheMocking() {
Expand Down Expand Up @@ -79,6 +93,9 @@ public void testPanacheMocking() {

Person.persist(p);
Assertions.assertNull(p.id);
// mocked via EntityManager mocking
p.persist();
Assertions.assertNull(p.id);

Mockito.when(Person.findById(12l)).thenThrow(new WebApplicationException());
try {
Expand All @@ -95,6 +112,7 @@ public void testPanacheMocking() {
PanacheMock.verify(Person.class).persist(Mockito.<Object> any(), Mockito.<Object> any());
PanacheMock.verify(Person.class, Mockito.atLeastOnce()).findById(Mockito.any());
PanacheMock.verifyNoMoreInteractions(Person.class);
Mockito.verify(session, Mockito.times(1)).persist(Mockito.any());

Assertions.assertEquals(0, Person.methodWithPrimitiveParams(true, (byte) 0, (short) 0, 0, 2, 2.0f, 2.0, 'c'));
}
Expand Down

0 comments on commit 648fd40

Please sign in to comment.