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

Unable to mock GHObject#id #1739

Open
Haarolean opened this issue Nov 9, 2023 · 10 comments
Open

Unable to mock GHObject#id #1739

Haarolean opened this issue Nov 9, 2023 · 10 comments
Labels

Comments

@Haarolean
Copy link
Contributor

Hi,

it seems like it's impossible to mock GHObject via Mockito, I observe this behavior:

try {
    var a = Mockito.mock(GHObject.class);
    when(a.getId()).thenReturn(123L);
} catch (Throwable e) {
    e.printStackTrace();
}

The exception:

java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because the return value of "java.util.concurrent.Callable.call()" is null
	at org.kohsuke.github.GHObject.getId(GHObject.java:140)
	at org.kohsuke.github.GHObject.getId(GHObject.java:140)

I believe this may be related to bridge methods, but I have no idea how to implement a workaround.

Could you please elaborate if this is possible?

@Haarolean
Copy link
Contributor Author

Seems related: mockito/mockito#701

@gsmet
Copy link
Contributor

gsmet commented Nov 9, 2023

We had the issue with the Quarkus GitHub API extension + Mockito when we developed Quarkus GitHub App and Quarkus GitHub Action and we couldn't find any good workaround. Mockito and/or ByteBuddy randomly choose one of the method and you end up with flaky tests.

Our solution in this context has been to drop the bridge methods from the bytecode using some Quarkus build time magic. They are only intended for binary compatibility and in a Quarkus environment, people would have to rebuild their app after an upgrade anyway.

But obviously, this cannot be applied in all environments. In other words, you are not alone :).

@Haarolean
Copy link
Contributor Author

@gsmet thanks for the feedback, that's very unfortunate.

There are so many issues due to this back-compatibility thing firing back, that the whole experience of using this library reminds me of the "jumping on a rake" meme.
/rant

@bitwiseman
Copy link
Member

mockito/mockito#701 was closed a while ago.

@gsmet
I wish you'd filed an issue for this a while ago. It might be possible to publish a no-bridge flavor of the project as part of the release. This version would not guaranteed binary backward compatibility, but maybe that's acceptable for your scenarios. Instead of you having to remove the code, it just wouldn't be there.

@bitwiseman
Copy link
Member

I'm working on publishing a github-api-unbridged artifact that doesn't include bridge methods.
https://github.com/hub4j/github-api/tree/release/v1.x-unbridged

However, I haven't gotten it to work yet. https://github.com/hub4j/github-api/actions/runs/6968301386

@Haarolean
Copy link
Contributor Author

@bitwiseman let me know if I could help with fixing that

@bitwiseman
Copy link
Member

@Haarolean @gsmet
github-api-unbridged v1.318 has been published. We need some additional infrastructure improvements, but you should be able to use this version the same as you would github-api.

https://github.com/hub4j/github-api/actions/runs/6987836071

@Haarolean
Copy link
Contributor Author

Thank you very much, it works!

@bitwiseman should we close this?

@bitwiseman
Copy link
Member

@Haarolean
Great! We need to note this in the documentation and/or Readme so others will know it exists and what its limitations are (backwards compatibility not guaranteed).

@Haarolean
Copy link
Contributor Author

Sure, I'll raise a PR then

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

Successfully merging a pull request may close this issue.

3 participants