Testing
The Java SDK comes with the module sdk-testing
that integrates with JUnit 5 and TestContainers to start up a Restate container together with your services code and automatically register them.
implementation("dev.restate:sdk-testing:1.2.0")
Using the JUnit 5 Extension
Given the service to test GreeterService
, annotate the test class with @RestateTest
and annotate the services to bind with @BindService
:
- Java
- Kotlin
@RestateTestclass GreeterTest {@BindService GreeterService service = new GreeterService();// Your tests}
@RestateTestclass GreeterTest {@BindService val service = GreeterService()// Your tests}
Note that the extension will start one Restate server for the whole test class. For more details, checkout RestateTest
Javadocs.
Once the extension is set, you can implement your test methods as usual, and inject a Client
using @RestateClient
to interact with Restate and the registered services:
- Java
- Kotlin
@Testvoid testGreet(@RestateClient Client ingressClient) {// Create the service client from the injected ingress clientvar client = GreeterServiceClient.fromClient(ingressClient);// Send request to service and assert the responsevar response = client.greet("Francesco");assertEquals(response, "Hello, Francesco!");}
@Testfun testGreet(@RestateClient ingressClient: Client) = runTest {// Create the service client from the injected ingress clientval client = GreeterServiceClient.fromClient(ingressClient)// Send request to service and assert the responseval response = client.greet("Francesco")assertEquals(response, "Hello, Francesco!")}
Usage without JUnit 5
You can use the testing tools without JUnit 5 by creating a ManualRestateRunner
with RestateRunnerBuilder#buildManualRunner
.
For more details, refer to the Javadocs.