Skip to content
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 anymore #16437

Closed
FroMage opened this issue Apr 12, 2021 · 43 comments · Fixed by #16689
Closed

Cannot mock EntityManager anymore #16437

FroMage opened this issue Apr 12, 2021 · 43 comments · Fixed by #16689
Labels
area/hibernate-orm Hibernate ORM area/testing kind/bug Something isn't working
Milestone

Comments

@FroMage
Copy link
Member

FroMage commented Apr 12, 2021

#15092, and more specifically https://github.com/quarkusio/quarkus/pull/15092/files#diff-dc1ed2e710fe857008635f1b3280c2adb1fc6b90b0925bd508e24d89f696050fR39 prevents me from mocking EntityManager:

        EntityManager entityManager = Mockito.mock(EntityManager.class);
        QuarkusMock.installMockForType(entityManager, EntityManager.class);

This leads to:

java.lang.RuntimeException: Mock for EntityManager, hashCode: 294008652 is not assignable to type class io.quarkus.hibernate.orm.runtime.session.ForwardingSession
	at io.quarkus.test.junit.MockSupport.installMock(MockSupport.java:41)
	at io.quarkus.test.junit.QuarkusMock.installMockForType(QuarkusMock.java:51)
	at io.quarkus.it.panache.PanacheMockingTest.testPanacheMocking(PanacheMockingTest.java:29)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at io.quarkus.test.junit.QuarkusTestExtension.runExtensionMethod(QuarkusTestExtension.java:939)
	at io.quarkus.test.junit.QuarkusTestExtension.interceptTestMethod(QuarkusTestExtension.java:845)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at java.util.ArrayList.forEach(ArrayList.java:1259)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at java.util.ArrayList.forEach(ArrayList.java:1259)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:84)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
@famod
Copy link
Member

famod commented Apr 13, 2021

Might be fixed already by #16341?

@FroMage
Copy link
Member Author

FroMage commented Apr 13, 2021

Ah yes indeed. We still have to add a test for it, though.

@FroMage
Copy link
Member Author

FroMage commented Apr 14, 2021

No, this is what I get now:

java.lang.RuntimeException: io.quarkus.hibernate.orm.runtime.HibernateOrmRecorder$5$1@70e4fefc is not a normal scoped CDI bean, make sure the bean is a normal scope like @ApplicationScoped or @RequestScoped
	at io.quarkus.test.junit.MockSupport.installMock(MockSupport.java:48)
	at io.quarkus.test.junit.QuarkusMock.installMockForType(QuarkusMock.java:57)
	at io.quarkus.it.panache.PanacheMockingTest.setup(PanacheMockingTest.java:33)

But the underlying issue is:

java.lang.NoSuchMethodException: io.quarkus.hibernate.orm.runtime.HibernateOrmRecorder$5$1.arc$setMock(java.lang.Object)

So there's a combination of failure from ORM and mocking setup.

@FroMage
Copy link
Member Author

FroMage commented Apr 14, 2021

This is way above anything I can comprehend.

I've pushed a reproducer at https://github.com/FroMage/quarkus/tree/16437

I don't know if this is more of an issue for @yrodiere @Sanne @geoand or @mkouba but I think it is probably important, not just for Panache, since I don't think we can mock EntityManager for ORM anymore. Unless I'm missing a mocking strategy that is better than this?

@FroMage
Copy link
Member Author

FroMage commented Apr 14, 2021

Repro is PanacheMockingTest class in integration-tests/hibernate-orm-panache

@yrodiere
Copy link
Member

yrodiere commented Apr 14, 2021

@FroMage The transaction-scoped EntityManagers are now exposed as Sessions as well, so that people can use @Inject Session session. It's been that way for a few months, though?

In any case... maybe try this?

        Session entityManager = Mockito.mock(Session.class);
        QuarkusMock.installMockForType(entityManager, Session.class);

Not saying that's a long-term solution, but if it works that's at least a workaround.

@yrodiere
Copy link
Member

Ok I should have read the rest of the thread... So no, I have no idea what's going on :]

@Sanne
Copy link
Member

Sanne commented Apr 14, 2021

... prevents me from mocking EntityManager

it's a feature! :)

@geoand
Copy link
Contributor

geoand commented Apr 14, 2021

I'll have a look tomorrow

@FroMage
Copy link
Member Author

FroMage commented Apr 14, 2021

Thanks!

@famod
Copy link
Member

famod commented Apr 14, 2021

FWIW, you can also do something like this with Mockito:

Mockito.mock(ForwardingSession.class, withSettings().extraInterfaces(EntityManager.class));

geoand pushed a commit to geoand/quarkus that referenced this issue Apr 14, 2021
@geoand
Copy link
Contributor

geoand commented Apr 14, 2021

@FroMage I pushed a couple of commits to: https://github.com/geoand/quarkus/tree/16437

It does make mocking work, but the test still doesn't pass because you (hopefully) need to mock the required EntityManager methods in the test like you did for persist.

@FroMage
Copy link
Member Author

FroMage commented Apr 15, 2021

Thanks!

@FroMage
Copy link
Member Author

FroMage commented Apr 15, 2021

Well I'm actually getting this exception now, which makes no sense, unless Mockito isn't able to implement that method for some reason:

java.lang.AbstractMethodError: Receiver class org.hibernate.Session_5b93bee577ae2f8d76647de04cfab36afbf52958_Synthetic_ClientProxy does not define or inherit an implementation of the resolved method abstract createQuery(Ljava/lang/String;)Ljavax/persistence/Query; of interface javax.persistence.EntityManager.
	at io.quarkus.hibernate.orm.panache.common.runtime.AbstractJpaOperations.count(AbstractJpaOperations.java:306)
	at io.quarkus.it.panache.Person.count(Person.java)
	at io.quarkus.it.panache.PanacheMockingTest.testPanacheMocking(PanacheMockingTest.java:87)

@geoand
Copy link
Contributor

geoand commented Apr 15, 2021

Did you try to mock that method?

@FroMage
Copy link
Member Author

FroMage commented Apr 15, 2021

Oh hold on. So yeah, I did mock it (and it's not getting called), but also, this isn't the mock that fails to implement it: it's the ClientProxy… Wow that's weird…

Huh, it's a covariant method org.hibernate.query.Query createQuery(String queryString);, where org.hibernate.query.Query extends the javax.persistence.Query interface returned by the supertype method, so perhaps something weird wrt covariant methods in ArC @mkouba ?

@mkouba
Copy link
Contributor

mkouba commented Apr 15, 2021

Oh hold on. So yeah, I did mock it (and it's not getting called), but also, this isn't the mock that fails to implement it: it's the ClientProxy… Wow that's weird…

Huh, it's a covariant method org.hibernate.query.Query createQuery(String queryString);, where org.hibernate.query.Query extends the javax.persistence.Query interface returned by the supertype method, so perhaps something weird wrt covariant methods in ArC @mkouba ?

How do I reproduce the issue in few steps?

@mkouba
Copy link
Contributor

mkouba commented Apr 19, 2021

Ok, so I was able to reproduce the problem. Yes, the client proxy class declares org.hibernate.query.Query createQuery(java.lang.String) from SharedSessionContract which overrides org.hibernate.query.QueryProducer.createQuery(String) from the QueryProducer. But apparently we need to add the generated bridge methods with javax.persistence types as well..

@FroMage
Copy link
Member Author

FroMage commented Apr 19, 2021

Sorry about the delay. OK so it's an ArC bug then?

@mkouba
Copy link
Contributor

mkouba commented Apr 19, 2021

I'm not sure TBH because I would expect the hibernate Session interface to have a bridge method for EntityManager.createQuery(String) defined, similarly as it has for EntityManager.createQuery(javax.persistence.criteria.CriteriaDelete), but the bridge method is not there...

@mkouba
Copy link
Contributor

mkouba commented Apr 19, 2021

Hm, it seems that all other create() methods are explicitly overriden: https://github.com/hibernate/hibernate-orm/blob/main/hibernate-core/src/main/java/org/hibernate/Session.java#L1141-L1154

@Sanne do you happen to know why org.hibernate.query.QueryProducer.createQuery(String) is not?

@Sanne
Copy link
Member

Sanne commented Apr 19, 2021

no sorry I don't know if there's a specific reason.

@Sanne
Copy link
Member

Sanne commented Apr 19, 2021

BTW it's defined in org.hibernate.SharedSessionContract, the parent interface of Session. The SharedSessionContract is shared across other flavours of Session.

@mkouba
Copy link
Contributor

mkouba commented Apr 21, 2021

Ok, so I was finally able to create a reproducer.

To sum it up:

  1. Session extends SharedSessionContract, EntityManager,
  2. SharedSessionContract declares org.hibernate.query.Query createQuery(String queryString) which overrides the org.hibernate.Query createQuery(String queryString) from the QueryProducer,
  3. EntityManager declares javax.persistence.Query createQuery(String qlString),
  4. Session class bytecode does not contain any bridge method for the createQuery(String str),

This PR: #16671 should fix this problem.

@FroMage
Copy link
Member Author

FroMage commented Apr 21, 2021

Thanks a lot. This allows me to move further.

Now I run into this weird thing:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type Session_5b93bee577ae2f8d76647de04cfab36afbf52958_Synthetic_ClientProxy and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
    verify(mock).someMethod();
    verify(mock, times(10)).someMethod();
    verify(mock, atLeastOnce()).someMethod();
	at io.quarkus.it.panache.PanacheMockingTest.testPanacheMocking(PanacheMockingTest.java:118)

When I'm doing this:

@QuarkusTest
public class PanacheMockingTest {

    @Inject
    Session session;

    @BeforeAll
    public static void setup() {
        Session 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() {
...
// THIS THROWS
        Mockito.verify(session).persist(Mockito.any());
   }
}

In other injection points, I do get the proper mock Session since the test passes. But apparently, the version injected in the test is not the mock.

I'm pretty sure https://quarkus.io/guides/getting-started-testing#quarkus_mock says this should work, no @geoand ?

@geoand
Copy link
Contributor

geoand commented Apr 21, 2021

@FroMage you are verifying the session when you should verifying the mock :)

Something like:

@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() {
...

        Mockito.verify(mock).persist(Mockito.any());
   }
}

@FroMage
Copy link
Member Author

FroMage commented Apr 21, 2021

But… the docs I pointed to indicate that even in the test, the @Inject instance should result in the mock instance, no? I don't see how it can mean anything else…

@mkouba
Copy link
Contributor

mkouba commented Apr 21, 2021

But… the docs I pointed to indicate that even in the test, the @Inject instance should result in the mock instance, no? I don't see how it can mean anything else…

I don't think you can use Mockito.verify() together with an injected bean; and the docs do not claim it is possible - simple asserts are used there.

@FroMage
Copy link
Member Author

FroMage commented Apr 21, 2021

OOOOOOOHHH OK. So… OK. So what happens is that @Inject doesn't get us the mock instance, but a CDI proxy to the mock instance, right?

And even though you can call the methods of both the mock instance and the proxy to the mock, in order to get mock responses, you can actually only call verify on the mock instance.

OK. That's a subtlety that I'll add to the guide, because that's quite a fine one IMO.

@FroMage
Copy link
Member Author

FroMage commented Apr 21, 2021

But thanks for your explanation!

@geoand
Copy link
Contributor

geoand commented Apr 21, 2021

@FroMage exactly

@Sanne
Copy link
Member

Sanne commented Apr 21, 2021

Interesting, but this is rather tricky for end users. Couldn't the CDI proxy be made to work with verify as well? That would surely be more user friendly.

@geoand
Copy link
Contributor

geoand commented Apr 21, 2021

I personally don't think it's tricky - it's just how mockito works.
If you want to able to do that sort of thing, you would likely use Quarkus' @InjectMock

@famod
Copy link
Member

famod commented Apr 21, 2021

Or resort to io.quarkus.arc.ClientProxy.arc_contextualInstance() or io.quarkus.arc.runtime.ClientProxyUnwrapper?

Got that from here: https://quarkusio.zulipchat.com/#narrow/stream/187038-dev/topic/Unproxy.20.40RequestScoped.20bean/near/219501112

@FroMage
Copy link
Member Author

FroMage commented Apr 21, 2021

If you want to able to do that sort of thing, you would likely use Quarkus' @InjectMock

Huh, so perhaps I used the wrong approach in my test?

@geoand
Copy link
Contributor

geoand commented Apr 21, 2021

What is you exact goal?

@Sanne
Copy link
Member

Sanne commented Apr 21, 2021

I personally don't think it's tricky - it's just how mockito works.

Sure but isn't that always the answer :) "There's a memory leak X" --> "That's just how this works" :-P

What I'm suggesting is that it's not obvious to users that when they inject an object they get a proxy. And sure they're mocking here so a proxy is expected, but I guess that having a proxy of a proxy isn't the general expectation.

But I'm entirely out of my field of comfort, so no idea if it's easy - just saying if we could have ArC generate the proxy while maintaining its mock superpowers that would be more useful IMO. No idea if there's drawbacks.

@geoand
Copy link
Contributor

geoand commented Apr 21, 2021

Sure, making users lives easier is what we strive to do.
That is what @InjectMock does

@FroMage
Copy link
Member Author

FroMage commented Apr 23, 2021

@geoand you're so right, sorry about this. I'll update my test and my docs to use it, that is indeed much simpler.

@geoand
Copy link
Contributor

geoand commented Apr 23, 2021

Glad to hear it!

FroMage added a commit to FroMage/quarkus that referenced this issue Apr 23, 2021
@FroMage
Copy link
Member Author

FroMage commented Apr 23, 2021

Done.

@quarkus-bot quarkus-bot bot added this to the 2.0 - main milestone Apr 27, 2021
@gian1200
Copy link
Contributor

gian1200 commented May 4, 2024

Ok, so I was finally able to create a reproducer.

To sum it up:

  1. Session extends SharedSessionContract, EntityManager,
  2. SharedSessionContract declares org.hibernate.query.Query createQuery(String queryString) which overrides the org.hibernate.Query createQuery(String queryString) from the QueryProducer,
  3. EntityManager declares javax.persistence.Query createQuery(String qlString),
  4. Session class bytecode does not contain any bridge method for the createQuery(String str),

This PR: #16671 should fix this problem.

Sorry for the archeology work, Is this fix specific to this scenario, or was there any regression or change since then?

I'm facing the same issue with jakarta.persistence.Query jakarta.persistence.EntityManager.createNativeQuery(String sqlString) method conflicting with org.hibernate.query.NativeQuery org.hibernate.query.QueryProducer.createNativeQuery(String sqlString) when mocking EntityManager via Session (Quarkus 3.8.4).

image

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 and Query, too.
image

Even when doing some manual casting with my code, I'm getting the following error

image

ERROR:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
Query$MockitoMock$JX88Svnf cannot be returned by createNativeQuery()
createNativeQuery() should return NativeQuery
  • Code Fragment:
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.hibernate.Session;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;

@QuarkusTest
class InjectionRepositoryTest {

	@InjectMock
	Session session;

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

	@Test
	void testPanacheMocking() {
		assertEquals(1, 1);
	}

}

Fails when importing org.hibernate.query.Query or jakarta.persistence.Query. Passes with org.hibernate.query.NativeQuery.

Is this expected (the warnings and the usage of different types)? Should I create a new issue?

@yrodiere
Copy link
Member

yrodiere commented May 6, 2024

Should I create a new issue?

Yes please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-orm Hibernate ORM area/testing kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants