Testing http4s + ZIO endpoints


I’m trying to test my ZServerEndpoints, preferably using ZIO Test. I have not found examples of this, so I’m trying to use the example on tapir’s website. However, the example uses SttpBackendStub.asynchronousFuture, which I don’t think is what I need for ZIO.

The docs say to check sttp’s docs, and there it says to use HttpClientZioBackend.stub and add "com.softwaremill.sttp.client3" %% "zio" % "3.8.7" as a dependency, but the examples on tapir’s GitHub use a different dependency, "com.softwaremill.sttp.client3" %% "async-http-client-backend-zio" % Versions.sttp, so I’m very confused :frowning:

Any help is greatly appreciated! :pray:

There’s a couple of ZIO-compatible backends (they use different low-level http client implementations). If you’re using HttpClientZioBackend in your app, you should use HttpClientZioBackend.stub to create the stub. This comes from the "com.softwaremill.sttp.client3" %% "zio" dependency. Other backends are included in other dependencies.

Can you send a link to the page and section in the docs you found misleading? Maybe there’s something to fix :slight_smile:

What I ended up using (and I don’t know why I forgot about it in the first place) is the example test that comes with “Adopt tapir”, which only requires "com.softwaremill.sttp.tapir" %% "tapir-sttp-stub-server", just had to change the Any in RIOMonadError[Any] to the needed environment for every test and it worked like a charm.

The docs here say the following:

The special backend that is described above is based on a SttpBackendStub, which can be used to stub arbitrary behaviors. See the sttp documentation for details.

Tapir builds upon the SttpBackendStub to enable stubbing using Endpoints or ServerEndpoints. To start, add the dependency:

"com.softwaremill.sttp.tapir" %% "tapir-sttp-stub-server" % "1.2.5"

Which, in hindsight, is correct, I don’t actually need neither the ZIO dependency from sttp nor the ones from the GitHub examples, just the above dependency.

Since “Adopt tapir” comes with an example test given the effect type and server library, I think it’d be helpful to mention, maybe in addition to reading the sttp docs, that such an example exists in “Adopt tapir”.

Thanks for your help, Adam :heart:

1 Like

Done: Schemas for any / any object by adamw · Pull Request #2673 · softwaremill/tapir · GitHub :slight_smile:

1 Like