-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
New panache-mock module for mocking Panache static methods #8334
Conversation
So the injected implementation looks like this at the start of every static public method: if(PanacheMock.IsMockEnabled && PanacheMock.isMocked(Person.class)) {
try {
return (int)PanacheMock.mockMethod(Person.class, "count", new Class<?>[] {}, new Object[] {});
} catch (PanacheMock.InvokeRealMethodException e) {
// fall-through
}
} I still have to make this conditional to the TEST mode. Now the question is how do I tie this with the Users probably don't want to write tests like this: @AfterEach
public void afterEach() {
// make sure one test's mocking doesn't leak to other tests
PanacheMock.reset();
}
@Test
public void testPanacheMocking() {
// specify all mocked classes
PanacheMock.mock(Person.class, Dog.class);
} So perhaps I should make Is there value in this alternative form to declare mocked classes? @Test
@PanacheMockClasses({Person.class, Dog.class})
public void testPanacheMocking() {
} Doesn't look very justified to me. |
This looks promising !
You need to turn PanacheMock in a Mockito extension or a JUnit extension. For Mockito extension point, I read quickly the documentation and you can provide you own For JUnit extension, the better is to implements If you don't need to integrate too deep into Mockito the JUnit extension is the easiest. Some early feedback on your PR:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there's some code missing.
Also, big question: should this really be an extension or should it be part of our test framework modules? I would favor the latter if there's no good reason to make it an extension.
Yeah, I was missing the extension, sorry. Updated. |
So ATM, the extension depends on This works because We could place the customizer interface and build item somewhere else, because they don't really depend on anything Panache, but where? |
extensions/panache/panache-mock/runtime/src/main/java/io/quarkus/panache/mock/PanacheMock.java
Outdated
Show resolved
Hide resolved
I could add that, yes.
Well we have to inject some code in the methods at build time so they can be mocked. We should only do it in the TEST mode, though, so I think it's OK. |
Just read the PR again with the panache-mock extension and it is clearer now ;)
I agree, in fact I found now the approach OK, we can reuse it if needed (for example to fix some Kotlin issue ?, or to provides specialization for some databases ?) so it's great.
I think that being a panache-mock extension is OK, Panache users will tipically add it with the test scope if they need it (not everybody uses panache entity). Of course, if it could be only a JUnit or a Mockito extension, this would be better as everything will be a test runtime and scoped to a single test instead of at build time and scopped at the entire test suite. But I'm OK with this anyway. |
As I said, there has to be something at build-time to inject a hook into the static methods so they can delegate to mocking. I don't know how to do otherwise. |
|
So now the only thing left IMO is where this extension sits. ATM it's an extension That build item is coupled with an interface I could move their implementations to the Perhaps @geoand or @stuartwdouglas have ideas? |
Is having a specific panache testing module not a good option? |
No idea. @gsmet asked the question, and it's a good question. It does make sense that OTOH it's not that annoying to have a dedicated extension IMO. So I'm really open to opinions here. |
I think that the panache testing stuff should be a dedicated module (that perhaps includes I don't think it will be a problem for users to add a test dependency. We can also make it clear in the release notes and the docs. Plus we can augment our project generation to add that dependency when the Panache extension is added. |
I'm OK with A few questions:
|
Yeah, I should definitely remove it.
Well Mockito can mock repos already so there's no need for that. It will not work if you pass a repo. I could add a type constraint for
I can't because it would add a dependency to orm/mongo-panache. Unless we create a common interface, which we didn't want to do. Not sure how to proceed here.
It should work, I can try it. |
Just add a sentence inside the documentation this should be OK for now. Regarding mocking of PanacheQuery, we can add it later if needed. As we don't have a supertype for both orm/mongo it will need to be created in specific extension ... not sure we want that now. |
Just got this:
So I'm guessing my mocking is lacking something :) |
OK that's fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concerns have been addressed
I believe this is something we should include in 1.4. @gsmet WDYT? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
So that leaves @gsmet |
The tests fail for vertx-web, but I don't see how that can be related to this PR. @cescoffier are you aware of current/intermitent test failures in vertx-web? |
Actually, vert.x web is quite stable.... Haivng a look. |
Looks like a race condition or something clearly wrong unrelated to this PR:
|
Ah ah, I believe it's the race condition addressed by @mkouba ! |
Already fixed in master you mean? |
AFAIK it's not merged yet |
@FroMage @cescoffier yes, this should be addressed by #8579. Unfortunately, I can't "force merge" anymore. |
Rebased on master to see if the test failures are gone, they're definitely not related to this PR. |
I declare CI to be dead. |
Please rebase PR onto the latest master in order to pick up a CI fix |
Now it's all automated, no more forgetting methods to bridge
@gsmet just merged it. Not sure if it's too late to make 1.4 or wait for 1.5? |
If it changed only the test code, I would have said 1.4 but I can see it changes quite a lot of things in the regular code too so I'm leaning towards 1.5. |
Yes, and 1.5 is not far off and let's be honest, nobody actually cares about mocking, as we've uncovered in this PR that nobody is mocking panache repositories ;) |
For the record, I mock panache repositories without any issue since the beginning ! |
That can't be true: this PR uncovered so many bugs in the generated code for repositories that prevented them from being mocked! Or you've never mocked any method with generics or primitives in their signature ;) |
When I tried mocking repos, Mockito just threw exceptions when it loaded the generated classes with bytebuddy. |
I mocked all my repos on raw JUnit tests with Mockito without any issue, even the findById methods. I think Daniel Petisme also (he report it during it's effort on JHipster). |
Tied to
Mockito
, usage: