Skip to content

Java-friendly APIs for MockWebServer #9

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

Open
Tracked by #4283
ibrhmyzc opened this issue May 15, 2022 · 2 comments
Open
Tracked by #4283

Java-friendly APIs for MockWebServer #9

ibrhmyzc opened this issue May 15, 2022 · 2 comments

Comments

@ibrhmyzc
Copy link

I imported these in my spring boot project.
testImplementation project(":testing:unit-testing")
testImplementation("com.apollographql.apollo3:apollo-mockserver:3.3.0")
api "com.apollographql.apollo:apollo-runtime"

For example, when I want to create a MockResponse, there are no builder or set methods. I can only call get methods for these objects. Or when I want to set URL for MockServer, it gives this error
Required type: Continuation <? super java.lang.String>
Provided: String

I hope its not an obvious question. Am i importing something wrong? or I cannot those in Java anymore?
Thank you in advance!

@martinbonnin
Copy link
Collaborator

martinbonnin commented May 15, 2022

Hi 👋 Indeed, using MockServer in Java is technically doable but it's a bit awkward. For some background there, we mostly designed MockServer for the Apollo integration tests and published them in hope that it could benefit the community but a lot of the APIs are pretty experimental. And as you found out, it's mainly designed for Kotlin.

If you really want to, you can get mockServer.url with this trick:

    /**
     * Because url doesn't suspend on the JVM, we can just use the return value
     */
    String url = (String) mockServer.url(new Continuation<String>() {
      @NotNull @Override public CoroutineContext getContext() {
        return EmptyCoroutineContext.INSTANCE;
      }

      @Override public void resumeWith(@NotNull Object o) {
      }
    });

Or write a little bit of Kotlin code to bridge the suspend function (see this stackoverflow answer).

All in all, for Java use cases, I would recommend using OkHttp MockWebServer. The main thing that the Apollo MockServer supports is multiplatform but if you're not using that, OkHttp MockWebServer should provide most of the API (and actually Apollo MockServer uses OkHttp MockWebServer under the hood) using a much more Java-friendly API.

Hope this helps!

@martinbonnin martinbonnin changed the title Is it possible to create a MockWebServer or MockResponse with Java Java-friendly APIs for MockWebServer May 16, 2022
@martinbonnin
Copy link
Collaborator

@ibrhmyzc I just updated the doc to be more explicit about MockServer being mostly Kotlin so far.

I'll keep this is issue around to keep track of improvements to the MockServer APIs and renamed it accordingly, I hope you don't mind.

@martinbonnin martinbonnin transferred this issue from apollographql/apollo-kotlin Sep 13, 2024
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

2 participants