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

MockitoExtension cannot be used with JUnit 5 in OSGi integration tests #1997

Closed
wborn opened this issue Aug 8, 2020 · 8 comments
Closed

Comments

@wborn
Copy link

wborn commented Aug 8, 2020

The Mockito junit-jupiter JAR is missing OSGi bundle manifest entries so it cannot be used in OSGi integration tests.

We run some OSGi integration tests (using bnd) that have @Mocks so it would be easier if these mocks can be initialized by annotating classes with @MockitoExtension instead of using MockitoAnnotations.openMocks(this), see for example this FirmwareUpdateServiceTest.

What may complicate adding the manifest entries is that MockitoExtension is importing internal classes which aren't exported by the mockito-core bundle. What may help is to make junit-jupiter a bundle fragment with mockito-core as host.

@kriegfrj
Copy link
Contributor

Fragments can be a bit of a pain.

@rotty3000 could offer some advice here - he did the OSGi metadata port for the JUnit 5 project itself. They had a similar problem, in that some of the bundles relied on internal packages of other bundles. I think that they ended up exporting the "internal" packages, but flagged them with an attribute to mark them as "internal use only".

@timothyjward
Copy link

I would also like to see a solution to this issue. Making the mockito-junit-jupiter jar into a fragment which exports the MockitoExtension package and attaches to mockito-core does sound like a good plan.

@rotty3000
Copy link
Contributor

I've got a set of changes that makes mockito-junit-jupiter work in OSGi using the same strategy that junit5 used to identify "friend" packages that are exported but not intended for public use outside of mockito.

@raphw do you think your team would be interested?

rotty3000 added a commit to rotty3000/mockito that referenced this issue Sep 17, 2020
…Extension cannot be used with JUnit 5 in OSGi integration tests

without
a) giving up the internal nature of some packages
b) resorting to substandard solutions like OSGi fragements

Signed-off-by: Raymond Augé <raymond.auge@liferay.com>
@raphw
Copy link
Member

raphw commented Sep 17, 2020

Looks good to me, I had a look at the build and I know too little about OSGi to understand the effect the API guardian dependencies have but since the classes only carry annotations and the dependency is not required, I do not see any chance of a regression.

@TimvdLippe - could you have a second look, you know the build better then me. To me it looks like a good merge.

@rotty3000
Copy link
Contributor

For your edification:
a) apiguardian does not bring with it any runtime dependencies (confirmed by the OSGi metadata)
b) the interaction between API guardian and bnd is explained here
c) the build adds a verifyOSGi gradle task glued onto check to make sure that future changes do not break the relationship required between mockito-core and mockito-junit-jupiter such that they continue working together in OSGi

rotty3000 added a commit to rotty3000/mockito that referenced this issue Dec 18, 2020
…Extension cannot be used with JUnit 5 in OSGi integration tests

without
a) giving up the internal nature of some packages
b) resorting to substandard solutions like OSGi fragements

Signed-off-by: Raymond Augé <raymond.auge@liferay.com>
@wborn
Copy link
Author

wborn commented Mar 12, 2023

Today I tried a recent version of Mockito 5 with OSGi but it doesn't work anymore, see also #2708. 😞
Perhaps some of the commenters here can help with fixing it?

@TimvdLippe
Copy link
Contributor

@wborn Unfortunately I don't have a lot of experience with OSGi, but would be happy to review a PR that fixes it (with an included regression test). Apologies for the breakage!

@raphw
Copy link
Member

raphw commented Mar 12, 2023

I think there needs to be some form of import of the boot loader package that we add. Not sure if such an import is considered in OSGi, but I'd need a reproduction to look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants