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 21, 2021
1 parent ef07949 commit 4ff57f1
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,38 @@
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.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;
import io.quarkus.panache.mock.PanacheMock;
import io.quarkus.test.junit.QuarkusMock;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;

@QuarkusTest
public class PanacheMockingTest {

@Inject
Session session;

static Session mock;

@BeforeAll
public static void setup() {
mock = Mockito.mock(Session.class);
Query mockQuery = Mockito.mock(Query.class);
Mockito.doNothing().when(mock).persist(Mockito.any());
Mockito.when(mock.createQuery(Mockito.anyString())).thenReturn(mockQuery);
Mockito.when(mockQuery.getSingleResult()).thenReturn(0l);
QuarkusMock.installMockForType(mock, Session.class);
}

@Test
@Order(1)
public void testPanacheMocking() {
Expand Down Expand Up @@ -79,6 +98,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 +117,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(mock, 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 4ff57f1

Please sign in to comment.