-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot mock EntityManager
methods with @InjectMock Session
#40475
Comments
Update: I found a workaround to solve my issue. I don't think it's a recommended approach (AFAIK we shouldn't mock // src/test/resources/application.properties (can't remember if these were required or not)
quarkus.arc.test.disable-application-lifecycle-observers=true
quarkus.hibernate-orm.active=false
-----------------------
// @QuarkusTest class:
EntityManager entityManager;
@InjectSpy
XXXRepository xxxRepository;
@BeforeEach
public void setup() {
entityManager = mock(EntityManager.class);
Query mockQuery = mock(Query.class);
when(xxxRepository.getEntityManager()).thenReturn(entityManager);
when(entityManager.createNativeQuery(anyString(), any(Class.class))).thenReturn(mockQuery);
} |
Hello, Thanks for reporting. Do I understand correctly that you are calling I'm afraid this is just how Mockito works, nothing related to Quarkus of Hibernate ORM. If you think about it: Mockito will create a I would suggest:
This warning is unrelated to mocking: the method you're trying to mock is simply deprecated. |
Hi, no. Actually, I'm trying to call/mock I'll give a try to In the docs:
From this section I understood that if I want to mock |
If your code uses Otherwise, since |
Replacing Update, it didn't work. As you just mention, it's probably injecting a Session object (which fails when colides with However, I'm not sure if it's a recommended approach, or if it should also be in someway in the docs. org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Query$MockitoMock$Ng11Y08m cannot be returned by createNativeQuery()
createNativeQuery() should return NativeQuery import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectSpy;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
@QuarkusTest
class XXXRepositoryTest {
@InjectMock
EntityManager entityManager;
@InjectSpy
XXXRepository xxxRepository;
@BeforeEach
public void setup() {
// entityManager = mock(EntityManager.class);
Query mockQuery = mock(Query.class);
when(xxxRepository.getEntityManager()).thenReturn(entityManager); // fails with or without this line
when(entityManager.createNativeQuery(anyString(), any(Class.class))).thenReturn(mockQuery);
}
@Test
void testPanacheMocking() {
xxxRepository.methodX("", ""); //calls getEntityManager().createNativeQuery(String, XXX.class) inside
assertEquals(1, 1);
}
} |
Can you please share the stacktrace? As text, preferably, not a screenshot :) From what I can see, Panache retrieves an
It's not though, you can just mock the Session. As you said: "Passes with |
Sure, no problem. I'm a good person (but don't give me ideas😈 )
PS: Doesn't work with
|
Thank you for the stracktrace, this should help with future investigations.
Alright, so let's agree there are two solutions, and you prefer the one that doesn't involve a |
Haha, yes. It's one way of saying it. |
So to sum up, I opened #40807 to address |
Describe the bug
It's not possible to mock all
EntityManager
methods with@InjectMock Session
(e.g.createNativeQuery
)Related: #16437 (comment)
Expected behavior
As stated in https://quarkus.io/version/3.8/guides/hibernate-orm-panache#mocking-entitymanager-session-and-entity-instance-methods, mocking EntityManager should be possible..
Warnings/errors shouldn't appear
Actual behavior
jakarta.persistence.Query jakarta.persistence.EntityManager.createNativeQuery(String sqlString)
method conflicting withorg.hibernate.query.NativeQuery org.hibernate.query.QueryProducer.createNativeQuery(String sqlString)
when mockingEntityManager
viaSession
Code from docs (https://quarkus.io/version/3.8/guides/hibernate-orm-panache#mocking-entitymanager-session-and-entity-instance-methods) shows some warnings for
createQuery
andQuery
, too.Even when doing some manual casting with my code, I'm getting the following error
How to Reproduce?
Session
extendsSharedSessionContract
,EntityManager
,SharedSessionContract
extendsQueryProducer
EntityManager
declaresjakarta.persistence.Query createQuery(String queryString)
,jakarta.persistence.Query createNativeQuery(String sqlString)
QueryProducer
declaresorg.hibernate.query.Query createQuery(String queryString)
,org.hibernate.query.NativeQuery createNativeQuery(String sqlString)
@InjectMock Session
to mockEntityManager
used inPanacheRepository
(getEntityManager()
),QueryProducer
methods are expected instead ofEntityManager
'sOutput of
uname -a
orver
No response
Output of
java -version
openjdk version "17.0.11"
Quarkus version or git rev
3.8.4
Build tool (ie. output of
mvnw --version
orgradlew --version
)Apache Maven 3.9.6
Additional information
Fails when importing
org.hibernate.query.Query
orjakarta.persistence.Query
. Passes withorg.hibernate.query.NativeQuery
.The text was updated successfully, but these errors were encountered: