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

Add CDI-Unit-Rule to use as alternative to CDI-Runner #74

Closed
joergsesterhenn opened this issue Jul 1, 2015 · 5 comments · Fixed by #277
Closed

Add CDI-Unit-Rule to use as alternative to CDI-Runner #74

joergsesterhenn opened this issue Jul 1, 2015 · 5 comments · Fixed by #277
Assignees
Labels
area/junit kind/enhancement New feature or request
Milestone

Comments

@joergsesterhenn
Copy link

Please provide a JUnit Rule for CDI-Unit, so that I do not have to use a custom Runner.

This would also allow using different Runners in combination with CDI-Unit.

@BrynCooke
Copy link
Collaborator

Please provide a pull request and I'll merge and release.

@seanf
Copy link
Collaborator

seanf commented Nov 10, 2015

I've had a look at doing this. As it stands, I don't think it's possible, in general, from a JUnit Rule, because CDI-Unit creates a custom classloader to invoke the test class, but Rules delegate to other Rules by wrapping each other's Statements (in the same classloader).

To make it work as a Rule, you would have to (a) get rid of the classloading (which would probably reduce CDI-Unit's abilities greatly, if it's even possible), or (b) construct a completely new Statement, and guess which test method to call based on the base Statement's Description. That won't work at all if (eg) another runner creates artificial test method names. In other cases, it might appear to work, but any other Rules would be unable to do their thing (because their custom Statements would be discarded by CdiUnitRule). I suppose this could work if you don't have any other Rules, and the other Runners don't do anything too interesting. It would be a real kludge though.

For option (a), it might be possible to use something like https://deltaspike.apache.org/javadoc/1.4.2/org/apache/deltaspike/core/api/provider/BeanProvider.html#injectFields-T- with the caveat that the Test class itself would not be a real CDI bean.

Another possibility might be delegating to another runner, but I haven't looked at the details of that approach.

@aschoerk
Copy link
Contributor

aschoerk commented Aug 4, 2017

Hello, because of necessary integration with camunda-rule, I tried this. The points with the classloader are valid and at the moment I am a bit astonished how well it works. The main challenge I faced was, how to make the Testclass work in the CDI-Container since it normally contains @produces and @Inject.... To be able to do this, I created the Testclass once more "ApplicationScoped" and copied all fields.

Until now that has only been done in ejb-cdi-unit with EjbUnitRunner which is on 3.1.4-Version yet. I would like to discuss it and if you think there is some merit, perhaps I can port that to the current cdi-unit-version:
See EjbUnitRule the interesting part is perhaps initWeldFields.

The Usage of the Rule together with the CamundaRule you can see at: EjbCamundaUnitRule

@ikysil
Copy link
Collaborator

ikysil commented May 20, 2020

Hello,

It looks like we have the same issue with TestNG support.
I could not find any way to instantiate the test class from the Weld since in TestNG the factory method is declared on the test class.

The practical consequence is that no interceptors are running to support @InRequestScope and similar annotations. That's why the code is duplicated in NgCdiRunner - see NgCdiRunner#initContexts.

I can also imagine the effects on the usage of @Produces and @ProducesAlternative annotations on the methods/fields in the test class if those depend on the state of the instance.

@ikysil ikysil added area/junit kind/enhancement New feature or request labels Jul 16, 2024
@ikysil
Copy link
Collaborator

ikysil commented Jul 31, 2024

TestNG listeners were implemented at #276.
JUnit rule should be possible as well.

@ikysil ikysil added this to the 5.0.0 milestone Jul 31, 2024
@ikysil ikysil self-assigned this Jul 31, 2024
@ikysil ikysil linked a pull request Jul 31, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/junit kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants