Skip to content

Commit

Permalink
WIP: reproducer for #16437 mocking EntityManager
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Apr 14, 2021
1 parent 2110988 commit 812d603
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@
import java.util.Optional;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.ws.rs.WebApplicationException;

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
EntityManager em;

@BeforeAll
public static void setup() {
EntityManager mock = Mockito.mock(EntityManager.class);
Mockito.doNothing().when(mock).persist(Mockito.any());
QuarkusMock.installMockForType(mock, EntityManager.class);
}

@Test
@Order(1)
public void testPanacheMocking() {
Expand Down Expand Up @@ -79,6 +92,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 +111,8 @@ 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(em).persist(Mockito.any());
Mockito.verifyNoInteractions(em);

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

0 comments on commit 812d603

Please sign in to comment.