-
Notifications
You must be signed in to change notification settings - Fork 565
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
4.x: Ability to Inject MockBeans in Helidon #7694 #8674
Conversation
Setting it as a draft for now. I have a couple of CDI questions: Question 1: Currently
Is there any way to not require Question 2: It does not work if the bean already exists in CDI. For example, this will not work because the service will be defined twice (you need to remove
I didn't find how to replace the existing bean defined by the mocked one. |
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
I'm not a CDI expert, but can you set the mocked bean as an alternative ? That's how the current solution (#8339) works. |
I think that requires a producer, and I cannot use a producer because you need to know the type in advance. I will try it further. Do you know who is expert on this?, I bet for @tomas-langer |
Question 1 is answered in this commit: 57ac20b |
319343b
to
b7314f4
Compare
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
Question 2 solved here: e0cd11b |
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
677558f
to
1087fb3
Compare
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
1153bc3
to
48461cd
Compare
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
For TestNG it seems we cannot use the For example, this simple test: @HelidonTest
class TestMockBeanField {
private final WebTarget target;
@Inject
public TestMockBeanField(WebTarget target) {
this.target = target;
}
@Test
void test() {
}
} It fails with:
|
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
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 nice so far.
We need to update the docs as well.
...ing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBeanField.java
Show resolved
Hide resolved
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.
This looks like a great feature, but the hard dependency on mockito
is something we probably do not desire.
It seems to me that the whole feature is implemented as a CDI extension. Can we maybe create a new module (depending on mockito), that would add the CDI extension and the annotation, and that could be used with both JUnit5 and testNG extension, because it is "just" a CDI portable extension?
i.e. helidon-microprofile-testing-mock-beans
?
...sting/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java
Outdated
Show resolved
Hide resolved
...sting/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
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.
A few small things.
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
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 good now, let's wait for library approval.
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
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.
Dependencies look good to me. Thanks for all the work on this!
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 good, please update the PR description to fill-in the documentation section:
- describe the feature (what it solves)
- describe the API (annotation + new GAV)
- add sample snippet
Let's also file an issue to create the actual docs for this feature.
Do we re-use this issue?: #8339 I didn't work in any documentation issue before, is there any guide? |
Let's file a separate issue as #8339 has more to it than strictly document this new feature.
No, however adding one more document is relatively easy as there is a lot of precedence. The bullet items I provided was guidance not a template. It should read like a short documentation of the feature not like a form ! |
This is the new issue: Are there more changes required in this PR or can it be approved/merged. I guess the documentation changes will be submitted in the new issue I created. |
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, nice work.
I've updated the description.
Description
#7694
Documentation
This feature provides a simple mechanism for replacing CDI bean with mock instances created with Mockito.
It is similar to Spring's
@MockBean
, in fact it uses the same name.The introduced API consist of a new Maven module
io.helidon.microprofile.testing:helidon-microprofile-testing-mocking
that provides one annotation@MockBean
.The annotation is designed to be used on fields of test classes annotated with
@HelidonTest
, the implementation relies only on CDI thus it works with either JUnit or TestNG.The annotation has a parameter "answer" that can be used to set the default answer for the mocked beans.
See a snippet below:
The mocked beans of a test class can be customized with a CDI producer method.
E.g.