diff --git a/integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/PanacheMockingTest.java b/integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/PanacheMockingTest.java index f645d5d77d16a..6f73755184364 100644 --- a/integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/PanacheMockingTest.java +++ b/integration-tests/hibernate-orm-panache/src/test/java/io/quarkus/it/panache/PanacheMockingTest.java @@ -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() { @@ -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 { @@ -95,6 +117,7 @@ public void testPanacheMocking() { PanacheMock.verify(Person.class).persist(Mockito. any(), Mockito. 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')); }